Removing hooked actions

  • Paul.Knight


    You had noted in several posts, in your code comments, and pointed out in the codex, that actions could be removed and replaced, but none of these mentioned where such code should be placed. The remove_action needs to be called after the parent has added the hook, but the child theme’s functions.php is called before the parent’s. So what child script is called after the parent’s functions.php that would permit such removal?


    Syahir Hakim


    When removing actions, the order of execution does not matter. This is because WordPress will not execute those actions until it has finished loading up all the files.

    When you’re using a child theme’s functions.php file to invoke the removal code, WordPress take that code and put it inside the proper queue. When the theme’s functions.php is run, and the original function is added to the action queue, WordPress will check if there’s any removal code for that function in the queue. If there is, the function won’t be executed.



    That’s the piece I missing! I did not realize these actions were queued. Now, according to the codex, you may need to prioritize the removal so that it occurs after the add, but it also states, at the top, that the priority must match that of the added action. This sounds somewhat contradictory. To replace this function, do I need to specify a priority?



    I tried it out without specifying a priority and it worked first time out of the chute. Thanks so much for enlightening me!


    Syahir Hakim


    It probably means something like this:

    Take the graphene_widgets_init() function for example. It is added to the widgets_init action hook by this line:

    add_action( 'widgets_init', 'graphene_widgets_init' );

    Take note that since I didn’t explicitly specifies the priority in the above add_action call, the default priority of 10 is used.

    To remove it, you’d do something like this:

    function graphene_remove_widgets_init(){
    remove_action( 'widgets_init', 'graphene_widgets_init' );
    add_action( 'widgets_init', 'graphene_remove_widgets_init', 20 );

    The priority of 20 above will cause the add_action hook to fire later than the theme’s add_action call, so it is executed after the theme has added the action hook.

Viewing 5 posts - 1 through 5 (of 5 total)

  • You must be logged in to reply to this topic.