When you make a WordPress child theme and want to override functions declared in the parent theme there aren’t any good documentation available from WordPress. In the reference manual you’ll find the the remove_action remove_action( $tag, $function_to_remove, $priority, $accepted_args)

The first location that you probably try to apply this is in the ”functions.php” file by adding something like this:


remove_action('wp_head','oldFunction');
function newFunction() { echo "use me instead"; }
add_action('wp_head','newFunction');

However, this is not going to work. The oldFunction is still going to be executed and the new one as well. The child themes ”functions.php” is executed before the parent themes ”functions.php” and the remove_action command doesn’t know about the function it is trying to remove. We somehow need to remove the old function after it has been declared. The wordpress hook after_setup_theme seems to be a good candidate for a solution because it runs AFTER the parent and childs functions are loaded.

Use it like this:

function removeOldFunction() {
remove_action('wp_head','oldFunction');
add_action('wp_head','newFunction');
}
function newFunction() { echo "use me instead"; }
add_action('after_setup_theme','removeOldFunction');

Good reading
WordPress theme function files
The Right Way to Override Theme Functions

Share →

3 Responses to Using remove_action in a WordPress child theme

  1. Adrian B skriver:

    Tack, hjälpte mig i nattens sena timme!

  2. Marco skriver:

    Hi! Thanks for this manual!

    Sadly, it doesn´t work for me.
    There is a function ”my_javascripts” that refers to several js-files in the parent theme. I need to change some of them so I tried your method, but it´s not beeing recognized.

    In functions.php of the child theme I wrote:
    function removeOldFunction() {
    remove_action('wp_head','my_javascripts');
    add_action('wp_head','newFunction');
    }

    function newFunction() {
    .... example:
    wp_enqueue_script('main', get_stylesheet_directory_uri() . '/js/main.js','','',false);
    ....
    }

    add_action('after_setup_theme','removeOldFunction');

    What´s wrong? =(

  3. Eric skriver:

    Thanks! I’ve been struggling with a WooCommerce parent theme and a child theme. remove_action() never worked. This fixed the issue perfectly for me.

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *

Följande HTML-taggar och attribut är tillåtna: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>