1

On parent theme index.php i have this code:

// The function.
add_action( 'mytheme_example_action', 'mytheme_example_function' );
function mytheme_example_function() {
    echo 'Example text on index page.'
}

// After function was defined i do the action.
do_action( 'mytheme_example_action' );

On child theme functions.php i have this code:

// Example 1 (not working).
add_action( 'mytheme_example_action', 'mytheme_remove_parent_function', 20);
function mytheme_remove_parent_function() {
    remove_action( 'mytheme_example_action', 'mytheme_example_function' );
}

// Example 2 (not working).
remove_action( 'mytheme_example_action', 'mytheme_example_function' );

I have tried both examples, still the echoed text 'Example text on index page.' is showing on browser. Any help?

2

Answer

  1. For removing an action hook use the same action name, callback name and priority that was used to add a action in parent theme.
  2. You should move the function from being defined in index.php to functions.php of the parent theme.
  3. Lastly, you could register the child function using init.

So you end up with something like this:


Parent Theme Code

functions.php

// The function.
add_action( 'mytheme_example_action', 'mytheme_example_function' );
function mytheme_example_function() {
    echo 'Example text on index page.';
}

Child Theme Code

functions.php

// Example 1
add_action( 'init', 'mytheme_remove_parent_function');
function mytheme_remove_parent_function() {
     remove_action( 'mytheme_example_action', 'mytheme_example_function' );
}

Final Notes

Here's a great tutorial that'll show you a couple methods of achieving your desired outcome.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.