4

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

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

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

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

4

2 回答 2

8

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

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

}
于 2012-02-07T18:52:36.583 回答
0

使用 SQL 语句(数据库查询)获取 Wordpress 数据不符合 Wordpress 编码标准。 请参阅 Wordpress 手册

最好使用 count_user_posts 函数

见法典上的功能

于 2013-06-29T02:03:52.440 回答