WordPress add a function

If you want to add custom code to the theme, then we suggest you to install and activate child theme. Otherwise, after each update of Kalium you have to re-insert that custom code you had previously used.

We occasionally ask our users to add custom code to their WordPress website to add simple features to GiveWP. We do this because 80% of GiveWP users don’t need that feature so we don’t want to add it to the core plugin. But where and how exactly are you supposed to add this custom code to your website? This article gives you an overview of why and how to do this right.

Dispelling the Myths of Custom Code in WordPress

Experienced developers know the best way to extend the functionality of a plugin is by adding their own code into what’s often called a “Functionality Plugin“. You might think that you can simply go into the plugin files and change them there. While it is true that you CAN do that, it’s not “best practice” because the next time your plugin is updated you will lose that custom code.

Another thing you’ll often hear is that you should just put this code into your theme’s `functions.php` file. Again, while this will work, it’s not considered “best practice” because your site most likely will update it’s theme in the near future and again you’ll lose that added functionality to the plugin once your theme changes.

So, the GiveWP Support team gave you some custom code to use on your website, or you pulled it from our documentation. But you CAN’T change the plugin, you can’t add it to your theme’s `functions.php` file, and you don’t know how to create a Functionality Plugin. What are you supposed to do?

It is not “Best Practice” to put custom plugin code in your theme’s functions.php file.

Use the “Code Snippets” plugin

susie@example.org'; wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: //blog.example.com' ); return $post_id; } add_action( 'publish_post', 'wpdocs_email_friends' );

Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: 9You must log in to vote on the helpfulness of this note

    Contributed by mkormendy

    To pass a variable to the called function of the action, you can use closures (since PHP 5.3+) when the argument is not available in the original coded do_action. For example:

    add_action('wp_footer', function($arguments) use ($myvar) { echo $myvar; }, $priority_integer, $accepted_arguments_integer);

    • Don’t use anonymous closures on themes you want to distribute. It would be impossible to remove the action. Instead use closure variables: function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }3 which can be removed with: function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }4 with the advantage that typos in the closure name will yeild a helpfull Undfined variable error on typos. — By theking2 —

    Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note

    Contributed by Anthony Hortin

    Related:

    do_action()
    remove_action()

    Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: 3You must log in to vote on the helpfulness of this note

    Contributed by Lucas Bustamante

    Passing parameters while using in a Class
    To pass parameters to your method in a Class while calling it with add_action, you can do as following:

    public function __construct() { // Actions add_action('init', array($this, 'call_somefunction')); } /** * Intermediate function to call add_action with parameters */ public function call_somefunction() { $this->somefunction('Hello World'); } /** * Actual function that does something */ public function somefunction($text) { echo $text; }

    Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: 3You must log in to vote on the helpfulness of this note

    Contributed by Christian Saborio

    How to add an action that calls a function (with parameters) from an instantiated class:

    do_action( 'save_post', $post_ID, $post, $update );0

    Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Accepted Arguments
    A hooked function can optionally accept arguments from the action call, if any are set to be passed. In this simplistic example, the function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }5 function takes the function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }6 argument, which is automatically passed to when the function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }7 call using the function wpdocs_my_save_post( $post_ID, $post, $update ) { // do stuff here }8 filter hook is run.

    do_action( 'save_post', $post_ID, $post, $update );1

    Log in to add feedback
  • You must log in to vote on the helpfulness of this noteVote results for this note: -3You must log in to vote on the helpfulness of this note

    Contributed by theking2

    To prevent runtime errors due to easy typo errors (defensive programming) and prevent pollution of the global namespace use closures instead of function names. The sample code adjusted:

    do_action( 'save_post', $post_ID, $post, $update );2