Strictly Admin Roles Plugin

Home » Snippets » Strictly Admin Roles Plugin
0

Created with:

Visibility: 

public

Creator: Chris LaMorte

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: Strictly Admin Roles
 * Plugin URI: https://example.com/strictly-admin
 * Description: Modifies the existing Strictly Admin user role with specific capabilities.
 * Version: 1.0.0
 * Author: StrictlyWP
 * Author URI: https://strictlywp.com
 * License: GPLv2 or later
 * Text Domain: strictly-admin-roles
 */

defined( 'ABSPATH' ) || exit;

add_action( 'init', 'strictly_admin_modify_role', 999 );

/**
 * Modifies the existing Strictly Admin user role with specific capabilities.
 *
 * This function is called on the 'init' hook with a priority of 999, ensuring it runs early.
 * 
 * @since 1.0.0
 */
function strictly_admin_modify_role() {
    $editor_role = get_role( 'editor' );

    if ( $editor_role ) {
        // Rename editor role
        $editor_role->name = 'Strictly Admin';

        // Set custom capabilities
        $editor_role->add_cap( 'edit_theme_options' );
        $editor_role->add_cap( 'manage_options' );
        $editor_role->add_cap( 'customize' );
        $editor_role->add_cap( 'edit_pages' );
        $editor_role->add_cap( 'edit_others_pages' );
        $editor_role->add_cap( 'edit_published_pages' );
        $editor_role->add_cap( 'publish_pages' );
        $editor_role->add_cap( 'delete_pages' );
        $editor_role->add_cap( 'delete_others_pages' );
        $editor_role->add_cap( 'delete_published_pages' );
        $editor_role->add_cap( 'delete_others_posts' );
        $editor_role->add_cap( 'delete_private_posts' );
        $editor_role->add_cap( 'edit_private_posts' );
        $editor_role->add_cap( 'read_private_posts' );
        $editor_role->add_cap( 'delete_private_pages' );
        $editor_role->add_cap( 'edit_private_pages' );
        $editor_role->add_cap( 'read_private_pages' );
        $editor_role->add_cap( 'unfiltered_html' );
        $editor_role->add_cap( 'edit_published_posts' );
        $editor_role->add_cap( 'upload_files' );
        $editor_role->add_cap( 'publish_posts' );
        $editor_role->add_cap( 'delete_published_posts' );
        $editor_role->add_cap( 'edit_posts' );
        $editor_role->add_cap( 'delete_posts' );
        $editor_role->add_cap( 'read' );
        $editor_role->add_cap( 'strictly_admin' );

        // SEOPress Capabilities
        $editor_role->add_cap( 'seopress_manage_advanced' );
        $editor_role->add_cap( 'seopress_manage_analytics' );
        $editor_role->add_cap( 'seopress_manage_instant_indexing' );
        $editor_role->add_cap( 'seopress_manage_social_networks' );
        $editor_role->add_cap( 'seopress_manage_titles_metas' );
        $editor_role->add_cap( 'seopress_manage_tools' );
        $editor_role->add_cap( 'seopress_manage_xml_html_sitemap' );

        // Remove capabilities for specific custom post types
        $editor_role->remove_cap( 'edit_wpcd_onboarding_list' );
        $editor_role->remove_cap( 'delete_wpcd_onboarding_list' );
        $editor_role->remove_cap( 'publish_wpcd_onboarding_list' );
        $editor_role->remove_cap( 'edit_wpcd_ai_prompts' );
        $editor_role->remove_cap( 'delete_wpcd_ai_prompts' );
        $editor_role->remove_cap( 'publish_wpcd_ai_prompts' );

        // Remove capabilities for specific custom post types
        $editor_role->remove_cap( 'edit_kadence_elements' );
        $editor_role->remove_cap( 'edit_others_kadence_elements' );
        $editor_role->remove_cap( 'edit_published_kadence_elements' );
        $editor_role->remove_cap( 'edit_private_kadence_elements' );
        $editor_role->remove_cap( 'publish_kadence_elements' );
        $editor_role->remove_cap( 'delete_kadence_elements' );
        $editor_role->remove_cap( 'delete_others_kadence_elements' );
        $editor_role->remove_cap( 'delete_published_kadence_elements' );
        $editor_role->remove_cap( 'delete_private_kadence_elements' );
        $editor_role->remove_cap( 'read_private_kadence_elements' );
    }
}
				

Explanation:

The code snippet modifies the existing editor user role by renaming it to "Strictly Admin" using the name property of the $editor_role object. It then adds or removes specific capabilities as required by your use-case.

To make it function as a Must Use (MU) plugin, I've added the plugin header section at the beginning of the file. This section includes essential information about the plugin, such as name, description, version, author, etc. The defined( 'ABSPATH' ) || exit; statement ensures that the plugin is loaded only in the WordPress environment.

The modified code is wrapped in a function strictly_admin_modify_role(), which is hooked to the init action with a priority of 999. This ensures that the modification is applied early in the WordPress initialization process.

Please note that this code assumes the role "editor" already exists in your WordPress installation. Make sure to adjust the capabilities and custom post types according to your specific needs.

Register an account to save your snippets or go Pro to get more features.