Title: Modify theme function with plugin
Last modified: August 6, 2021

---

# Modify theme function with plugin

 *  [Rough Pixels](https://wordpress.org/support/users/roughpixels/)
 * (@roughpixels)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/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….”

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [Rough Pixels](https://wordpress.org/support/users/roughpixels/)
 * (@roughpixels)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/modify-theme-function-with-plugin/#post-14743072)
 * I think I found the solution(s). I can duplicate the theme’s function into the
   plugin’s function file with the same function name, but insert:
 * `add_action( 'init', 'mytheme_blog_layout', 15 );`
 * …this uses priority and it seems to work. I’m assuming this is a safe way of 
   doing it? I also did a remove_filter() method which also worked and then simply
   reloaded the original function after.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/modify-theme-function-with-plugin/#post-14745013)
 * I don’t know how you got away with re-declaring a function, it shouldn’t be possible
   unless each are wrapped in `if ( ! function_exists( 'function_name' ) ) :` logic,
   making it “pluggable”.
 * While making a function pluggable is perfectly valid, I always cringe at redundant
   code. Using a filter should work. In your default case, instead of echoing, collect
   all output into a variable that will be passed to filter callbacks, along with
   $mytheme_blog_layout. You’d then echo out the return from `apply_filters()`.
 * Plugins can hook this filter and based on $mytheme_blog_layout value, generate
   some other sort of output which gets returned for eventual echoing out.
 *  Thread Starter [Rough Pixels](https://wordpress.org/support/users/roughpixels/)
 * (@roughpixels)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/modify-theme-function-with-plugin/#post-14745025)
 * …actually, I did have:
 * `if ( ! function_exists( 'function_name' ) ) :`
 * I generally do that with most, if not all of my functions.
    As for filters, oddly,
   I couldn’t get the filters to work. Overall, everything is working right now 
   but will set aside time to figure out how to use less code–as you say, redundant
   code.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Modify theme function with plugin’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [Rough Pixels](https://wordpress.org/support/users/roughpixels/)
 * Last activity: [4 years, 10 months ago](https://wordpress.org/support/topic/modify-theme-function-with-plugin/#post-14745025)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
