Support » Developing with WordPress » Pluggable functions – theme to plugin

  • Resolved Andre


    This is my first time creating a plugin (a plugin to add and modify free theme functions). For some reason, when activating the plugin, it keeps displaying that I cannot redeclare the function(s) in question.

    As far as I understand it, the plugin functions should be initialized before the theme (from what I’ve learned online). Here is the first example with a function in my theme:

    if (!function_exists('mytheme_page_post_thumbnail')) :
        function mytheme_page_post_thumbnail() {
        echo '<div class="row"><div class="col">', mytheme_post_thumbnail(), '</div></div>';

    That should check to see if the function is existing or not. This means, the plugin when activated, should load its version (a modified function of the one above) and then skip the theme’s version. The plugin has a slight modification of that function, just without the !function_exists part.

    It seems as none of the function overrides in the plugin for the theme are loading before the theme’s functions–as I keep getting a “cannot redeclare…” message in the admin.

    I was following an online tutorial from MHThemes. Their example for overriding a pluggable function:

    if (!function_exists('my_parent_theme_function')) {
        function my_parent_theme_function() {
            // Code of your pluggable function

    Then the plugin can do:

    function my_parent_theme_function() {
        // Code of your new function in the child theme

    I even tried using priority with add_action…but, still getting cannot redeclare…

    Any ideas on how to solve this? Plus, is the above only for child themes, although I did see online that plugin functions load before?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Andre


    Little update: It appears if the theme is active and then I activate the plugin, I get the cannot redeclare…message. If I activate the plugin first and then the theme, no issues, everything works.

    So it appears, the pluggable functions (kind of) works, but only when activating the plugin first, then the theme. The other way around doesn’t.

    Would love to hear how to solve this one from all you experienced plugin devs so that when the plugin is activated after, I don’t get that problem 🙂

    Moderator bcworkz


    Usually plugins load first, but that’s not always the case. When you activate your plugin, the theme is typically already loaded and active. The solution is both the theme’s declaration and your plugin’s must both declare the if (!function_exists('mytheme_page_post_thumbnail')) : bit so there is no error regardless of which comes first.

    This means during activation, the theme’s function will be active, not yours. This is of no consequence because with the next request, your plugin will load first and your function will take priority over the theme’s.

    Thread Starter Andre


    Major thanks for that. It appears this solution worked!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pluggable functions – theme to plugin’ is closed to new replies.