最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

admin - Force Contributors to add Tags via edit-tags.php

programmeradmin3浏览0评论

I need to be able to stop contributors to the website from adding new Tags when they create or edit a Post in WP.

The reason is that I need them to enter an additional field at the same time which I can achieve via ACF in the edit-tags.php page.

However, no matter what I do I can't seem to stop users entering new tags on the tag section on the new post or edit post page(s). They should still be able to select from existing tags when creating/editing a post, but to enter one they should be forced to use the edit-tags.php.

Here's what I've got so far (edited after discussions below):

function disallow_insert_term($term, $taxonomy) {
  if( strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php') || strstr($_SERVER['REQUEST_URI'], 'wp-admin/post.php') ) {
    return new WP_Error(
      'disallow_insert_term',
      __('Your role does not have permission to add terms to this taxonomy')
    );
  } elseif (strstr($_SERVER['REQUEST_URI'], 'wp-admin/edit-tags.php')) {
      return $term;
  } else {
      return $term;
  }
}
add_filter('pre_insert_term', 'disallow_insert_term', 10, 2);

The problem is that any new "if" tests I try to do within the function end up being ignored and just the final task runs. So, in the above, the user can enter new tags on any page. If I remove the "elseif" function you can't enter any tags into the site at all.

I've tried using get_current_screen() and $wp->request to get the URL but just run into the same problem.

发布评论

评论列表(0)

  1. 暂无评论