4

I'm working with a plugin that declares a function using this format:

if (!function_exists('ABdevDND_get_current_post_type')){
    function ABdevDND_get_current_post_type() {
       ...
    }
}

I'm trying to declare it in my theme's functions.php file, by just using:

function ABdevDND_get_current_post_type() {
    ...
}

But my timing is wrong, and I'm getting Fatal error: Cannot redeclare ABdevDND_get_current_post_type().

How and where can I declare this function so it's declared before the plugin?

2 Answers 2

6

You cannot do that in a theme, because themes are loaded after plugins.

Use a custom plugin, maybe a mu-plugin to be sure, because they are loaded earlier than normal plugins.

1

On the inverse here, I recently had the issue of a theme overriding a plugin function on plugin activation. The theme code is loaded before the plugin is activated so as to buffer the any fatal errors. You will see the previously declared error even though the chain of actions is still working properly after activation. You can use this to disable your function override for the plugins page where you most likely will never need it.

It would be great for WordPress to attempt activation of the plugin before plugins_loaded, but that is not the case here.

global $pagenow;
if ( $pagenow !== 'plugins.php' )
{
    //declare your function here
}

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.