0

I'm writing a plugin, most of my code is wrapped in classes, except for an admin_notice fallback function that I call when I am not loading the main plugin class:

class MyClass {
    //Main plugin class
}

if ( $check = true ) {
    $myClass = new MyClass();
} else {
    add_action( 'admin_notices', 'admin_notice' );
} 

function admin_notice() {
    echo "plugin not loaded";
}

this worked before like it should, until suddenly I got an error when I deactivated and reactivated te plugin:

Plugin could not be activated because it triggered a fatal error.

Fatal error: Cannot redeclare admin_notice() (previously declared in myplugin.php:12) in myplugin.php on line 14

The line numbers (12&14) refer to the first and last lines of the function, the file is the same.

the strange thing is that when I wrap the function in a function exists conditional like this:

if ( ! function_exists('admin_notice') ) {
    function admin_notice() {
        echo "plugin not loaded";
    }
}

I get an error that the function doesn't exist!

I have tried deleting & reinstalling the plugin files, renaming the function, but that doesn't help... Any ideas what's going on?

  • It looks to me like you are declaring a variable value inside the if() statement, rather than comparing values. Normally you want the equality operator (==) inside an IF statement, so if ( $check == true ) – Gary D Jan 5 '18 at 3:30
2

Ok, solved my own issue... This might be very specific, but in case anyone ever finds a similar issue:

to catch unexpected output errors I had another function:

add_action('activated_plugin','save_error');
function save_error(){
    $file = "unexpected-output.txt";
    $fh = fopen($file, 'w') or die("can't open file");
    $error = ob_get_contents();
    fclose($fh);
}

there was a typo in the fopen(), which apparently messed up things badly (still not sure why it was trying to load everything more than once - I think it has to do with the fact that the function died).

| improve this answer | |
  • +1 Same here, answering your own question is nearly always worth a vote. – s_ha_dum Apr 5 '13 at 20:06

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