5

I am trying to build a plugin that will replace one file from the theme. It will be plugin for certain theme that has a file placed in includes folder to the path would be:

wp-content/theme/includes/example-file.php

This example-file.php is loaded in header and footer also via include

I want to make a plugin that would overwrite example-file.php so that theme includes that file from my plugin and not the includes folder from theme.

How is that possible, what would be the best way to do this?

  • file connects what way(get_template_part ?)? Do hooks and filters? – TrubinE Feb 26 '17 at 14:10
  • Is the include file like: wp-content/theme/includes/example-file.php or is it like: wp-content/themes/<your-theme>/includes/example-file.php? Both is workable, but the later makes more sense. – Fayaz Feb 26 '17 at 14:28
  • you need to add the exact line from the theme's template if you want an accurate answer for your case. – majick Feb 26 '17 at 15:04
6

WordPress way (recommended):

In your plugin, use the WordPress theme_file_path filter hook to change the file from the plugin. Use the following CODE in your plugin:

add_filter( 'theme_file_path', 'wpse_258026_modify_theme_include_file', 20, 2 );
function wpse_258026_modify_theme_include_file( $path, $file = '' ) {
    if( 'includes/example-file.php' === $file ) {
        // change path here as required
        return plugin_dir_path( __FILE__ ) . 'includes/example-file.php';
    }
    return $path;
}

Then you may include the file in your theme using the get_theme_file_path function, like this:

include get_theme_file_path( 'includes/example-file.php' );

Here, if the file is matched in the plugin's filter hook, then the plugin version of the file will be included, otherwise the theme version will be included.

Also, this way, even if the plugin is disabled, the theme will work with its own version of the file without any modification.

Controlling Page Template from Plugin:

If you are looking for controlling page templates from your plugin, then you may check out this answer to see how to do it.

PHP way:

If you are including the wp-content/theme/<your-theme>/includes/example-file.php file using simple include call with relative path inside header, footer templates like the following:

include 'includes/example-file.php';

then it's also possible to replace it with the plugin's version of includes/example-file.php file using PHP set_include_path() function.

Generally speaking, to include a file with relative path, PHP looks for the file in the current directory. However, you can manipulate it so that PHP looks for it in another path first. In that case, use the following PHP CODE in your plugin's main PHP file to include the plugin's directory into PHP's default include path:

set_include_path(  plugin_dir_path( __FILE__ ) . PATH_SEPARATOR . get_include_path() );

After that, for any include with relative path in header.php, footer.php etc. PHP will look for the file in your plugin's directory first.

However, this method will not work if you include the file using absolute path in your header, footer etc.

0

Try this one.This works if you are using page templates.

add_filter("page_template", "plugin_function_name");
function plugin_function_name($page_template)
{
   $id = substr($page_template, strrpos($page_template, '/') + 1);
   if ($id == 'includes/example-file.php') {
    $page_template = plugin_dir_path(__FILE__) . 'includes/example-file.php';
   }
   return $page_template;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.