WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to make a page template available to multiple or all themes without having to save the page template in every single theme folder?

Maybe something like: themes/globalform.php instead of doing this for each theme themes/twentyten/globalform.php

share|improve this question
    
I think I found what I was looking for: codex.wordpress.org/… but I've tried putting the page-131.php in pretty much every directory without it working. – Raymond Jul 2 '12 at 18:48

I believe you have to make a plugin for that. The following code is based on this answer: Is it possible to define a template for a custom post type within a plugin independent of the active theme?

/*
Plugin Name: Universal Template
Plugin URI: http://wordpress.stackexchange.com/questions/57211
Description: Uses a custom template in the plugin directory accordding to Conditional Tags (http://codex.wordpress.org/Conditional_Tags), maybe even other conditions
Version: 1.0
Author: brasofilo
Author URI: http://wordpress.stackexchange.com/users/12615
*/

class Universal_Template
{

    public function __construct()
    {       
        $this->url = plugins_url( '', __FILE__ );   
        $this->path = plugin_dir_path( __FILE__ );
        add_action( 'init', array( $this, 'init' ) );
   }

    public function init() 
    {
        add_filter( 'template_include', array( $this, 'wpse_57211_my_plugin_templates' ) );
    }


    public function wpse_57211_my_plugin_templates( $template ) 
    {
        $post_types = array( 'post' );

        if ( is_post_type_archive( $post_types ) )
            $template = $this->path . '/single-custom.php';

        if ( is_singular( $post_types ) )
            $template = $this->path . '/single-other-custom.php';

        return $template;
    }

}

$wpse_57211 = new Universal_Template();
share|improve this answer

You could put files in wordpress/wp-content/themes and then include them from various themes. You could even make a folder named wordpress/wp-content/themes/global-includes or shared.

For me, including a file from the parent directory I couldn't just do:

<?php include_once "../globalform.php"; ?>

Instead I needed to do:

<?php include_once get_template_directory() . "/../globalform.php"; ?>

You could also use get_stylesheet_directory() (gets child theme directory) instead of get_template_directory() (will get parent theme directory if the current theme is a child theme. Since we're getting the parent directory it probably doesn't matter here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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