databasewordpresspluginswordpress-capabilities

Delete custom Wordpress caps left behind by plugins


I recently installed and then uninstalled a plugin called Advanced Gutenberg from PublishPress.

Doing wp cap list <role> for several roles shows the custom caps it left behind, all prefixed with advgb . Aside from removing each one of these capabilities for each one of the roles modified one-by-one with wp cap remove <role> <cap>, is there a way to remove all these junk capabilities the plugin should have removed itself?

Looking closer at the DB, it seems Advanced Gutenberg added the caps directly to the wp_user_roles option of the wp_options table. The value of this option is in some JSON-like syntax that I don't understand and don't want to risk editing manually:

a:5:{s:13:"administrator";a:2:{s:4:"name";s:13:"Administrator";s:12:"capabilities";a:69:{s:13:"switch_themes";b:1;s:11:"edit_themes";b:1;s:16:"activate_plugins";b:1;s:12:"edit_plugins";b:1;s:10:"edit_users";b:1;s:10:"edit_files";b:1;s:14:"manage_options";b:1;s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:6:"import";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:8:"level_10";b:1;s:7:"level_9";b:1;s:7:"level_8";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;s:12:"delete_users";b:1;s:12:"create_users";b:1;s:17:"unfiltered_upload";b:1;s:14:"edit_dashboard";b:1;s:14:"update_plugins";b:1;s:14:"delete_plugins";b:1;s:15:"install_plugins";b:1;s:13:"update_themes";b:1;s:14:"install_themes";b:1;s:11:"update_core";b:1;s:10:"list_users";b:1;s:12:"remove_users";b:1;s:13:"promote_users";b:1;s:18:"edit_theme_options";b:1;s:13:"delete_themes";b:1;s:6:"export";b:1;s:19:"edit_advgb_profiles";b:1;s:26:"edit_others_advgb_profiles";b:1;s:21:"create_advgb_profiles";b:1;s:22:"publish_advgb_profiles";b:1;s:21:"delete_advgb_profiles";b:1;s:28:"delete_others_advgb_profiles";b:1;s:18:"read_advgb_profile";b:1;s:27:"read_private_advgb_profiles";b:1;s:17:"edit_others_posts";b:1;}}s:6:"editor";a:2:{s:4:"name";s:6:"Editor";s:12:"capabilities";a:37:{s:17:"moderate_comments";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:12:"upload_files";b:1;s:15:"unfiltered_html";b:1;s:10:"edit_posts";b:1;s:17:"edit_others_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:10:"edit_pages";b:1;s:4:"read";b:1;s:7:"level_7";b:1;s:7:"level_6";b:1;s:7:"level_5";b:1;s:7:"level_4";b:1;s:7:"level_3";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:17:"edit_others_pages";b:1;s:20:"edit_published_pages";b:1;s:13:"publish_pages";b:1;s:12:"delete_pages";b:1;s:19:"delete_others_pages";b:1;s:22:"delete_published_pages";b:1;s:12:"delete_posts";b:1;s:19:"delete_others_posts";b:1;s:22:"delete_published_posts";b:1;s:20:"delete_private_posts";b:1;s:18:"edit_private_posts";b:1;s:18:"read_private_posts";b:1;s:20:"delete_private_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"read_private_pages";b:1;s:18:"read_advgb_profile";b:1;s:27:"read_private_advgb_profiles";b:1;s:19:"edit_advgb_profiles";b:1;}}s:6:"author";a:2:{s:4:"name";s:6:"Author";s:12:"capabilities";a:15:{s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:4:"read";b:1;s:7:"level_2";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:22:"delete_published_posts";b:1;s:12:"upload_files";b:1;s:19:"edit_advgb_profiles";b:1;s:18:"read_advgb_profile";b:1;s:27:"read_private_advgb_profiles";b:1;s:17:"moderate_comments";b:1;s:20:"delete_private_posts";b:1;s:12:"delete_posts";b:1;s:10:"edit_posts";b:1;}}s:11:"contributor";a:2:{s:4:"name";s:11:"Contributor";s:12:"capabilities";a:7:{s:10:"edit_posts";b:1;s:4:"read";b:1;s:7:"level_1";b:1;s:7:"level_0";b:1;s:12:"delete_posts";b:1;s:18:"read_advgb_profile";b:1;s:27:"read_private_advgb_profiles";b:1;}}s:10:"subscriber";a:2:{s:4:"name";s:10:"Subscriber";s:12:"capabilities";a:2:{s:4:"read";b:1;s:7:"level_0";b:1;}}}

What is this syntax and how can I remove only the key-value pairs in it that contain advgb?


Solution

  • Since I already use wp-cli (highly recommended for Wordpress dev work), I managed to solve this with a quick Bash for loop:

    for i in `wp role list --field=role`; do wp cap remove "$i" <cap1> <cap2> <cap3>; done
    

    I ran in it in Cygwin but it will just as easily work in WSL or a Mac terminal.

    Simply make sure wp-cli is installed and replace <cap1>, <cap2>, etc. with each of the capabilities that the plugin added to your roles (you probably only need to check one role to get these) - the code will then loop through each of your roles and remove all the given caps from it.

    $ for i in `wp role list --field=role`; do wp cap remove "$i" read_advgb_profile read_private_advgb_profiles edit_advgb_profiles; done
    Success: Removed 3 capabilities from 'administrator' role.
    Success: Removed 3 capabilities from 'editor' role.
    Success: Removed 3 capabilities from 'author' role.
    Success: Removed 3 capabilities from 'contributor' role.
    Success: Removed 3 capabilities from 'subscriber' role.