WordPress網(wǎng)站如何實(shí)現(xiàn)后臺只顯示當(dāng)前用戶的文章和媒體?
wordpress后臺都是默認(rèn)顯示所有用戶的文章及媒體文件,這樣就會導(dǎo)致大家也可以查看其它用戶的文章及媒體文件,這時候就有人問WordPress網(wǎng)站如何實(shí)現(xiàn)后臺只顯示當(dāng)前用戶的文章和媒體?接下來為大家分享一下。
將下面的代碼添加到當(dāng)前主題的 functions.php 文件即可:
//僅顯示當(dāng)前用戶的文章、媒體文件https:
add_action( 'init', 'check_user_role' );
function check_user_role() {
global $current_user;
if( $current_user->roles[0] != 'administrator' ) {
//在[媒體庫]只顯示用戶上傳的文件
add_action('pre_get_posts','MBT_restrict_media_library');
function MBT_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;
}
}
}