-
-
Save modemlooper/7f70b85f74d74d892d3e to your computer and use it in GitHub Desktop.
example of adding a dropdown filter to media library
This file contains 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
class BP_MEDIA_LIBRARY_FILTER { | |
/** | |
* Defines media types | |
* @var array | |
*/ | |
protected static $media_types = array(); | |
/** | |
* Holds a singleton instance of this class | |
* @var null | |
*/ | |
protected static $instance = null; | |
/** | |
* Returns an instance of this class | |
*/ | |
public static function get_instance() { | |
if ( null === self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
/** | |
* __construct function. | |
* | |
* @access private | |
* @return void | |
*/ | |
private function __construct() { | |
self::$media_types = array( | |
'all_user_media' => __( 'All User Media', 'bp-media-custom-fields' ), | |
'my_media' => __( 'My Media', 'bp-media-custom-fields' ), | |
); | |
add_filter( 'restrict_manage_posts', array( $this, 'bp_media_restrict_manage_posts' ) ); | |
add_filter( 'request', array( $this, 'bp_media_request_admin' ) ); | |
add_filter( 'post_mime_types', array( $this, 'bp_media_modify_post_mime_types' ) ); | |
add_action( 'pre_get_posts' , array( $this, 'bp_media_pre_get_posts' ) ); | |
} | |
/** | |
* modify_post_mime_types function. | |
* | |
* @access public | |
* @param mixed $post_mime_types | |
* @return void | |
*/ | |
public function bp_media_modify_post_mime_types( $post_mime_types ) { | |
//set meme type to empty | |
$post_mime_types['bpmedia'] = array( '', '', '' ); | |
return $post_mime_types; | |
} | |
/** | |
* request_admin function. | |
* | |
* @access public | |
* @param mixed $request | |
* @return void | |
*/ | |
public function bp_media_request_admin( $request ) { | |
// bail if not the admin screen | |
if ( ! is_admin() ) { | |
return $request; | |
} | |
// add a mime type so items display | |
if ( ! empty( $_GET['media_type'] ) ) { | |
$request['post_mime_types'] = 'bpmedia'; | |
} | |
return $request; | |
} | |
/** | |
* restrict_manage_posts function. | |
* | |
* @access public | |
* @return void | |
*/ | |
public function bp_media_restrict_manage_posts() { | |
global $pagenow; | |
if( 'upload.php' != $pagenow ) return; | |
?> | |
<select name="media_type"> | |
<option value=""><?php _e( 'User Media', 'bp-media-type-search' ); ?></option> | |
<?php foreach ( self::$media_types as $type => $name ): ?> | |
<option value="<?php echo esc_attr( $type ); ?>" <?php selected( $_GET['media_type'], $type ); ?>><?php echo esc_attr( $name ); ?></option> | |
<?php endforeach; ?> | |
</select> | |
<?php | |
} | |
/** | |
* bp_media_pre_get_posts function. | |
* | |
* @access public | |
* @param mixed $query | |
* @return void | |
*/ | |
public function bp_media_pre_get_posts( $query ) { | |
global $pagenow; | |
if( 'upload.php' != $pagenow || !$query->is_admin && $query->is_main_query() ) return; | |
// filter only items that have bp_media meta | |
$query->set( 'meta_query', array( | |
array( | |
'key' => 'bp_media', | |
'value' => '1' | |
) | |
) | |
); | |
// filter based on select | |
switch ( $_REQUEST['media_type'] ) { | |
case 'my_media': | |
$query->set( 'author', bp_loggedin_user_id() ); | |
break; | |
} | |
} | |
} | |
BP_MEDIA_LIBRARY_FILTER::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment