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

标签 wordpress conditional role

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

另外我想制作默认注册组为订阅者的注册系统。他们将获得验证电子邮件的链接,例如

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

希望我能从你的专家那里得到解决方案。我发布这个问题,希望各位 friend 寄予厚望。

最佳答案

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

add_action('publish_post', 'update_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:有条件地更改用户角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181440/

相关文章:

mysql - Wordpress MySQL 查询执行时间太长

mysql - 如何在docker中运行wordpress和mysql解决 "Error establishing a database connection"

wordpress - 如何在 wordpress 上找到我的 php.ini?

vba - 如何在条件格式中适应非常长的条件

radio-button - 基于 R Shiny 条件面板中的单选按钮的切换图

conditional - 如何使用条件检查 channel 中是否有任何条目?

postgresql - 无法连接到 PostgreSQL 数据库

perl - 我如何需要带有接口(interface)的 Moose 属性?

variables - 如何在 ansible 角色中设置常量?

mysql - Compex MySQL Left Join 使用元表中的多个条目