Guest User

Untitled

a guest
Aug 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. <?php
  2. /**
  3. * Show Page Templates
  4. *
  5. * @package show-page-templates
  6. * @author ryanshoover
  7. * @license Proprietary
  8. *
  9. * @wordpress-plugin
  10. * Plugin Name: Show Page Templates
  11. * Plugin URI: wordpress.org
  12. * Description: Shows page templates filterable
  13. * Version: 0.1.0
  14. * Author: ryanshoover
  15. * Author URI: ryan.hoover.ws
  16. * Text Domain: show-page-templates
  17. * License: Proprietary
  18. */
  19.  
  20. namespace ShowPageTemplates;
  21.  
  22. function register( $columns ) {
  23. $columns[ 'page-template'] = __( 'Template', 'show-page-templates' );
  24.  
  25. return $columns;
  26. }
  27.  
  28. add_filter( 'manage_page_posts_columns', __NAMESPACE__ . '\register' );
  29.  
  30. function sortable( $columns ) {
  31. $columns['page-template'] = 'page-template';
  32.  
  33. return $columns;
  34. }
  35.  
  36. add_filter( 'manage_edit-page_sortable_columns', __NAMESPACE__ . '\sortable' );
  37.  
  38. function filter_dropdown(){
  39. if ( empty( $_GET['post_type'] ) || 'page' !== $_GET['post_type'] ) {
  40. return;
  41. }
  42.  
  43. $templates = array_flip( get_page_templates( null, 'page' ) );
  44.  
  45. $selected_value = ! empty( $_GET['page-template'] ) ? esc_attr( $_GET['page-template'] ) : '';
  46.  
  47. ?>
  48. <select name="page-template">
  49. <option value="">All Templates</option>
  50. <?php
  51. foreach ( $templates as $value => $name ) :
  52. ?>
  53. <option
  54. value="<?php esc_attr_e( $value ); ?>"
  55. <?php selected( $value, $selected_value, true ); ?>
  56. >
  57. <?php esc_html_e( $name ); ?>
  58. </option>
  59. <?php
  60. endforeach;
  61. ?>
  62. </select>
  63. <?php
  64. }
  65.  
  66. add_action( 'restrict_manage_posts', __NAMESPACE__ . '\filter_dropdown' );
  67.  
  68. function content( $column, $post_id ) {
  69. static $templates;
  70.  
  71. $templates = $templates ?: array_flip( get_page_templates( null, 'page' ) );
  72.  
  73. if ( 'page-template' === $column ) {
  74. $template = get_page_template_slug( $post_id );
  75.  
  76. $template_name = ! empty( $templates[ $template ] ) ? $templates[ $template ] : 'Default';
  77.  
  78. esc_html_e( $template_name, 'show-page-templates' );
  79. }
  80.  
  81. return $column;
  82. }
  83.  
  84. add_filter( 'manage_pages_custom_column', __NAMESPACE__ . '\content', 10, 2 );
  85.  
  86. function get_posts( $query ) {
  87. if ( ! is_admin() ) {
  88. return;
  89. }
  90.  
  91. if ( 'page-template' == $query->get( 'orderby' ) ) {
  92. $query->set( 'meta_key', '_wp_page_template' );
  93. $query->set( 'orderby', 'meta_value' );
  94. }
  95.  
  96. if ( ! empty( $_GET['page-template'] ) ) {
  97. $meta_query = $query->get( 'meta_query' );
  98.  
  99. $meta_query = $meta_query ?: [];
  100.  
  101. $meta_query[] = [
  102. 'key' => '_wp_page_template',
  103. 'value' => esc_attr( $_GET['page-template'] ),
  104. ];
  105.  
  106. $query->set( 'meta_query', $meta_query );
  107. }
  108. }
  109.  
  110. add_action( 'pre_get_posts', __NAMESPACE__ . '\get_posts' );
Add Comment
Please, Sign In to add comment