How to override functions.php in child theme?

I have a theme and I need to override some specific parts|behavior of it specifically on the functions.php file. I know the best practice here is doing this at child theme by placing a custom functions.php. Now this is part of the original file:

function mytheme_setup() {
    ...
    add_image_size( 'mytheme-l-thumbs' , 750 , 423 , true);
    add_image_size( 'mytheme-m-thumbs' , 555 , 313 , true);
    add_image_size( 'mytheme-s-thumbs' , 450 , 254 , true);
    add_image_size( 'mytheme-square-thumbs' , 750 , 750 , true);
    add_image_size( 'mytheme-nocrop-thumbs' , 750 , 1500 , false);

}

add_action( 'after_setup_theme', 'mytheme_setup' );

And I want to override each add_image_size by set crop to false on my child theme. I have tried by copying & pasting the function on child theme but I ended up with this error:

Cannot redeclare mytheme_setup() (previously declared in /themes/mytheme-child/functions.php:11) in /themes/mytheme/functions.php on line 88

How I can do that?

1 Answer
1

When a function is hooked, it is easy to change that in a child theme. What you need to do is

  • Remove the original call back function

  • Copy the function to your child theme and rename it.

  • Do your customizations as needed

  • Rehook your call back function

You can try the following:

// Remove the callback function from the hook
remove_action( 'after_setup_theme', 'mytheme_setup' );

// Copy the function, rename and do what you need
function my_new_callback()
{
    // Modify what you need
}

// Rehook your custom callback
add_action( 'after_setup_theme', 'my_new_callback' );

Leave a Comment