【问题标题】:wordpress admin panel, how to prevent users from deleting categories?wordpress 管理面板,如何防止用户删除类别?
【发布时间】:2021-04-05 09:52:00
【问题描述】:

我正在尝试从类别编辑部分移除、隐藏或禁用“删除”按钮 我试过的代码。没有运气。有什么想法吗?

add_action('admin_head', 'hide_category_buttons');
function hide_category_buttons() {
  echo '<style>
    .taxonomy-category tr:hover .row-actions {
        visibility: hidden;
    }
  </style>';
}

【问题讨论】:

    标签: php css wordpress admin panel


    【解决方案1】:

    我正在尝试从类别中移除、隐藏或禁用“删除”按钮 我试过的编辑部分代码。没有运气。有什么想法吗?

    下面的代码应该做你想做的事:

    • “删除”按钮,隐藏阻止在帖子分类category 和它的编辑页面。
    • “删除”按钮,隐藏阻止在帖子分类tags 和它的编辑页面。

    我还添加了关于用户能力的条件语句。如果用户可以编辑帖子,他将不会受到我们的限制。

    你可以了解更多关于current_user_can()函数@https://developer.wordpress.org/reference/functions/current_user_can/

    一切都经过我的测试,并且正在运行。

    <?php
    add_action( 'init', 'admin_restriction' );
    function admin_restriction() {
      if ( is_admin() && strpos( $_SERVER[ 'REQUEST_URI' ], 'edit-tags.php?taxonomy=category' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'term.php?taxonomy=category' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'edit-tags.php?taxonomy=post_tag' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'term.php?taxonomy=post_tag' ) ) {
        if ( ! current_user_can( 'edit_post' ) ) { //Define use capability here
          add_action( 'admin_head', 'admin_scripts' );
          function admin_scripts() {
            echo "<style type='text/css'>
            .delete {
              display: none;
              visibility: hidden; }
            </style>";
            echo "<script type='text/javascript'>
            jQuery( document ).ready( function($) {
              $( '.delete-tag' ).click( false ); } );
            </script>";
          };
        };
      };
    }; ?>
    

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 2019-09-21
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2020-09-01
      相关资源
      最近更新 更多