How to Create filter in wordpress admin
Wordpress allows to add or modify the filters to all post type listing (default or custom). We will add custom author filter to posts list in wordpress dashboard.
Please copy the following code and paste in your theme functions.php or plugin file. I preferred only create a new plugin because if you are switching one theme to another theme custom filter functionality erase.
/* Plugin name: Custom Author Filter Description: A simple plugin to create Custom Author Filter. Author: Sekhar Version: 0.5 */ function output_buffer() { ob_start(); } add_action('init', 'output_buffer'); /* * restrict_manage_posts action hook allows to add custom filter to posts list * */ /** Create the filter dropdown */ add_action('restrict_manage_posts','add_custom_filter_by_author'); function add_custom_filter_by_author() { global $typenow; if ($typenow=='post') { $authors = get_users(); if ( $authors ) { printf( 'select class="postform" name="%s">', 'author' ); printf( '<option selected="selected" value="0">%s</option>', "Show All Authors" ); foreach ( $authors as $author ) { if(!in_array('subscriber',$author->roles)) { if(isset($_GET["author"])){ if($_GET["author"] == $author->ID){ printf( '<option selected="selected" value="%s">%s</option>', $author->ID, $author->display_name ); }else{ printf( '<option value="%s">%s</option>', $author->ID, $author->display_name ); } }else{ printf( '<option value="%s">%s</option>', $author->ID, $author->display_name ); } } } print( '</select>' ); } } }
No comments: