{"id":238,"date":"2018-04-01T21:55:56","date_gmt":"2018-04-01T21:55:56","guid":{"rendered":"http:\/\/new.ras01.com\/?p=238"},"modified":"2018-04-09T20:41:16","modified_gmt":"2018-04-10T01:41:16","slug":"wordpress-user-controls-editor-cannot-delete-super-admin","status":"publish","type":"post","link":"https:\/\/riotactstudios.com\/wordpress-user-controls-editor-cannot-delete-super-admin\/","title":{"rendered":"WordPress User Controls: Editor Cannot Delete Super Admin"},"content":{"rendered":"<p>One of the many tweaks we often build into our plugins to enhance the WordPress dashboard functionality is limiting the capabilities of additional users who have been given access. In this case, Editors have the ability to add\/remove\/edit users and could accidentally or possibly purposefully change the access rights of the primary admin (Super Admin). With this little bit of code, we eliminate the possibility of that. Let&#8217;s get started!<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n\/**\r\n * Editor cannot edit or delete Super Admin\r\n *\/\r\n\r\nclass RAS_User_Caps {\r\n\r\n  \/\/ Add our filters\r\n  function RAS_User_Caps(){\r\n    add_filter( 'editable_roles', array(&amp;$this, 'editable_roles'));\r\n    add_filter( 'map_meta_cap', array(&amp;$this, 'map_meta_cap'),10,4);\r\n  }\r\n\r\n  \/\/ Remove 'Administrator' from the list of roles if the current user is not an admin\r\n  function editable_roles( $roles ){\r\n    if( isset( $roles&#x5B;'administrator'] ) &amp;&amp; !current_user_can('administrator') ){\r\n      unset( $roles&#x5B;'administrator']);\r\n    }\r\n    return $roles;\r\n  }\r\n\r\n  \/\/ If someone is trying to edit or delete and admin and that user isn't an admin, don't allow it\r\n  function map_meta_cap( $caps, $cap, $user_id, $args ){\r\n\r\n    switch( $cap ){\r\n        case 'edit_user':\r\n        case 'remove_user':\r\n        case 'promote_user':\r\n            if( isset($args&#x5B;0]) &amp;&amp; $args&#x5B;0] == $user_id )\r\n                break;\r\n            elseif( !isset($args&#x5B;0]) )\r\n                $caps&#x5B;] = 'do_not_allow';\r\n            $other = new WP_User( absint($args&#x5B;0]) );\r\n            if( $other-&gt;has_cap( 'administrator' ) ){\r\n                if(!current_user_can('administrator')){\r\n                    $caps&#x5B;] = 'do_not_allow';\r\n                }\r\n            }\r\n            break;\r\n        case 'delete_user':\r\n        case 'delete_users':\r\n            if( !isset($args&#x5B;0]) )\r\n                break;\r\n            $other = new WP_User( absint($args&#x5B;0]) );\r\n            if( $other-&gt;has_cap( 'administrator' ) ){\r\n                if(!current_user_can('administrator')){\r\n                    $caps&#x5B;] = 'do_not_allow';\r\n                }\r\n            }\r\n            break;\r\n        default:\r\n            break;\r\n    }\r\n    return $caps;\r\n  }\r\n\r\n}\r\n\r\n$ras_user_caps = new RAS_User_Caps();\r\n<\/pre>\n<p>This bit of code could be added to the theme<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">functions.php<\/pre>\n<p>file, but we prefer to put this into a custom plugin so that if the theme is ever changed, the rules still apply.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the many tweaks we often build into our plugins to enhance the WordPress dashboard functionality is limiting the capabilities of additional users who have been given access. In this case, Editors have the ability to add\/remove\/edit users and could accidentally or possibly purposefully change the access rights of the primary admin (Super Admin). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":243,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[28],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":2,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":332,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/posts\/238\/revisions\/332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/media\/243"}],"wp:attachment":[{"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/riotactstudios.com\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}