Pluggable functions – theme to plugin
-
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>'; } endif;
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?
- The topic ‘Pluggable functions – theme to plugin’ is closed to new replies.