Last active
October 5, 2016 03:22
-
-
Save imvaskii/08fe6d53c4a6b6a7ce065d7015af3f4a to your computer and use it in GitHub Desktop.
Wordpress admin custom filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author : Bhaskar K C | |
* generic class for admin posts custom filters **dropdown filter on admin listing pages | |
* Assuming column values are to be stored in post meta | |
*/ | |
if( ! class_exists('TWR_admin_post_filter') ) { | |
class TWR_admin_post_filter { | |
private $post_type; | |
private $filter_options; | |
private $filter_name; | |
function __construct( $post_type, $filter_name, $filter_options ) { | |
if( ! is_admin() ) { | |
return false; | |
} | |
if( empty( $post_type ) || empty( $filter_options ) ) { | |
return true; | |
} | |
global $pagenow; | |
$current_screen_post_type = ( !empty( $_GET['post_type'] )? $_GET['post_type'] : null ); | |
if( $post_type != $current_screen_post_type && 'edit' != $pagenow ) return; | |
$this->post_type = $post_type; | |
$this->filter_name = strtolower( $filter_name ); | |
$this->filter_options = $filter_options; | |
add_action( 'admin_enqueue_scripts', array( &$this, 'twr_admin_filter_enqueue_script' ) ); | |
add_action( 'restrict_manage_posts', array ( &$this, 'twr_custom_admin_posts_filter' ) ); | |
add_action( 'pre_get_posts', array( &$this, 'twr_custom_pre_get_posts' ) ); | |
} | |
function twr_admin_filter_enqueue_script() { | |
// https://codex.wordpress.org/Function_Reference/wp_script_is | |
if( wp_script_is( 'select2', 'enqueued' ) ) return; | |
wp_enqueue_script( 'select2', trailingslashit( get_stylesheet_directory_uri() ) . 'admin-helper/script/inc/select2/select2.min.js', array('jquery') ); | |
wp_enqueue_style( 'select2', trailingslashit( get_stylesheet_directory_uri() ) . 'admin-helper/script/inc/select2/select2.css' ); | |
wp_localize_script( 'select2', 'select2_loaded', 'loaded' ); | |
} | |
function twr_custom_admin_posts_filter () { | |
if( empty( $this->filter_options ) && $this->post_type ) return; | |
$filter_label = ucfirst( $this->filter_name ); | |
printf( "<label class='screen-reader-text' for='%s'> %s </label>", $this->filter_name, "Filter by {$filter_label}" ); | |
printf( "<select name='%s' id='%s' class='twr_admin_filter'>", $this->filter_name, $filter_label ); | |
foreach( $this->filter_options as $option_key => $option_title ) { | |
printf( "<option name='%s'>%s</option>", wp_strip_all_tags( $option_key ), wp_strip_all_tags( $option_title ) ); | |
} | |
printf( '</select>' ); | |
} | |
function twr_custom_pre_get_posts( $query ) { | |
global $pagenow; | |
if( is_admin() && 'edit' == $pagenow && ! empty( $_GET[$this->filter_name] ) ) { | |
$meta_query_args = array( | |
'relation' => 'AND', | |
array( | |
'key' => $this->filter_name, | |
'value' => $_GET[$this->filter_name], | |
'compare' => '=' | |
) | |
); | |
$query->set( 'meta_query', $meta_query_args ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment