朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

WordPress后台只显示当前用户的文章和媒体文件

在我们使用wordpress多用户主题的时候,WordPress后台会默认显示的所有用户文章、媒体文件,这样的情况会导致大家都可以看到其他用户的文章、媒体文件,很不方便管理。

今天本站分享一篇够让后台后台只显示当前登录用户的文章和媒体文件wordpress教程希望能给大家带来帮助

具体方法是将下面的代码添加到当前主题的 functions.php 文件即可:

//仅显示当前用户的文章、媒体文件

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;
		}
	}
}

注:如果你的WordPress所使用的数据库前缀不是默认的wp_,请将第 5 、6 行中的wp_ 修改为你的WordPress站点的数据库前缀。

文章来源于互联网:wordpress后台只显示当前用户的文章和媒体文件

赞(0) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » WordPress后台只显示当前用户的文章和媒体文件

评论 39

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #45

    Stunning story there. What occurred after? Take care!
    https://cutt.ly/r9myRbP

  2. #44

    Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your feed and I hope you write again soon! https://www.youtube.com/watch?v=2IEIuU18xdk

  3. #43

    Wonderful article! This is the type of information that are
    supposed to be shared around the web. Shame on Google
    for no longer positioning this submit higher! Come on over and
    visit my site . Thank you =)

    Here is my page … coupon

    coupon2个月前 (11-27)回复
  4. #42

    Somebody necessarily lend a hand to make seriously articles I’d state.
    That is the very first time I frequented your website page
    and up to now? I amazed with the analysis you made to make this actual put up incredible.
    Great activity!

    http://tinyurl.com/5个月前 (09-09)回复
  5. #41

    Very nice blog post. I absolutely love this site. Thanks!

    tinyurl.com5个月前 (09-05)回复
  6. #40

    I like the valuable info you provide in your articles.
    I’ll bookmark your blog and check again here regularly.

    I am quite certain I will learn many new stuff right here!
    Best of luck for the next!

    tinyurl.com6个月前 (08-15)回复
  7. #39

    Quality content is the secret to interest the viewers to pay a quick
    visit the web site, that’s what this web page is providing.

    tinyurl.com6个月前 (08-13)回复
  8. #38

    This information is invaluable. When can I find out more?

    tinyurl.com6个月前 (08-13)回复
  9. #37

    Good day! Would you mind if I share your blog with my myspace
    group? There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Thank you

    Here is my web page coupon

    coupon6个月前 (08-12)回复