2

What is the difference between following two codes? (Directly call a function and call a function using add_action)

function pp_submit__link_form(){
      if(isset( $_POST['action']) && $_POST['action']="submit_link" ){
          echo "Hello";
      }
    }
add_action( 'init', 'pp_submit__link_form' );

function pp_submit__link_form(){
  if(isset( $_POST['pp_action']) && $_POST['pp_action']="submit_link" ){
      echo "Hello";
  }
}
pp_submit__link_form();
  • 1
    add_action doesn't call the function. it hooks the fonction to the hook init in a first time. and in a second time the function is called when the init is fired. look this to know when init is called : wordpress.stackexchange.com/questions/71406/… – mmm Sep 17 '16 at 9:26
0

In the first block of code, pp_submit__link_form() will be fired on the init action in WordPress.

In the second block of code, pp_submit__link_form() will be fired immediately at the time the function is called.

|improve this answer|||||
0

In the first example you are registering the function 'pp_submit__link_form' into the action hook 'init'. So that means when WordPress fires the action 'init' (by calling do_action('init') ) it will include your function call.

See the WordPress codex on hooks, actions and filters

In the second example you are simply calling that function in the general execution of your php script So it will fire everytime your plugin is loaded.

From your example it looks like you want to do something with the $_POST data, (maybe when someone posts a form) so the first example is the correct one to use.

|improve this answer|||||
0

Another reason to use add_action() when you want to call a function is, its opposite, the remove_action(), which allow any plugin/script to not call the function when the action is running.

add_action / add_filter, remove_action / remove_filter are the best for the developper to maximize the customization of a plugin, theme and core behaviours.

So, in any running process you'll be able to remove a function called and right after add your own function by using add_action(). For example, from your code:

remove_action('init', 'pp_submit__link_form');
action_action('init', 'my_pp_submit__link_form);

function my_pp_submit__link_form(){
    if(isset( $_POST['action']) && $_POST['action']="submit_link" ){
      echo "Good bye, see you later, I'm tired";
  }
}
|improve this answer|||||
0

1) when executing directly, you cant access almost any important WP global variable, because the query/variables are run within add_action functions.

2) using add_action you execute function whenever it is necessary. For example, you want to check logged-in user, then you should do it after 'init' action started.

3) add_action is the recommended way in most cases

4) with add_action you make function to be "removable" also,if you meet some conditions... i.e:

if($url = 'mypage.php') {  remove_action('wp_footer'); }

5) with add_action you make your function to be readable by DEBUGER plugins, to check that function's execution time,CPU usage, and etc...

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.