Support » Fixing WordPress » What’s the cause of this ‘Cannot redeclare’ error?

  • Resolved gulliver

    (@gulliver)


    I have a custom theme which I’ve been using for years, on various sites.

    After some minor updates and a rename, on activation I’m getting several ‘cannot redeclare’ errors for files which haven’t been modified (and which work fine when reverting to the older version of the theme, and on other sites using the updated theme).

    The issue only occurs with function files which are in a sub-folder ‘admin’.

    The functions.php file contains this:

    function require_all_files($dir) {
    foreach( glob( "$dir/*" ) as $path ) {
    if ( preg_match( '/\.php$/', $path ) ) {
    require_once $path;  // it's a PHP file so just require it
    }
    elseif ( is_dir( $path ) ) {
    require_all_files( $path );  // it's a subdir, so call the same function for this subdir
    } } }
    require_all_files( get_template_directory() . "/functions/" );
    

    And the ‘functions’ folder contains a sub-folder ‘admin’.

    I have this code, and folder/sub-folder set-up on all of my sites (including the older version of the theme) – and it works fine except for one.

    On renaming that ‘admin’ sub-folder, things work ok.

    I’m baffled.

    • This topic was modified 2 years, 3 months ago by gulliver.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, @gulliver.

    To answer your main question, ‘Cannot redeclare’ is caused because your function is already defined, may be you have the same function defined in two different files, or the same function is defined in two places in the same file.

    I’d suggest you to check if the function exists. If it doesn’t, then define it.

    E.g:

    if (!function_exists(‘name_of_your_function’)){

    function name_of_your_function(){
    //your code
    }

    }

    Hope it helps!

    @juan2machado… thanks.

    Yes, I know the cause of a ‘cannot redeclare’ message is that it’s already been used)… and I’m familiar with these when, for example, I’ve moved a function to a different file but forgotten to delete the original instance.

    But, as noted, the files are identical to ones I use on other sites – and for which no errors are triggered.

    And further, the ‘previously declared’ reference is to the same file!

    For example, if I create a test file (test.php), containing just this code:

    
    <?php
    
    function maintenance_mode()
    {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
    wp_die('<img src="/graphics/header/logo/1.png" />
    <p><strong>Site temporarily offline for maintenance.</strong> </p>' );
    } }
    add_action( 'get_header', 'maintenance_mode' );
    

    It triggers an error of ‘Fatal error: Cannot redeclare maintenance_mode() (previously declared in [path to]/functions/admin/test.php:5) in [path to]/functions/admin/test.php on line 8’. And, when I rename that ‘admin’ folder the error doesn’t appear.

    Moreover… every function in an ‘admin’ sub-folder triggers a ‘cannot redeclare’ error, but not if that folder is renamed. (Again, as noted, I use an identical structure on my other sites without problem.)

    UPDATE: Issue fixed – my error (of course!).

    The cause was that I’d inadvertently not deleted an earlier file which used non-recursive code (with a hard-coded reference to ‘/functions/admin/’) to include sub-folder files.

    • This reply was modified 2 years, 3 months ago by gulliver.
    • This reply was modified 2 years, 3 months ago by gulliver.
    • This reply was modified 2 years, 3 months ago by gulliver.
    • This reply was modified 2 years, 3 months ago by gulliver.

    Hey, @gulliver. That’s awesome!

    Document it for future reference. Just in case 😉

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘What’s the cause of this ‘Cannot redeclare’ error?’ is closed to new replies.