This is a pretty goofy function, but a client once deleted the default user roles using some plugin, and I just threw this into functions.php to re-create them.
It’s hooked onto admin_init
, so it runs when you load any back-end page. I set a transient that keeps the function from running more than once (and gives me 10 minutes to remove the code).
add_action( 'admin_init', function() { // set transient just to keep this from running more than one $run = '1';// change the number to run again if ( get_transient('admin_run_once' ) === $run ) return; set_transient( 'admin_run_once', $run, 1e5 );// expires a bit after a day (100,000 seconds) remove_role( 'subscriber' ); add_role('subscriber', 'Subscriber', array ( 'read' => true, 'level_0' => true, ) ); remove_role( 'contributor' ); add_role('contributor', 'Contributor', array ( 'edit_posts' => true, 'read' => true, 'level_1' => true, 'level_0' => true, 'delete_posts' => true, ) ); remove_role( 'author' ); add_role('author', 'Author', array ( 'upload_files' => true, 'edit_posts' => true, 'edit_published_posts' => true, 'publish_posts' => true, 'read' => true, 'level_2' => true, 'level_1' => true, 'level_0' => true, 'delete_posts' => true, 'delete_published_posts' => true, ) ); remove_role( 'editor' ); add_role('editor', 'Editor', array ( 'moderate_comments' => true, 'manage_categories' => true, 'manage_links' => true, 'upload_files' => true, 'unfiltered_html' => true, 'edit_posts' => true, 'edit_others_posts' => true, 'edit_published_posts' => true, 'publish_posts' => true, 'edit_pages' => true, 'read' => true, 'level_7' => true, 'level_6' => true, 'level_5' => true, 'level_4' => true, 'level_3' => true, 'level_2' => true, 'level_1' => true, 'level_0' => true, 'edit_others_pages' => true, 'edit_published_pages' => true, 'publish_pages' => true, 'delete_pages' => true, 'delete_others_pages' => true, 'delete_published_pages' => true, 'delete_posts' => true, 'delete_others_posts' => true, 'delete_published_posts' => true, 'delete_private_posts' => true, 'edit_private_posts' => true, 'read_private_posts' => true, 'delete_private_pages' => true, 'edit_private_pages' => true, 'read_private_pages' => true, ) ); remove_role( 'administrator' ); add_role( 'administrator', 'Administrator', array ( 'switch_themes' => true, 'edit_themes' => true, 'activate_plugins' => true, 'edit_plugins' => true, 'edit_users' => true, 'edit_files' => true, 'manage_options' => true, 'moderate_comments' => true, 'manage_categories' => true, 'manage_links' => true, 'upload_files' => true, 'import' => true, 'unfiltered_html' => true, 'edit_posts' => true, 'edit_others_posts' => true, 'edit_published_posts' => true, 'publish_posts' => true, 'edit_pages' => true, 'read' => true, 'level_10' => true, 'level_9' => true, 'level_8' => true, 'level_7' => true, 'level_6' => true, 'level_5' => true, 'level_4' => true, 'level_3' => true, 'level_2' => true, 'level_1' => true, 'level_0' => true, 'edit_others_pages' => true, 'edit_published_pages' => true, 'publish_pages' => true, 'delete_pages' => true, 'delete_others_pages' => true, 'delete_published_pages' => true, 'delete_posts' => true, 'delete_others_posts' => true, 'delete_published_posts' => true, 'delete_private_posts' => true, 'edit_private_posts' => true, 'read_private_posts' => true, 'delete_private_pages' => true, 'edit_private_pages' => true, 'read_private_pages' => true, 'delete_users' => true, 'create_users' => true, 'unfiltered_upload' => true, 'edit_dashboard' => true, 'update_plugins' => true, 'delete_plugins' => true, 'install_plugins' => true, 'update_themes' => true, 'install_themes' => true, 'update_core' => true, 'list_users' => true, 'remove_users' => true, 'promote_users' => true, 'edit_theme_options' => true, 'delete_themes' => true, 'export' => true, ) ); });