Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do do_action and add_action work?

Tags:

I am trying to find what exactly do_action and add_action works. I already examine with add_action but for do_action i am trying as new now. This what i tried.

function mainplugin_test() {  $regularprice = 50;  if(class_exists('rs_dynamic')) { $regularprice = 100; }  // and doing further //like i echoing the regular price echo $regularprice; //It print 100 from this code  } 

Now instead of placing few code in main file i am planning to create do_action to avoid code messing issue.

    function mainplugin_test() {      $regularprice = 50;      do_action('testinghook');  // and doing further //like i echoing the regular price echo $regularprice; //It should print 100 but it print 50      } 

so i created another function to point out that hook as something like

function anothertest() { if(class_exists('rs_dynamic')) { $regularprice = 100; } } add_action('testinghook','anothertest'); 

Not sure how to add the lines of code to that hook that above function may work? As per i tried in my testing environment nothing helps. If i understand correct do_action is more like including a file??? If not please advise me.

Thanks.

like image 362
Vignesh Pichamani Avatar asked Nov 06 '14 14:11

Vignesh Pichamani


People also ask

What's the difference between Do_action and Add_filter?

add_action is used to perform a particular action when some event occurs. Eg: when user adds a post, I want to check if the similar post already exists or not, for this I will add an action to the submit post event. add_filter is used when we want to replace/filter something when some event occurs.

Can I pass arguments to my function through Add_action?

Yes you can! The trick really is in what type of function you pass to add_action and what you expect from do_action.

Does action hook WordPress?

Action Hooks are a very useful tool in WordPress and they are used to perform functions (actions) in specific places of a theme or plugin. Many themes and plugins, such as Total, use action hooks as an easy way for users to modify the output of the project or to add their own custom code.

What is Add_action init?

The benefit to using add_action is that you allow core wordpress functions to keep track of what has been added, and by doing so, can override previously added functions by de-registering them later on. For example: You download a plugin with a defined action/method named. add_action( 'init', 'crappy_method' );


1 Answers

do_action creates an action hook, add_action executes hooked functions when that hook is called.

For example, if you add the following in your theme's footer:

do_action( 'my_footer_hook' ); 

You can echo content at that location from functions.php or a custom plugin:

add_action( 'my_footer_hook', 'my_footer_echo' ); function my_footer_echo(){     echo 'hello world'; } 

You can also pass variables to a hook:

do_action( 'my_footer_hook', home_url( '/' ) ); 

Which you can use in the callback function:

add_action( 'my_footer_hook', 'my_footer_echo', 10, 1 ); function my_footer_echo( $url ){     echo "The home url is $url"; } 

In your case, you're probably trying to filter the value based on a condition. That's what filter hooks are for:

function mainplugin_test() {     echo apply_filters( 'my_price_filter', 50 ); }  add_filter( 'my_price_filter', 'modify_price', 10, 1 ); function modify_price( $value ) {     if( class_exists( 'rs_dynamic' ) )         $value = 100;     return $value; } 

Reference

  • add_action()
  • do_action()
  • add_filter()
  • apply_filters()

Edit (updated references links)

like image 159
diggy Avatar answered Oct 04 '22 22:10

diggy