[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2154
  • Last Modified:

What is the diff between do_action and add_action in wordpress ?

What is the difference between do_action and add_action functions in wordpress. I have read about them, but I have a confusion:: I feel that add_action is mandatory for adding functions to a location, then only we can call do_action to call those functions. We can not call do_action without addding any functions to an action. Am I right or not ?

Also I am working on a wordpress project for the first time.

I have a lot of do_action calls in header.php file and if I remove do_action calls, I can not see any effect on the browser output. Also I cannot find the corresponding add_action calls for those do_action calls in my project.
0
puneetdudeja
Asked:
puneetdudeja
  • 2
3 Solutions
 
NeoAshuraCommented:
The do_action function finds out about the functions it should include by a sibling function named add_action, which creates a list of all functions. You can thing of add_action  as the boss and do_action as the worker. This function, like its sibling, is a predefined function in WordPress, so you only need to call it. This function call is often included next to the function definition its mentioning. The following is an example of defining my_new_code and asks WordPress to hook it on:

<?php

function my_new_code($name) {

echo “Hello ” . $name;

}

add_action(’the_location’, ‘my_new_code’);

?>
0
 
gwkgCommented:
You can "add_action" all you like, but until you "do_action" nothing is going to change.

say you had these functions in functions.php

function function1() { echo "Hello "; }
function function2() { echo "John!"; }

You could simply add them to the init function

add_action('init', 'function1');
add_action('init', 'function2');

And they will display at the top of your page without you specifically calling "do_action" because Wordpress is already going to "do" 'init' upon initialization.

But if you add them to something that does not already exist...

add_action("doit", "function1");
add_action("doit", "function2");

Nothing will happen until you "doit"

do_action('doit');

0
 
gwkgCommented:
Heres a list of all the existing actions you can "add_action" to

http://codex.wordpress.org/Plugin_API/Action_Reference
0
 
link2caroCommented:
To say it briefly,

add_action/add_filter are for registering the action/filter to WordPress

do_action/do_filter are for applying that registered action/filter
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now