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 feedbackYou 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 —
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()
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:
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:
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.
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 );2Do not do this in themes or plugins you intend to distribute, as it will prevent others from removing your callables if desired. — By crstauf —
How do I add a function to a specific page in WordPress?
You can use the is_page() conditional tag to target a specific WordPress page to make your changes. You can also use the is_single() tag to target an individual post in the exact same way. You'll do a conditional check for this page (or post if using is_single() instead) by targeting the page ID, title, or slug.How to add custom function to functions php in WordPress?
To add your code to functions. php go to Appearance > Editor then select Theme Functions file and paste your code in the end of editor and click Update File: If there is a syntax error on that code, your site will stop working! Before making changes backup your functions.How do I call a custom function in WordPress?
So effectively:.Rename your function to something a bit more unique, like naser_send_notitification().Put this code in /wp-content/mu-plugins/custom-funtions.php..How do I trigger a function in WordPress?
You have three options for this:.Code the function directly into your theme template file (or another plugin file)..Attach it to an action hook..Attach it to a filter hook..