1. Go to this page and download the library: Download devgeniem/wp-geniem-roles library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
devgeniem / wp-geniem-roles example snippets
\Geniem\Roles::reset_roles();
\Geniem\Roles::reset_to_default_roles();
// If wp-geniem-roles is active.
if ( class_exists( '\Geniem\Roles' ) ) {
// Run Geniem roles functions here.
}
/**
* Create a new role
*/
// Caps to be added to the new role
// all caps default to false see the details plugin.php \Geniem\Role::get_default_caps()
$new_role_caps = array(
"activate_plugins" => true,
"delete_others_pages" => true,
"delete_others_posts" => true,
);
// Create a new role "testrole" with wanted capabilities
$new_role = \Geniem\Roles::create( 'new_role', __( 'New role', 'theme-text-domain' ), $new_role_caps );
// Check if role throws a WordPress error
if ( is_wp_error( $new_role ) ) {
error_log( $new_role->get_error_messages() );
}
// Gets a role instance of \Geniem\Roles
$admin = \Geniem\Roles::get( 'administrator' );
// Define desired capabilities for a role 'administrator'
$admin_caps = [
'geniem_roles'
];
// add_caps takes an array off capabilities
$admin->add_caps( $admin_caps );
// Define removable caps in an array
$admin_removable_caps = [
'edit_users',
'delete_users',
'create_users'
];
// Run function remove_caps for desired wp role
$admin->remove_caps( $admin_removable_caps );
// Get a role.
$author = \Geniem\Roles::get( 'author' );
// If role is removed successfully this will cause an error on the next page load.
if ( $author ) {
// Remove a role
$author->remove();
}
// Rename a role
$author->rename( 'New name' );
/**
* Define removable admin pages array
*
* note: Most of the plugins can be removed by plugin page slug.
* Geniem roles lists all menu slugs in the admin page [Geniem Roles -> Menu slugs].
*
*/
$admin_removable_admin_pages = [
'edit.php', // Posts
'edit.php?post_type=page', // Pages
'edit.php?post_type=acf-field-group', // Advanced Custom Fields
'plugin_page_slug', // Remove plugin page by plugin page slug
// You can also remove submenu pages like so.
'themes.php' => [
'themes.php',
// Note geniem roles has automated feature to remove 'customize.php' with ease.
// No need for usually
/**
* Define removable admin bar nodes.
*/
$admin_removable_admin_bar_nodes = [
'new-post', // Default post type 'post' add new button.
];
$admin->remove_admin_bar_nodes( $admin_removable_admin_bar_nodes );
/**
* All possible restricted capabilities are listed below
* https://codex.wordpress.org/Function_Reference/map_meta_cap
*/
'delete_page'
'delete_post'
'delete_user'
'edit_comment' (3.1. Mapped to 'edit_post' meta capability.)
'edit_page'
'edit_post'
'edit_user'
'publish_post' (3.5)
'promote_user'
'read_post'
'read_page'
'remove_user'
// Define restricted post IDs
$frontpage_id = (int) get_option( 'page_on_front' );
$restricted_posts = [
$frontpage_id,
2
];
// Restricted capabilities for the post.
$capabilities = [
'delete_post',
'edit_post',
];
$admin->restrict_post_edit( $restricted_posts, $capabilities );
/**
* All possible restricted capabilities are listed below
* https://codex.wordpress.org/Function_Reference/map_meta_cap
*/
'delete_page'
'delete_post'
'delete_user'
'edit_comment' (3.1. Mapped to 'edit_post' meta capability.)
'edit_page'
'edit_post'
'edit_user'
'publish_post' (3.5)
'promote_user'
'read_post'
'read_page'
'remove_user'
// Define restricted post IDs
$frontpage_id = (int) get_option( 'page_on_front' );
$granted_post_ids = [
$frontpage_id,
2
];
// Allow edit_post cap for granted posts.
$granted_posts_caps = [
'edit_post',
];
// If empty all caps will be blocked.
// Here we are allowing delete_post cap for other posts than $granted_post_ids. (optional)
$restricted_posts_caps = [
'delete_post',
];
// Post types to restrict. If defined other post types won't be handled. (optional)
$post_types = [ 'page' ];
$admin->grant_post_edit( $granted_post_ids, $granted_posts_caps, $restricted_posts_caps, $post_types );
/**
* Associative array of removed caps by the role.
* $key = User role to be restricted from the role.
* $value = Array of removed caps from the role.
*
* All possible user editing caps has been listed below:
* 'edit_user'
* 'delete_user'
* 'promote_user'
* 'remove_user'
*/
$custom_role_restricted_user_roles = [
// Role
'administrator' => [
// Removed caps.
'edit_user',
'delete_user',
'remove_user',
],
// Another role
'editor' => [
'delete_user',
],
];
$custom_role->restrict_user_management_by_role( $custom_role_restricted_user_roles );
// Example 1
// Allow only default template for the user.
$role->restrict_role_templates( 'default' );
// Example 2 allow
// page-frontpage.php and default template for the user.
$allowed_templates = [
'page-frontpage.php',
];
$role->restrict_role_templates( $allowed_templates );
/**
* Filter wp-geniem-roles default capabilities for the new role.
*
* @return array An array of capabilities to be added to the new role automatically.
*/
function filter_geniem_roles_default_caps( $default_caps ) {
$default_caps = [
'edit_posts' => true,
];
return $default_caps;
}
\add_filter( 'geniem/roles/default_roles', 'filter_geniem_roles_default_caps' );
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.