我写了一个功能来限制用户角色的用户活动更新,每天限制在一定数量。我遇到的问题是,在 switch 语句中,所有案例/块都被跳过并直接进入默认值并忽略所有 wp 角色。
默认不允许任何帖子并给出错误信息
感谢您提供的任何意见。
<?php
// Limit activity posts by day and user role
function limit_activity_posts($user_id)
{
// get the current user role
$user_role = get_userdata($user_id)->roles[0];
// set the maximum number of posts based on user role
switch ($user_role) {
case "subscriber":
$max_posts = 3;
break;
case "contributor":
$max_posts = 5;
break;
case "editor":
$max_posts = 10;
break;
case "administrator":
$max_posts = -1;
break;
default:
$max_posts = 0;
}
// check the number of posts for the current user
$today = strtotime("today midnight");
$args = [
"user_id" => $user_id,
"date_query" => [
[
"after" => $today,
"inclusive" => true,
],
],
];
$count = bp_activity_get($args, "total");
// if the maximum number of posts is reached, prevent new post submission
if ($max_posts >= 0 && $count >= $max_posts) {
bp_core_add_message(
sprintf(
__(
"You have reached the maximum number of posts allowed for today (%d).",
"buddypress"
),
$max_posts
),
"error"
);
bp_core_redirect(bp_get_activity_directory_permalink());
}
}
add_action("bp_activity_before_save", "limit_activity_posts");
我期待的结果是每个wp角色都有自己的每日限额
这个版本比我原来的版本简单,它是一个带有管理员选项的插件形式。遇到多个问题,所以为了安全起见,我决定只使用 wp codebox 来实现一个功能。