wordpress的作者角色在后台发布文章时强制需要审核的PHP代码

要在 WordPress 中强制作者(Author)角色发布的文章需要经过审核,你可以通过以下几种方式实现:

方法一:使用 wp_insert_post_data 过滤器

将以下代码添加到主题的 functions.php 文件或自定义插件中:

function force_author_posts_to_pending($data, $postarr) {
if (current_user_can('author')) {
$data['post_status'] = 'pending';
}
return $data;
}
add_filter('wp_insert_post_data', 'force_author_posts_to_pending', 99, 2);

方法二:使用 user_has_cap 过滤器(更彻底)

这个方法会完全移除作者直接发布文章的能力:

function disable_author_publish_capability($allcaps, $caps, $args, $user) {
// 检查是否是作者角色
if (in_array('author', $user->roles)) {
// 移除发布权限
$allcaps['publish_posts'] = false;
}
return $allcaps;
}
add_filter('user_has_cap', 'disable_author_publish_capability', 10, 4);

方法三:结合两种方法(推荐)

// 移除作者的直接发布权限
function disable_author_publish_capability($allcaps, $caps, $args, $user) {
if (in_array('author', $user->roles)) {
$allcaps['publish_posts'] = false;
}
return $allcaps;
}
add_filter('user_has_cap', 'disable_author_publish_capability', 10, 4);

// 确保编辑文章时也不会意外发布
function force_author_edits_to_pending($data, $postarr) {
// 只对新文章和作者角色生效
if (current_user_can('author') && !$postarr['ID']) {
$data['post_status'] = 'pending';
}
return $data;
}
add_filter('wp_insert_post_data', 'force_author_edits_to_pending', 99, 2);

注意事项

  1. 这些代码应该添加到子主题的 functions.php 文件或自定义插件中,以避免主题更新时丢失
  2. 方法二和方法三的组合是最彻底的解决方案
  3. 如果你想对其他自定义角色应用相同规则,只需修改角色检查部分
  4. 这些修改不会影响管理员和编辑角色的权限
赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址