Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

(Moderators note: Title was originally "How can I add the "Page Attributes" and/or "Page Attributes > Template" selector to POSTS editor")

WP currently only allows the assignment of a "template" to Pages (i.e. post_type=='page'.) I'd like to extend this functionality to Posts as well (i.e. post_type=='post'.)

How can I add the "Page Attributes" meta box and more specifically, the template switcher to the posts editor?

I'm assuming this is code I will place in my functions.php for my theme.

UPDATE: I've managed to add the hardcoded templates pulldown menu to my post editor, by simply adding the select box html to my existing custom meta options box. Here's the code I'm using for that...

add_meta_box('categorydiv2', __('Post Options'), 'post_categories_meta_box_modified', 'post', 'side', 'high');

And here's the function that writes out the options and the template select box...

//adds the custom categories box
function post_categories_meta_box_modified() {
    global $post;
    if( get_post_meta($post->ID, '_noindex', true) ) $noindexChecked = " checked='checked'";
    if( get_post_meta($post->ID, '_nofollow', true) ) $nofollowChecked = " checked='checked'";
?>
<div id="categories-all" class="ui-tabs-panel">
    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
        <li id='noIndex' class="popular-category"><label class="selectit"><input value="noIndex" type="checkbox" name="chk_noIndex" id="chk_noIndex"<?php echo $noindexChecked ?> /> noindex</label></li> 
        <li id='noFollow' class="popular-category"><label class="selectit"><input value="noFollow" type="checkbox" name="chk_noFollow" id="chk_noFollow"<?php echo $nofollowChecked ?> /> nofollow</label></li>
    </ul>

    <p><strong>Template</strong></p> 
    <label class="screen-reader-text" for="page_template">Post Template</label><select name="page_template" id="page_template"> 
    <option value='default'>Default Template</option> 
    <option value='template-wide.php' >No Sidebar</option>
    <option value='template-salespage.php' >Salespage</option>
    </select>
</div>
<?php
}

And finally, the code to capture the selected values on save...

function save_post_categories_meta($post_id) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
    $noIndex = $_POST['chk_noIndex'];
    $noFollow = $_POST['chk_noFollow'];
    update_post_meta( $post_id, '_noindex', $noIndex );
    update_post_meta( $post_id, '_nofollow', $noFollow );
    return $post_id;
}

Now, I believe all that's left is (1) capturing the selected template and adding it to the post meta for this post and (2) modifying index.php and single.php so that it uses the chosen template.

share|improve this question
    
@Scott B: Well I wrote my whole answer before I saw that you were working on it too, and it seems you've taken it in a somewhat different direction that your original question with your no follow and no index options. Hopefully what I did still has value for you. If not, maybe it will help others. –  MikeSchinkel Oct 10 '10 at 23:14
    
Yep, you've answered the question. I did take a bit different tack once I realized I didn't need to parse the directory and could hardcode the values for my specific templates. I'll still likely need to borrow some of your code for actually getting WP to use the correct assigned template for the post. –  Scott B Oct 11 '10 at 16:36

2 Answers 2

up vote 8 down vote accepted

Hi Scott B:

Hate to be the bearer of bad news but WordPress hardcodes the Page Template functionality to the "page" post type, at least in v3.0 (that might change in future versions but there's not a specific initiative I'm aware of to change it yet. So this is one of the very few times I'm struggling to figure out how to get around something without hacking core.)

The solution I've come up with is to basically copy the relevant code from WordPress core and modify it to our needs. Here are the steps (the line numbers are from v3.0.1):

  1. Copy the page_attributes_meta_box() function from line 535 of /wp-admin/includes/meta-boxes.php and modify to suit.

  2. Code an add_meta_boxes hook to add the metabox created in #1.

  3. Copy the get_page_templates() function from line 166 of /wp-admin/includes/theme.php and modify to suit.

  4. Copy the page_template_dropdown() function from line 2550 of /wp-admin/includes/template.php and modify to suit.

  5. Add a Post Template to your theme.

  6. Code a save_post hook to enable storing of the post template file name upon save.

  7. Code a single_template hook to enable loading of the post template for the associated posts.

Now on with it!


1. Copy the page_attributes_meta_box() function

As our first step you need to copy the page_attributes_meta_box() function from line 535 of /wp-admin/includes/meta-boxes.php and I've chosen to rename it post_template_meta_box(). Since you only asked for page templates I omitted the code for specifying a parent post and for specifying the order which makes the code much simpler. I also chose to use postmeta for this rather than try to reuse the page_template object property in order to avoid and potential incompatibilities caused by unintentional coupling. So here's the code:

function post_template_meta_box($post) {
  if ( 'post' == $post->post_type && 0 != count( get_post_templates() ) ) {
    $template = get_post_meta($post->ID,'_post_template',true);
    ?>
<label class="screen-reader-text" for="post_template"><?php _e('Post Template') ?></label><select name="post_template" id="post_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php post_template_dropdown($template); ?>
</select>
<?php
  } ?>
<?php
}

2. Code an add_meta_boxes hook

Next step is to add the metabox using the add_meta_boxes hook:

add_action('add_meta_boxes','add_post_template_metabox');
function add_post_template_metabox() {
    add_meta_box('postparentdiv', __('Post Template'), 'post_template_meta_box', 'post', 'side', 'core');
}

3. Copy the get_page_templates() function

I assumed it would only make sense to differentiate between page templates and post template thus the need for a get_post_templates() function based on get_page_templates() from line 166 of /wp-admin/includes/theme.php. But instead of using the Template Name: marker which page templates use this function uses a Post Template: marker instead which you can see below.

I also filtered out inspection of functions.php (not sure how get_page_templates() ever worked correctly without that, but whatever!) And the only thing left is to change references to the word page to post for maintenance readability down the road:

function get_post_templates() {
  $themes = get_themes();
  $theme = get_current_theme();
  $templates = $themes[$theme]['Template Files'];
  $post_templates = array();

  if ( is_array( $templates ) ) {
    $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );

    foreach ( $templates as $template ) {
      $basename = str_replace($base, '', $template);
      if ($basename != 'functions.php') {
        // don't allow template files in subdirectories
        if ( false !== strpos($basename, '/') )
          continue;

        $template_data = implode( '', file( $template ));

        $name = '';
        if ( preg_match( '|Post Template:(.*)$|mi', $template_data, $name ) )
          $name = _cleanup_header_comment($name[1]);

        if ( !empty( $name ) ) {
          $post_templates[trim( $name )] = $basename;
        }
      }
    }
  }

  return $post_templates;
}

