WordPress remove_action not working in child theme

I’m using the Unite theme in WordPress with a child theme I have created.

When trying to remove an action the php error indicates that the functions.php file from the child theme is loading before the parent /inc/extras.php which contains the hook I want to override.

Read More

Trying to do everything correctly rather than just making the quick and dirty change in the parent theme.

Parent /inc/extras.php loaded from include in parent functions.php;

add_action('woocommerce_before_main_content', 'unite_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'unite_wrapper_end', 10);

function unite_wrapper_start() {
  echo '<div id="primary" class="col-md-8">';
}

function unite_wrapper_end() {
  echo '</div>';
}

When I add remove to the same file it works;

add_action('woocommerce_before_main_content', 'unite_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'unite_wrapper_end', 10);

function unite_wrapper_start() {
  echo '<div id="primary" class="col-md-8">';
}

function unite_wrapper_end() {
  echo '</div>';
}

remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);

however, when I add to the child functions.php file it does not;

remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);

Child functions.php files is certainly loading and working…

Why would this be?

Related posts

2 comments

  1. add_action('init' , 'remove_functions' , 15 );
    function remove_functions() {
     remove_action('woocommerce_before_main_content', 'unite_wrapper_start',10);
    }
    add_action('woocommerce_before_main_content', 'unite_wrapper_start1', 10);
    function unite_wrapper_start1(){
     echo '<div id="primary" class="col-md-12">';
    }
    
  2. Not so much another answer as an addition to Stuart’s answer above.

    This can be used to remove parent theme customisations (dynamic ‘inc’ files) as in a theme like Bard where the parent functions.php ‘requires’ an ‘inc’ file containing a further function and action (bard_dynamic_css).

    1) Parent functions file reads:

    require get_parent_theme_file_path( '/inc/customizer/customizer-defaults.php' );
    

    2) in child functions file:

    add_action('init' , 'remove_functions' , 15 );
     function remove_functions() {
     remove_action( 'wp_head', 'bard_dynamic_css',10);
    }
    

    3) Then require child dynamic css

    require get_stylesheet_directory() . '/inc/customizer/dynamic-css.php';
    

    REMEMBER: change the original function name (in child ‘dynamic-css.php’) to avoid conflicts e.g. ‘bard_dynamic_css’ TO ‘child_bard_dynamic_css’

Comments are closed.