Custom Post Template - Version 1.2

Version Description

Download this release

Release Info

Developer simonwheatley
Plugin Icon wp plugin Custom Post Template
Version 1.2
Comparing to
See all releases

Code changes from version 1.1 to 1.2

Files changed (3) hide show
  1. custom-post-templates.php +39 -8
  2. readme.txt +8 -3
  3. template-tags.php +59 -0
custom-post-templates.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Post Templates
4
  Plugin URI: http://wordpress.org/extend/plugins/custom-post-template/
5
  Description: Provides a drop-down to select different templates for posts from the post edit screen. The templates are defined similarly to page templates, and will replace single.php for the specified post.
6
  Author: Simon Wheatley
7
- Version: 1.1
8
  Author URI: http://simonwheatley.co.uk/wordpress/
9
  */
10
 
@@ -27,6 +27,7 @@ Author URI: http://simonwheatley.co.uk/wordpress/
27
  */
28
 
29
  require_once( dirname (__FILE__) . '/plugin.php' );
 
30
 
31
  /**
32
  *
@@ -47,12 +48,33 @@ class CustomPostTemplates extends CustomPostTemplates_Plugin
47
  $this->add_meta_box( 'select_post_template', __('Post Template'), 'select_post_template', 'post', 'side', 'default' );
48
  $this->add_action( 'save_post' );
49
  $this->add_filter( 'single_template', 'filter_single_template' );
 
50
  }
51
 
52
  /*
53
  * FILTERS & ACTIONS
54
  * *******************
55
  */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  public function select_post_template( $post )
58
  {
@@ -117,22 +139,31 @@ class CustomPostTemplates extends CustomPostTemplates_Plugin
117
  $theme = get_current_theme();
118
  $templates = $themes[ $theme ][ 'Template Files' ];
119
 
120
- $page_templates = array();
121
 
122
  if ( is_array( $templates ) ) {
 
 
123
  foreach ( $templates as $template ) {
124
- // Get the file data and collapse it into a single string
125
- $template_data = implode( '', file( $template ) );
126
- if ( ! preg_match( '|Template Name Posts:(.*)$|mi', $template_data, $name ) )
 
127
  continue;
128
 
129
- $name = _cleanup_header_comment( $name[ 1 ] );
 
 
 
 
 
130
 
131
- $page_templates[ trim( $name ) ] = basename( $template );
 
132
  }
133
  }
134
 
135
- return $page_templates;
136
  }
137
  }
138
 
4
  Plugin URI: http://wordpress.org/extend/plugins/custom-post-template/
5
  Description: Provides a drop-down to select different templates for posts from the post edit screen. The templates are defined similarly to page templates, and will replace single.php for the specified post.
6
  Author: Simon Wheatley
7
+ Version: 1.2
8
  Author URI: http://simonwheatley.co.uk/wordpress/
9
  */
10
 
27
  */
28
 
29
  require_once( dirname (__FILE__) . '/plugin.php' );
30
+ require_once( dirname (__FILE__) . '/template-tags.php' );
31
 
32
  /**
33
  *
48
  $this->add_meta_box( 'select_post_template', __('Post Template'), 'select_post_template', 'post', 'side', 'default' );
49
  $this->add_action( 'save_post' );
50
  $this->add_filter( 'single_template', 'filter_single_template' );
51
+ $this->add_filter( 'body_class' );
52
  }
53
 
54
  /*
55
  * FILTERS & ACTIONS
56
  * *******************
57
  */
58
+
59
+ /**
60
+ * Hooks the WP body_class function to add a class to single posts using a post template.
61
+ *
62
+ * @param array $classes An array of strings
63
+ * @return array An array of strings
64
+ * @author Simon Wheatley
65
+ **/
66
+ public function body_class( $classes ) {
67
+ if ( ! is_post_template() )
68
+ return $classes;
69
+ global $wp_query;
70
+ // We distrust the global $post object, as it can be substituted in any
71
+ // number of different ways.
72
+ $post = $wp_query->get_queried_object();
73
+ $post_template = get_post_meta( $post->ID, 'custom_post_template', true );
74
+ $classes[] = 'post-template';
75
+ $classes[] = 'post-template-' . str_replace( '.php', '-php', $post_template );
76
+ return $classes;
77
+ }
78
 
79
  public function select_post_template( $post )
80
  {
139
  $theme = get_current_theme();
140
  $templates = $themes[ $theme ][ 'Template Files' ];
141
 
142
+ $post_templates = array();
143
 
144
  if ( is_array( $templates ) ) {
145
+ $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
146
+
147
  foreach ( $templates as $template ) {
148
+ $basename = str_replace($base, '', $template);
149
+
150
+ // don't allow template files in subdirectories
151
+ if ( false !== strpos($basename, '/') )
152
  continue;
153
 
154
+ // Get the file data and collapse it into a single string
155
+ $template_data = implode( '', file( $template ));
156
+
157
+ $name = '';
158
+ if ( preg_match( '|Template Name Posts:(.*)$|mi', $template_data, $name ) )
159
+ $name = _cleanup_header_comment( $name[1] );
160
 
161
+ if ( !empty( $name ) )
162
+ $post_templates[trim( $name )] = $basename;
163
  }
164
  }
165
 
166
+ return $post_templates;
167
  }
