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?

2 s
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *