0

Updated Question:

I’ve declared a basic child theme. As I create a function to enqueue stylesheets, my website crashes and gives me a Fatal error: Cannot redeclare thando_stylesheets() (previously declared in........ load_stylesheets() is a new function and has not been declared yet but I am getting an error saying it has been declared.

my function is new and has not been declared yet. Why am I getting redeclare error? All my other previously created themes do not have such an issue

Here is the error log

PHP Fatal error: Cannot redeclare thando_stylesheets() (previously declared in /Applications/MAMP/htdocs/In-progress/bros/wp-content/themes/wifibros/functions.php:5) in /Applications/MAMP/htdocs/In-progress/bros/wp-content/themes/Wifibros/functions.php on line 3

function thando_stylesheets() { 
    wp_register_style('bootstrap', 
        get_template_directory_uri().'/css/bootstrap.min.css',
        array(), 
        1, 
       'all');
    wp_enqueue_style('bootstrap'); 
}
1
  • The path after "previously declared in" gives you the exact place where it has been declared.
    – fuxia
    Aug 25, 2021 at 7:52

1 Answer 1

0

You can't use a function name in your child theme that is in use by your parent theme. Your parent theme likely has a function named load_stylesheets(), so if you add a function with that name to your child theme you will get this error.

To work around this you need to give your child theme's function a unique name. Issues like this is why it's best practice to prefix your function names (eg. mytheme_load_stylesheets()) or use namespaces.

Not that in some cases a parent theme might have "pluggable" functions. If a parent theme defines a function inside a condition, like this:

if ( ! function_exists( 'load_stylesheets' ) ) {
    function load_stylesheets() {
        // etc.
    }
}

Then it will be possible to replace that function in your child theme by creating a new function with the same name. This is possible because the parent theme is checking that your child theme hasn't already defined a function with that name before it defines it itself. If the parent theme does not perform this check then you can't use the name.

5
  • Jacob thanks for the clarification, adding the function_exists, does take away the error. What I would like to know is, even after I renamed my function to my very own name "thando_stylesheets", I still get the same error. whats more confusing is that this load_stylesheets() worked previously but for this new website its giving me this error. I am baffled at the error message, all my other child themes for different website have no error message like this Aug 25, 2021 at 3:43
  • Are you sure you just haven't accidentally defined the function twice? That's the only way this error can happen. Somewhere you're using the same function twice. The error message should even tell you where the function was originally defined. Aug 25, 2021 at 3:55
  • Here is the error log PHP Fatal error: Cannot redeclare thando_stylesheets() (previously declared in /Applications/MAMP/htdocs/In-progress/bros/wp-content/themes/wifibros/functions.php:5) in /Applications/MAMP/htdocs/In-progress/bros/wp-content/themes/Wifibros/functions.php on line 3 function thando_stylesheets() { wp_register_style('bootstrap', get_template_directory_uri().'/css/bootstrap.min.css', array(), 1, 'all'); wp_enqueue_style('bootstrap'); Aug 25, 2021 at 4:04
  • 1
    Ok, and have you checked those files on those lines? It looks like you have two themes with the same name but one is capitalised. Is one of them the parent theme? Aug 25, 2021 at 6:20
  • I changed the name of my website directory and that shorted out my issue. Thank you Aug 29, 2021 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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