How do do_action and add_action work?

前端 未结 4 1374
执念已碎
执念已碎 2020-12-14 18:42

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.



        
相关标签:
4条回答
  • 2020-12-14 18:55

    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');
    
    0 讨论(0)
  • 2020-12-14 18:57

    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.

    0 讨论(0)
  • 2020-12-14 19:09

    //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.

    0 讨论(0)
  • 2020-12-14 19:12

    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()
    0 讨论(0)
提交回复
热议问题