4. Copy the page_template_dropdown() function

Similarly copy page_template_dropdown() from line 2550 of /wp-admin/includes/template.php to create post_template_dropdown() and simply change it to call get_post_templates() instead:

function post_template_dropdown( $default = '' ) {
  $templates = get_post_templates();
  ksort( $templates );
  foreach (array_keys( $templates ) as $template )
    : if ( $default == $templates[$template] )
      $selected = " selected='selected'";
    else
      $selected = '';
  echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
  endforeach;
}

5. Add a Post Template

Next step is to add a post template for testing. Using the Post Template: marker mentioned in step #3 copy single.php from your theme to single-test.php and add the following comment header (be sure to modify something in single-test.php so you can tell it is loading instead of single.php):

/**
 * Post Template: My Test Template
 */

Once you've done steps #1 thru #5 you can see your "Post Templates" metabox appear on your post editor page:

What a Post Templates Metabox looked like when added to WordPress 3.0

6. Code a save_post hook

Now that you have the editor squared away you need to actually save your page template file name to postmeta when the user clicks "Publish". Here's the code for that:

add_action('save_post','save_post_template',10,2);
function save_post_template($post_id,$post) {
  if ($post->post_type=='post' && !empty($_POST['post_template']))
    update_post_meta($post->ID,'_post_template',$_POST['post_template']);
}

7. Code a single_template hook

And lastly you need to actually get WordPress to use your new post templates. You do that by hooking single_template and returning your desired template name for those posts that have had one assigned:

add_filter('single_template','get_post_template_for_template_loader');
function get_post_template_for_template_loader($template) {
  global $wp_query;
  $post = $wp_query->get_queried_object();
  if ($post) {
    $post_template = get_post_meta($post->ID,'_post_template',true);
    if (!empty($post_template) && $post_template!='default')
      $template = get_stylesheet_directory() . "/{$post_template}";
  }
  return $template;
}

And that's about it!

NOTE that I did not take into consideration Custom Post Types, only post_type=='post'. In my opinion addressing custom post types would require differentiating between the different post types and, while not overly difficult, I didn't attempt that here.

share|improve this answer
    
Great! I went to sleep with an almost complete code in my editor with the same approach of copying the default WordPress functions (it was complete, but I would not post it since I didn't test). :) –  sorich87 Oct 11 '10 at 7:57
    
@sorich87 - You know the old saying, "You snooze, you loose!" Seriously though, ONLY kidding. There's really only one reasonable way to make it work so it's not wonder your code would be the same! –  MikeSchinkel Oct 11 '10 at 9:08
    
Mike, you continue to amaze. Thanks very much for taking the time to hammer this out. –  Scott B Oct 11 '10 at 16:33
    
@sorich87 - Thanks for working on it. I really appreciate the effort. –  Scott B Oct 11 '10 at 16:34
    
@MikeSchinkel: :) it was almost exactly the same. @Scott B: you are welcome! –  sorich87 Oct 11 '10 at 16:47

Wordpress allows you to add Meta to Categories using a plugin:

To do this you need to add one of the various extensions that adds meta to categories (mimicking what pages get out of the box), Simple Term Meta does the job nicely.

N.B. WordPress 3.x is needed for extending Categories.

After that you can use:

  • add_term_meta
  • update_term_meta
  • get_term_meta

Use Functions.php to add methods to do what you want e.g.

add_action('category_add_form_fields', 'category_metabox_add', 10, 1);

function category_metabox_add($tag) { ?>
    <div class="form-field">
        <label for="image-url"><?php _e('Image URL') ?></label>
        <input name="image-url" id="image-url" type="text" value="" size="40" aria-required="true" />
        <p class="description"><?php _e('This image will be the thumbnail shown on the category page.'); ?></p>
    </div>
<?php } 

add_action('created_category', 'save_category_metadata', 10, 1);

function save_category_metadata($term_id)
{
    if (isset($_POST['image-url'])) 
        update_term_meta( $term_id, 'image-url', $_POST['image-url']);                  
}

Calling new fields in themes is easy:

<?php echo get_term_meta(get_query_var('cat'), 'image-url', true); ?>

More details and examples: http://www.wphub.com/adding-metadata-taxonomy-terms/

share|improve this answer

protected by toscho Aug 23 at 16:32

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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