168
  }
169
 
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: simonwheatley
3
  Donate link: http://www.simonwheatley.co.uk/wordpress/
4
  Tags: post, template, theme
5
  Requires at least: 2.9
6
- Tested up to: 2.9.1
7
- Stable tag: 1.1
8
 
9
  Provides a drop-down to select different templates for posts from the post edit screen. The templates replace single.php for the specified post.
10
 
@@ -12,7 +12,7 @@ Provides a drop-down to select different templates for posts from the post edit
12
 
13
  **This plugin requires PHP5 (see Other Notes > PHP4 for more).**
14
 
15
- Provides a drop-down to select different templates for posts from the post edit screen. The templates are defined similarly to page templates, and will replace single.php for the specified post.
16
 
17
  Post templates, as far as this plugin is concerned, are configured similarly to [page templates](http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates) in that they have a particular style of PHP comment at the top of them. Each post template must contain the following, or similar, at the top:
18
  <code>
@@ -52,6 +52,11 @@ Right, that's it. Grump over. ;)
52
 
53
  == Change Log ==
54
 
 
 
 
 
 
55
  = v1.1 2010/01/27 =
56
 
57
  * IDIOTFIX: Managed to revert to an old version somehow, this version should fix that.
3
  Donate link: http://www.simonwheatley.co.uk/wordpress/
4
  Tags: post, template, theme
5
  Requires at least: 2.9
6
+ Tested up to: 2.9.2
7
+ Stable tag: 1.2
8
 
9
  Provides a drop-down to select different templates for posts from the post edit screen. The templates replace single.php for the specified post.
10
 
12
 
13
  **This plugin requires PHP5 (see Other Notes > PHP4 for more).**
14
 
15
+ Provides a drop-down to select different templates for posts from the post edit screen. The templates are defined similarly to page templates, and will replace single.php for the specified post. This plugin will NOT switch the templates for the different posts in a listing page, e.g. a date or category archive, it will only affect the template used for single posts (i.e. you can choose a template which is not single.php).
16
 
17
  Post templates, as far as this plugin is concerned, are configured similarly to [page templates](http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates) in that they have a particular style of PHP comment at the top of them. Each post template must contain the following, or similar, at the top:
18
  <code>
52
 
53
  == Change Log ==
54
 
55
+ = v1.2 2010/04/28 =
56
+
57
+ * ENHANCEMENT: Now sporting a conditional `is_post_template` function/template tag which is functionally equivalent to the core WordPress [is_page_template](http://codex.wordpress.org/Function_Reference/is_page_template) conditional function/template tag
58
+ * ENHANCEMENT: If the theme uses the core WordPress (body_class)[http://codex.wordpress.org/Template_Tags/body_class] template tag, then you will have two new classes added: "post-template" and "post-template-my-post-template-php" (where your post template file is named "my-post-template.php").
59
+
60
  = v1.1 2010/01/27 =
61
 
62
  * IDIOTFIX: Managed to revert to an old version somehow, this version should fix that.
template-tags.php ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /* Copyright 2010 Simon Wheatley
4
+
5
+ This program is free software; you can redistribute it and/or modify
6
+ it under the terms of the GNU General Public License as published by
7
+ the Free Software Foundation; either version 2 of the License, or
8
+ (at your option) any later version.
9
+
10
+ This program is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU General Public License for more details.
14
+
15
+ You should have received a copy of the GNU General Public License
16
+ along with this program; if not, write to the Free Software
17
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+
19
+ */
20
+
21
+
22
+ /**
23
+ * Whether currently in a post template.
24
+ *
25
+ * This template tag allows you to determine whether or not you are in a post
26
+ * template. You can optionally provide a template name and then the check will be
27
+ * specific to that template.
28
+ *
29
+ * @since 1.3
30
+ * @uses $wp_query
31
+ *
32
+ * @param string $template A template file name (not complete file path), if specific matching is required.
33
+ * @return bool False on failure, true if success.
34
+ */
35
+ function is_post_template($template = '') {
36
+ if (!is_single()) {
37
+ return false;
38
+ }
39
+
40
+ global $wp_query;
41
+
42
+ $post = $wp_query->get_queried_object();
43
+ $post_template = get_post_meta( $post->ID, 'custom_post_template', true );
44
+
45
+ // We have no argument passed so just see if a page_template has been specified
46
+ if ( empty( $template ) ) {
47
+ if (!empty( $post_template ) ) {
48
+ return true;
49
+ }
50
+ } elseif ( $template == $post_template) {
51
+ return true;
52
+ }
53
+
54
+ return false;
55
+ }
56
+
57
+
58
+
59
+ ?>