一聚教程网:一个值得你收藏的教程网站

热门教程

WordPress 多媒体库只显示当前用户上传的文件

时间:2022-06-25 19:07:18 编辑:袖梨 来源:一聚教程网


wordpress 的 media library 里只显示当前用户上传的文件。也试用于 acf_form

 代码如下 复制代码

//wordpress show only media user has uploaded
add_action('pre_get_posts','ml_restrict_media_library');
 
function ml_restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    if( !is_a( $current_user, 'WP_User') )
        return;
    if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
        return;
    if( !current_user_can('manage_media_library') )
        $wp_query_obj->set('author', $current_user->ID );
        return;
}

is_a() 函数已废弃,自 PHP 5 起使用 instanceof 类型运算符。上例在 PHP 5 中会是这样:

 代码如下 复制代码

//wordpress show only media user has uploaded
add_action('pre_get_posts','ml_restrict_media_library');
 
function ml_restrict_media_library( $wp_query_obj ) {
    global $current_user, $pagenow;
    //if( !is_a( $current_user, 'WP_User') )
    if ($current_user instanceof WP_User)
        return;
    if ( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
        return;
    if ( !current_user_can('manage_media_library') )
        $wp_query_obj->set('author', $current_user->ID );
        return;
}

热门栏目