php WordPress阻止管理员删除

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php WordPress阻止管理员删除相关的知识,希望对你有一定的参考价值。

// Do not let users create other admin users except when user is admin
    class VM_User_Caps {

        // Add our filters
        function __construct(){
            add_filter( 'editable_roles', array($this, 'editable_roles'));
            add_filter( 'map_meta_cap', array($this, 'map_meta_cap'), 10, 4);
          }
    
          // Remove 'Administrator' from the list of roles if the current user is not an admin
          function editable_roles( $roles ){
            if( isset( $roles['administrator'] ) && !current_user_can('administrator') ){
                unset( $roles['administrator']);
            }
            return $roles;
          }
    
          // If someone is trying to edit or delete and admin and that user isn't an admin, don't allow it
          function map_meta_cap( $caps, $cap, $user_id, $args ){
    
            switch( $cap ){
                case 'edit_user':
                case 'remove_user':
                case 'promote_user':
                    if( isset($args[0]) && $args[0] == $user_id )
                        break;
                    elseif( !isset($args[0]) )
                        $caps[] = 'do_not_allow';
                    $other = new WP_User( absint($args[0]) );
                    if( $other->has_cap( 'administrator' ) ){
                        if(!current_user_can('administrator')){
                            $caps[] = 'do_not_allow';
                        }
                    }
                    break;
                case 'delete_user':
                case 'delete_users':
                    if( !isset($args[0]) )
                        break;
                    $other = new WP_User( absint($args[0]) );
                    if( $other->has_cap( 'administrator' ) ){
                        if(!current_user_can('administrator')){
                            $caps[] = 'do_not_allow';
                        }
                    }
                    break;
                default:
                    break;
            }
            return $caps;
          }

    }

    $vm_user_caps = new VM_User_Caps();

以上是关于php WordPress阻止管理员删除的主要内容,如果未能解决你的问题,请参考以下文章

php 从管理员Wordpress中删除博客(帖子类型)

php WordPress:删除管理栏

php WordPress:删除管理栏

php wordpress删除管理栏

php 从WordPress管理页面删除通知

php 管理栏删除wordpress