Add Archive Page Template via Plugin

WordPress has a template_include filter which you can use.

// Add a filter to 'template_include' hook
add_filter( 'template_include', 'wpse_force_template' );
function wpse_force_template( $template ) {
    // If the current url is an archive of any kind
    if( is_archive() ) {
        // Set this to the template file inside your plugin folder
        $template = WP_PLUGIN_DIR ."https://wordpress.stackexchange.com/". plugin_basename( dirname(__FILE__) ) .'/archive.php';
    }
    // Always return, even if we didn't change anything
    return $template;
}

It would be wise to allow your users to decide which archives are affected. Many sites have custom post types and may not want those archives affected like other post types. You can add an options page that dynamically pulls all post types on the current site and allows them to toggle your plugin on or off for each. Once you have that, you’ll need to again dynamically pull all post types, and then dynamically build “if” statements for each one. For example, if(is_archive('event') && $eventtoggle == true – if it’s an archive of this specific type and the user toggled the plugin on for this specific type.

Leave a Comment