How do do_action and add_action work?

偶尔善良 提交于 2019-11-29 03:37:33

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!