Wordpress:有条件地更改用户角色

Cod*_*ver 3 wordpress conditional role

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

另外我想建立一个默认注册组将是Subscriber的注册系统.他们会收到验证邮件的链接

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

希望我能从专家那里得到解决方案.我发布这个问题有很多朋友的希望.

the*_*imp 7

你想要做的是当他们发布他们的提交检查以查看他们创作了多少帖子然后更改角色.所以在你的主题的functions.php文件中,你需要一个像这样的钩子.

add_action('publish_post', 'update_roles');
Run Code Online (Sandbox Code Playgroud)

然后是更新角色的功能.

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 .......

}
Run Code Online (Sandbox Code Playgroud)