• swinggraphics

    (@swinggraphics)


    My plugin uses if ( ! function_exists(…) ) to avoid naming collisions with another plugin whose functions I want to duplicate only if absent. However, if my plugin is already active when the other plugin is activated, you get the “Cannot redeclare” error. All my code is inside plugins_loaded, but that happens before the activation action (wp-admin/plugins.php?action=activate).

    I tried using

    $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
    $action = $wp_list_table->current_action();

    (from plugins.php) and also WP_Plugins_List_Table::current_action(); but those don’t work.

    Any suggestions, other than disabling my plugin, enabling the other plugin, then reenabling my plugin? (That’s fine for me, but not for the end user.) Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Alan Fuller

    (@alanfuller)

    You say all your code is inside plugins_loaded but what priority?

    Have you tried add_action('plugins_loaded', 'mypluginfeatures', 99999) ?

    Thread Starter swinggraphics

    (@swinggraphics)

    1, 10, 99999, PHP_INT_MAX – it makes no difference.

    Moderator bcworkz

    (@bcworkz)

    That’s all fine when plugins are already activated. The problem is the other plugin has no if ( ! function_exists(…) ) code and during its activation, all other WP loading is said and done with. There’s no way for your code to know there will be an activation and that your code would conflict, so the function will always be declared already, breaking the other’s activation. Unless the other plugin cooperates with similar code, I don’t see a better solution than temporary deactivation of your plugin.

    Alan Fuller

    (@alanfuller)

    avoid naming collisions with another plugin whose functions I want to duplicate only if absent

    Is there another way of thinking about this issue?

    If you are duplicating it ( exactly ) why do you care whether it present or not -just give it a unique name and leave it be.

    Thread Starter swinggraphics

    (@swinggraphics)

    Thanks for the input.

    @bcworkz Is there a way to preempt the error message and display something like, “Please temporarily disable My Plugin and active Other Plugin first”?

    @alanfuller Themes and other plugins use the standard function names.

    This is part of a set of plugins and parent themes that an agency uses for different clients, so the particular plugin and theme combination is not always the same. The one thing we can make consistent is the function names.

    The plugin that does not use if ( ! function_exists(…) ) is like that because its version needs to take priority.

    Moderator bcworkz

    (@bcworkz)

    See https://www.php.net/manual/en/function.set-error-handler.php
    and https://developer.wordpress.org/reference/classes/wp_fatal_error_handler/

    Not a very good UX, as I’m sure you know.

    If the other plugin also used if ( ! function_exists(…) ), you can take measures to ensure the other’s function takes priority like Alan suggested with “plugins_loaded” or similar. The intended priority will fail during activation, but will work again for the very next request.

    Or maybe better, have a common set of functions used by the family of themes and plugins declared in a must-use plugin. These function’s behavior could be modified from other modules by way of filter and action hooks. Which module has hook priority could then be managed by the "add_{filter|action}()" function’s $priority argument.

    • This reply was modified 3 years ago by bcworkz.
    Thread Starter swinggraphics

    (@swinggraphics)

    Thanks all. I decided to rejigger the base plugin to do things on plugins_loaded. They don’t run during activation, but that’s fine for that one page load. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Avoiding previously declared function name error during activation’ is closed to new replies.