Modify theme function with plugin
-
Working on a new theme but also a plugin that adds more functions to it. The theme has a function using a switch/case for blog layouts, but I want to have a function in the plugin that modifies it with additional cases. What is the best way to do this from the plugin’s own functions file?
Right now, the theme function is:
function mytheme_blog_layout(){ if ( have_posts() ) : $mytheme_blog_layout = get_theme_mod( 'mytheme_blog_layout', 'classic' ); switch ( esc_attr($mytheme_blog_layout ) ) { case "center": // center blog echo '<div class="col">'; while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content/content' ); endwhile; echo '</div>'; break; default: // classic blog echo '<div class="col-lg-8">'; while ( have_posts() ) : the_post(); get_template_part( 'template-parts/content/content' ); endwhile; echo '</div>'; echo '<aside id="blog-sidebar" class="col-lg-4 widget-area sidebar-right">'; get_sidebar(); echo '</aside>'; } } endif;
Basically, I need the function file in the plugin to modify that theme function with additional cases. I thought a filter would work, but it didn’t. What oddly does work is that I simply copied that over into the plugin’s function file and it worked–but don’t believe that is the correct method, despite that it works. It means the “same” function is in the theme and plugin, but doesn’t seem to error with “cannot redeclare….”
- The topic ‘Modify theme function with plugin’ is closed to new replies.