本文介绍了Wordpress:有条件地更改用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为多作者创建一个Wordpress网站,并希望根据文章提交设置用户角色。意味着如果任何用户有0-10条文章,他们将进入贡献者角色,如果11-30条变为作者角色,如果31-100条变为编辑者角色。

I am creating a Wordpress website for multi author and want to set user role as per article submission. Means if any user have 0-10 article they will go to Contributor role, if 11-30 will go to Author role if 31-100 will go to Editor role.

我也想建立一个默认的注册组为订户的注册系统。他们将获得指向验证电子邮件的链接,例如

Also I want to make registration system where default registration group will be Subscriber. They will get a link into verification email like

如果您想成为贡献者,请单击下面的链接。 (要提交文章,您必须至少具有贡献者权限)
http://链接将在此处...该链接会自动将用户角色从订阅者更改为贡献者。

If you want to become a Contributor please click on below link. (To submit an article you must have at least Contributor permission)http:// link will be here ... this link automatically change user role from Subscriber to Contributor.

希望我会从您的专家那里得到解决方案。我正在发布这个问题,希望您的朋友充满希望。

Hope I will get solution from you expert. I am posting this issue with lots of hope from you friends.

推荐答案

您要做的是当他们发布提交检查时查看他们撰写了多少帖子,然后更改了角色。因此,在主题的functions.php文件中,您需要一个类似这样的钩子。

What you want to do is when they post their submission check to see how many posts they have authored and then change the role. So in your theme's functions.php file you'd need a hook that is like this.

add_action('publish_post', 'update_roles');

,然后是一个更新角色的函数。

and then a function to update the roles.

function update_roles()
{

   global $wpdb;

   // Get the author
   $author = wp_get_current_user();

   // Not sure if $author and $u are the same object I suspect they are.
   // so this may not be necessary, but I found this code elsewhere.
   // You may be able to do without this and just replace $u with $author later in the code.
   // Get post by author
   $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

   $numPost = count($posts);

   // Do the checks to see if they have the roles and if not update them.
   if($numPost > 0 && $numposts <= 10 && current_user_can('subscriber'))
   {
       // Remove role
       $author->remove_role( 'subscriber' );

       // Add role
       $author->add_role( 'contributor' );

   }

   ...... other conditions .......

}

这篇关于Wordpress:有条件地更改用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:14