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.
The reason it didn't print 100
, because $regularprice
within anothertest()
function is local to that function. The variable $regularprice
used in parent mainplugin_test()
function is not same as the variable used in anothertest()
function, they are in separate scope.
So you need to either define the $regularprice
in a global scope (which is not a good idea) or you can pass argument as a parameter to do_action_ref_array
. The do_action_ref_array
is the same as do_action
instead it accepts second parameter as array of parameters.
Passing as argument:
function mainplugin_test() {
$regularprice = 50;
// passing as argument as reference
do_action_ref_array('testinghook', array(&$regularprice));
echo $regularprice; //It should print 100
}
// passing variable by reference
function anothertest(&$regularprice) {
if(class_exists('rs_dynamic')) {
$regularprice = 100;
}
}
// remain same
add_action('testinghook','anothertest');
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()
Actually, the add_action
is an action hook which is used to invoke an action (a registered handler) on a certain point depending on the action and the do_action
is used to manually invoke that registered action. For example:
add_action('some_hook', 'handler_for_some_hook');
This handler will be invoked when you take or the script does the some_action
but if you want you may invoke that action manually using the do_action
. So, basically the do_action
invokes the registered action hook when you call it. Check more on Codex.
//with do_action we create own tag(hook) use in wordpress
Ex. Here I added 1 custom function to call this
In add_action I added function name with any custom tag name
With do_action(my custom tag name)
function mywork()
{
echo "display my name";
}
add_action('mytag','mywork');
do_action('mytag');
---add_action()---
hook-specific hook that will affect by function()
add_action( hook, **function**() )
It will change functionality and behaviour of wordpress by Specific “hook” we can change it and functions method.