Wordpress: how to create a template that is still available when you switch theme?

I have a custom template for my portfolio page. However, after switching theme, it's gone because it is in my previous theme's folder.

Technically, I can copy that file into my new theme folder. However, I plan to change theme every two weeks and this becomes non-trivial. Is there a way to always have a bunch of common template files available to me no matter when and how often I switch theme? (In other words, I want to create template files that are not dependent on themes.)

Thanks!

Solutions

There is, using template_redirect, which you would put in the functions.php file.

function uniquename_default_template() {

global $wpdb;

if(get_post_type() == 'posttype') : /* You could use is_single() instead of get_post_type() == '' or any type of conditional tag) */

include(TEMPLATEDIR . 'path/to/theme/file.php'); /* You could use TEMPLATEDIR to get a file from a template folder, or PLUGINDIR to get a file from the plugins directory - doesn't support HTTP requests */

exit; endif;

}

add_action('template_redirect', 'uniquename_default_template');

Hope it helps.

Similar questions

How do you seamlessly switch hosting?
I recently switched a WP site from shared hosting to a VPS and would like to document the process here.
Is there a hook for when you switch themes?
I created a plugin but I just came across a bug that I don't really know how to solve. When you activate my plugin, it creates a file in the active theme directory and when you deactivate it, it deletes that file. The problem is, if you activate the plugin, and then switch the theme, the files won't exist in the new theme's directory. Is there a li...
Can't switch to a child theme using filters template, option_template and option_stylesheet
In order to dynamically switch a theme in functions file (e.g. when wanting a different theme for a different domain, but same website), you use the following: This does not seem to work when wanting to call a child theme. I get a white/blank page when calling a child theme, I have tried a few. Is there some other code to switch to a child theme?
Wordpress template deleted, but still working and showing on template selection
I have a template front-page.php in my theme. Its name is "Front Page". My problem is that when I commit changes in this file. It wont reflect on the front end. This happened just yesterday and I don't know what's going on.. I tried deleting this template but, my page that uses this template still uses this (deleted) template. When I edit the page,...
How do you unblock or access Plugins, Tools and Settings in the WordPress WP-Admin when they are not available?
I have paid someone to create a dynamic classified website for me. The website was created in WordPress. I was given administrator access to the website, however I am not able to access the Plugins, Tools and Settings on the WP-Admin Dashboard. I will be needing those to be available for functionality of the website. What I see I expected to see to...
Is it possible to create categories that are available in a Wordpress theme by default?
Is there a way of having categories available right after installing a theme. For instance, if a theme post content to a loop which retrieves posts with the category "Tagline". Is there a way of having the category "Tagline" by default (in the theme)? (So the end user wouldn't have to create it himself/herself)

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.