{"id":46605,"date":"2026-10-02T08:02:55","date_gmt":"2026-10-02T08:02:55","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46605"},"modified":"2026-10-10T16:33:33","modified_gmt":"2026-10-10T16:33:33","slug":"wp-cli-list-admin-users-hidden-admins","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-cli-list-admin-users-hidden-admins\/","title":{"rendered":"WP CLI List Admin Users, Including Hidden Admins"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>wp user list --role=administrator<\/code> answers &#8220;who are my admins?&#8221; on a healthy site. On a hacked one it can leave someone out. Backdoors often hook <code>pre_user_query<\/code> to hide the account they created. That account then disappears from the Users screen and from every tool that runs through <code>WP_User_Query<\/code>, and <code>wp user list<\/code> is one of those tools. The command also only checks the role. It misses a user who was given <code>manage_options<\/code> directly while still showing as an Editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet adds <code>wp dpc-admins<\/code>. It reads the capability rows straight from the <code>usermeta<\/code> table and keeps every account that holds the capability, whether through a role or a direct grant. It then compares that list with what the normal user query returns. Any account the query can&#8217;t see is marked <code>hidden: YES<\/code>. The snippet also starts recording a last-login time, so you can spot an admin account that nobody on the team uses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Save this as <code>wp-content\/mu-plugins\/dpc-admin-audit.php<\/code>. Use an mu-plugin so the command still works when the regular plugins are deactivated during a cleanup.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Plugin Name: DPC Admin Audit\n * Description: Lists every account that can manage the site, read from the database, with a WP-CLI command.\n *\/\n\ndefined( 'ABSPATH' ) || exit;\n\nadd_action( 'wp_login', 'dpc_record_last_login', 10, 2 );\n\nfunction dpc_record_last_login( string $user_login, WP_User $user ): void {\n\tupdate_user_meta( $user-&gt;ID, 'dpc_last_login', time() );\n}\n\n\/**\n * Every user holding $capability, via a role or a direct grant.\n *\/\nfunction dpc_get_admin_audit( string $capability = 'manage_options' ): array {\n\tglobal $wpdb;\n\n\t$all_roles      = wp_roles()-&gt;role_objects;\n\t$granting_roles = array();\n\tforeach ( $all_roles as $role_name =&gt; $role ) {\n\t\tif ( $role-&gt;has_cap( $capability ) ) {\n\t\t\t$granting_roles&#91;&#93; = $role_name;\n\t\t}\n\t}\n\n\t\/\/ Raw rows: pre_user_query filters cannot hide anyone from this.\n\t$rows = $wpdb-&gt;get_results(\n\t\t$wpdb-&gt;prepare(\n\t\t\t\"SELECT u.ID, u.user_login, u.user_email, u.user_registered, m.meta_value\n\t\t\tFROM {$wpdb-&gt;users} AS u\n\t\t\tINNER JOIN {$wpdb-&gt;usermeta} AS m ON m.user_id = u.ID\n\t\t\tWHERE m.meta_key = %s\n\t\t\tORDER BY u.ID\",\n\t\t\t$wpdb-&gt;get_blog_prefix() . 'capabilities'\n\t\t)\n\t);\n\n\t\/\/ What the normal user query (Users screen, wp user list) returns.\n\t$visible_ids = array_map(\n\t\t'intval',\n\t\tget_users( array( 'capability' =&gt; $capability, 'fields' =&gt; 'ID' ) )\n\t);\n\n\t$audit = array();\n\tforeach ( $rows as $row ) {\n\t\t$caps = maybe_unserialize( $row-&gt;meta_value );\n\t\tif ( ! is_array( $caps ) ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t$granted   = array_keys( array_filter( $caps ) );\n\t\t$via_roles = array_intersect( $granted, $granting_roles );\n\t\t$direct    = ! empty( $caps&#91; $capability &#93; );\n\t\tif ( ! $via_roles &amp;&amp; ! $direct ) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t$sources = $via_roles ? array( 'role' ) : array();\n\t\tif ( $direct ) {\n\t\t\t$sources&#91;&#93; = 'direct';\n\t\t}\n\n\t\t$last_login = (int) get_user_meta( (int) $row-&gt;ID, 'dpc_last_login', true );\n\n\t\t$audit&#91;&#93; = array(\n\t\t\t'ID'         =&gt; (int) $row-&gt;ID,\n\t\t\t'user_login' =&gt; $row-&gt;user_login,\n\t\t\t'user_email' =&gt; $row-&gt;user_email,\n\t\t\t'roles'      =&gt; implode( ',', array_intersect( $granted, array_keys( $all_roles ) ) ),\n\t\t\t'granted_by' =&gt; implode( '+', $sources ),\n\t\t\t'registered' =&gt; $row-&gt;user_registered,\n\t\t\t'last_login' =&gt; $last_login ? wp_date( 'Y-m-d H:i', $last_login ) : 'not recorded',\n\t\t\t'hidden'     =&gt; in_array( (int) $row-&gt;ID, $visible_ids, true ) ? 'no' : 'YES',\n\t\t);\n\t}\n\n\treturn $audit;\n}\n\n\/**\n * List every account that holds a capability, read straight from the database.\n *\n * ## OPTIONS\n *\n * &#91;--capability=&lt;capability&gt;&#93;\n * : Capability to audit.\n * ---\n * default: manage_options\n * ---\n *\n * &#91;--format=&lt;format&gt;&#93;\n * : Output format.\n * ---\n * default: table\n * options:\n *   - table\n *   - csv\n *   - json\n *   - yaml\n * ---\n *\n * ## EXAMPLES\n *\n *     wp dpc-admins\n *     wp dpc-admins --capability=edit_users --format=csv\n *\/\nfunction dpc_cli_admins( array $args, array $assoc_args ): void {\n\t$capability = sanitize_key( $assoc_args&#91;'capability'&#93; ?? 'manage_options' );\n\t$items      = dpc_get_admin_audit( $capability );\n\n\tif ( ! $items ) {\n\t\tWP_CLI::warning( \"No users hold '{$capability}'.\" );\n\t\treturn;\n\t}\n\n\tWP_CLIUtilsformat_items( $assoc_args&#91;'format'&#93; ?? 'table', $items, array_keys( $items&#91;0&#93; ) );\n\n\t$hidden = count( wp_list_filter( $items, array( 'hidden' =&gt; 'YES' ) ) );\n\tif ( $hidden ) {\n\t\tWP_CLI::warning( \"{$hidden} account(s) are hidden from the normal user query. Check them now.\" );\n\t}\n}\n\nif ( defined( 'WP_CLI' ) &amp;&amp; WP_CLI ) {\n\tWP_CLI::add_command( 'dpc-admins', 'dpc_cli_admins' );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using it<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"># Every account with manage_options, as a table\nwp dpc-admins\n\n# Export for a client report, or audit a different capability\nwp dpc-admins --format=csv &gt; admins.csv\nwp dpc-admins --capability=edit_users\n\n# Core equivalent: role only, and it goes through the filterable user query\nwp user list --role=administrator --fields=ID,user_login,user_email\n\n# Passwords are hashed, so reset them instead of trying to read them\nwp user reset-password dpc_ghost\nwp user delete dpc_ghost --reassign=1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First it finds every role that grants the capability, using <code>wp_roles()<\/code>. Custom roles count too.<\/li>\n\n\n<li>Next it reads the <code>{prefix}capabilities<\/code> meta row for each user directly with <code>$wpdb<\/code>. <code>pre_user_query<\/code> filters only change <code>WP_User_Query<\/code>, so they can&#8217;t remove anyone from this list.<\/li>\n\n\n<li>A user is kept if one of their roles grants the capability or if the capability is set directly on them. The <code>granted_by<\/code> column tells you which: <code>role<\/code>, <code>direct<\/code> or <code>role+direct<\/code>.<\/li>\n\n\n<li>Then the same capability is looked up through <code>get_users()<\/code>. If an account is in the database result but missing from that list, some code is hiding it, and the account is marked <code>hidden: YES<\/code>.<\/li>\n\n\n<li>A <code>wp_login<\/code> hook saves <code>dpc_last_login<\/code>. It only records logins that happen after you install the snippet, so older accounts show <code>not recorded<\/code> at first.<\/li>\n\n\n<li>Warnings go to STDERR, so <code>--format=csv<\/code> and <code>--format=json<\/code> output stays clean when you pipe it.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>You can&#8217;t &#8220;retrieve&#8221; an admin password.<\/strong> People search for this in phpMyAdmin a lot. <code>user_pass<\/code> holds a one-way hash. Find the account with this command, then reset its password with <code>wp user reset-password<\/code> or set a new one with <code>wp user update<\/code>.<\/li>\n\n\n<li>If a user is flagged <code>hidden<\/code>, assume the site is compromised. Deleting the user is not enough. Look for the code that hides it (<code>grep -r pre_user_query wp-content<\/code>) and remove that as well.<\/li>\n\n\n<li>The check is per site. On multisite, run it once per site with <code>--url=<\/code> and check network admins separately with <code>wp super-admin list<\/code>.<\/li>\n\n\n<li>It reads the stored roles and capabilities. Permissions added at runtime through <code>user_has_cap<\/code> or <code>map_meta_cap<\/code> filters don&#8217;t show up here.<\/li>\n\n\n<li>It loads one meta row per user. That&#8217;s fine as an occasional audit, but don&#8217;t call <code>dpc_get_admin_audit()<\/code> on page loads on a site with very large numbers of users.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Related: to get an alert as soon as a new account appears, add <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/notify-admin-when-new-account-is-created\/\">Notify Admin When New Account Is Created<\/a>. To narrow who can reach wp-admin at all, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/lockdown-admin-backend-to-your-ip\/\">Lockdown Admin Backend to your IP<\/a>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>A custom WP-CLI command that reads every admin account straight from the database and flags admins hidden from the Users screen.<\/p>\n","protected":false},"author":1,"featured_media":46606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WP CLI List Admin Users, Including Hidden Admins - Code DPlugins","description":"A custom WP-CLI command that reads every admin account straight from the database and flags admins hidden from the Users screen."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[131,3],"tags":[151,148,144,150,149],"dpc_library":[],"class_list":["post-46605","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-production","category-wordpress","tag-malware","tag-mu-plugin","tag-security","tag-users","tag-wp-cli"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46605"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46605\/revisions"}],"predecessor-version":[{"id":46656,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46605\/revisions\/46656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46606"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46605"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46605"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}