Override a WordPress parent theme function

I am creating a child theme of flowmaster theme.I have a problem to override the parent function.
The function exists in parent’s theme:

add_filter('loop_shop_columns', 'pt_loop_shop_columns');
function pt_loop_shop_columns(){
    if ( 'layout-one-col' == pt_show_layout() ) return 4;
    else return 3;
}

I add a function in child theme

Read More
if ( ! function_exists( 'pt_loop_shop_columns' ) ) :
function pt_loop_shop_columns(){
    global $wp_query;
    if ( 'layout-one-col' == pt_show_layout() ) return 4;
    else return 4;
}
endif;
add_filter('loop_shop_columns', 'pt_loop_shop_columns');

Got this error:

Fatal error: Cannot redeclare pt_loop_shop_columns() (previously
declared in
C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster-child\functions.php:44)
in
C:\xampp\htdocs\futuratab\wp-content\themes\flowmaster\woofunctions.php
on line 9

Please help.Thanks

Related posts

3 comments

  1. Function of child theme is executed first and then parent theme’s. Checking using function_exists should have been done in parent theme.

    To overcome this you can remove parent theme’s hook and hook your custom function to same filter.

    remove_filter('loop_shop_columns', 'pt_loop_shop_columns');
    
    add_filter('loop_shop_columns', 'custom_pt_loop_shop_columns');
    
    function custom_pt_loop_shop_columns(){
        global $wp_query;
        if ( 'layout-one-col' == pt_show_layout() ) return 4;
        else return 4;
    }
    
  2. You can’t redefine a function in PHP, but you can unhook the old function and hook the new one with a different name. Something like:

    remove_filter('loop_shop_columns', 'pt_loop_shop_columns');
    add_filter('loop_shop_columns', 'pt_loop_shop_columns_2');
    
  3. you can try this on your child theme

    function pt_loop_shop_columns() {
    
    //NEW CODE IN HERE///////////////////////////////
    
    return apply_filters('pt_loop_shop_columns', $link, $id);
    }
    
    add_filter('attachment_link', 'pt_loop_shop_columns');
    

    OR

    you can use hook on existing function

    function pt_loop_shop_columns() {
    //code goes here
    }
    
    $hook = 'get_options'; // the function name you're filtering
    add_filter( $hook, 'pt_loop_shop_columns' );
    

    OR

    And last method is

     function remove_thematic_actions() {
    remove_action('thematic_header','thematic_blogtitle',3);
    }
    // Call 'remove_thematic_actions' during WP initialization
    add_action('init','remove_thematic_actions');
    
    // Add our custom function to the 'thematic_header' phase
    add_action('thematic_header','fancy_theme_blogtitle', 3);
    

Comments are closed.