Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Last active December 14, 2017 09:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thefuxia/f657e323a60f12ad9605 to your computer and use it in GitHub Desktop.
Save thefuxia/f657e323a60f12ad9605 to your computer and use it in GitHub Desktop.
T5 Page Template Column. Shows the page templates as separate, sortable column.
<?php
/**
* Plugin Name: T5 Page Template Column
* Plugin URI: https://gist.github.com/toscho/f657e323a60f12ad9605
* Description: Shows the page templates as separate, sortable column.
* Version: 2015.02.14
* Required: 4.0
* Author: Thomas Scholz <info@toscho.de>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*/
if ( ! is_admin() || empty ( $_GET[ 'post_type' ] ) || 'page' !== $_GET[ 'post_type' ] )
return;
class T5_Page_Template_Column {
private $column = 'page_template';
private $meta_key = '_wp_page_template';
public function setup() {
add_filter(
'manage_page_posts_columns',
array( $this, 'add_header' )
);
add_filter(
'manage_edit-page_sortable_columns',
array( $this, 'add_sortable_column' )
);
add_filter(
'manage_page_posts_custom_column',
array( $this, 'render' ),
10,
2
);
add_action(
'pre_get_posts',
array( $this, 'filter_pre_get_posts' )
);
// You can change or remove hooks from this class here.
do_action( 't5_page_template_column_setup', $this );
}
public function filter_pre_get_posts( WP_Query $wp_query ) {
if ( 'template' === $wp_query->get( 'orderby' ) ) {
$wp_query->set( 'meta_key', $this->meta_key );
$wp_query->set( 'orderby', 'meta_value' );
}
if ( ! empty ( $_GET[ 'template' ] ) ) {
$wp_query->set( 'meta_key', $this->meta_key );
$wp_query->set( 'meta_value', $_GET[ 'template' ] );
}
}
public function add_header( $columns ) {
$columns[ $this->column ] = 'Template';
return $columns;
}
public function add_sortable_column( $columns ) {
$columns[ $this->column ] = 'template';
return $columns;
}
public function render( $column, $post_id ) {
if ( $column !== $this->column )
return;
// $template is the path to the template file relative to the theme root.
$template = get_post_meta( $post_id, $this->meta_key, TRUE );
$name = $this->get_template_name( $post_id, $template );
$url = $this->get_selection_url( $template );
print "<a href='$url'>$name</a>";
if ( 'default' !== $template ) {
$template = esc_html( $template );
print "<br><code>$template</code>";
}
}
private function get_template_name( $post_id, $template ) {
if ( 'default' === $template )
return __( 'Default Template' );
$post = get_post( $post_id );
$all_templates = get_page_templates( $post );
$template = esc_html( $template );
$name = array_search( $template, $all_templates );
if ( $name )
return esc_html( $name );
return esc_html__( 'Default Template' );
}
private function get_selection_url( $template ) {
return add_query_arg( 'template', $template );
}
}
$t5_ptc = new T5_Page_Template_Column;
add_action( 'load-edit.php', array( $t5_ptc, 'setup' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment