deazulturquesa
2/18/2020 - 9:41 AM

Permisos para el rol de editor

Añade permisos al rol de editor para editar theme options y cuentas de usuario pero evita crear cuentas de administrador

function add_theme_caps() {
    $role = get_role('editor');
    $role->add_cap( 'edit_theme_options' );
    $role->add_cap('list_users');
    $role->add_cap('create_users');
    $role->add_cap('delete_users');
    $role->add_cap('edit_users');
}
add_action( 'admin_init', 'add_theme_caps');

/**
 * Deny access to 'administrator' for other roles
 * Else anyone, with the edit_users capability, can edit others
 * to be administrators - even if they are only editors or authors
 * 
 * @since   0.1
 * @param   (array) $all_roles
 * @return  (array) $all_roles
 */
function deny_change_to_admin( $all_roles )
{
    if ( ! current_user_can('administrator') )
        unset( $all_roles['administrator'] );

    if ( 
        ! current_user_can('administrator')
        OR ! current_user_can('editor')
    )
        unset( $all_roles['editor'] );

    if ( 
        ! current_user_can('administrator')
        OR ! current_user_can('editor')
        OR ! current_user_can('author')
    )
        unset( $all_roles['author'] );

    return $all_roles;
}
function deny_rolechange()
{
    add_filter( 'editable_roles', 'deny_change_to_admin' );
}
add_action( 'after_setup_theme', 'deny_rolechange' );