get_role example


function populate_roles_160() {
    // Add roles.     add_role( 'administrator', 'Administrator' );
    add_role( 'editor', 'Editor' );
    add_role( 'author', 'Author' );
    add_role( 'contributor', 'Contributor' );
    add_role( 'subscriber', 'Subscriber' );

    // Add caps for Administrator role.     $role = get_role( 'administrator' );
    $role->add_cap( 'switch_themes' );
    $role->add_cap( 'edit_themes' );
    $role->add_cap( 'activate_plugins' );
    $role->add_cap( 'edit_plugins' );
    $role->add_cap( 'edit_users' );
    $role->add_cap( 'edit_files' );
    $role->add_cap( 'manage_options' );
    $role->add_cap( 'moderate_comments' );
    $role->add_cap( 'manage_categories' );
    $role->add_cap( 'manage_links' );
    $role->add_cap( 'upload_files' );
    
$wp_roles = wp_roles();

        // Filter out caps that are not role names and assign to $this->roles.         if ( is_array( $this->caps ) ) {
            $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
        }

        // Build $allcaps from role caps, overlay user's $caps.         $this->allcaps = array();
        foreach ( (array) $this->roles as $role ) {
            $the_role      = $wp_roles->get_role( $role );
            $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
        }
        $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );

        if ( $switch_site ) {
            restore_current_blog();
        }

        return $this->allcaps;
    }

    


/** * Retrieves role object. * * @since 2.0.0 * * @param string $role Role name. * @return WP_Role|null WP_Role object if found, null if the role does not exist. */
function get_role( $role ) {
    return wp_roles()->get_role( $role );
}

/** * Adds a role, if it does not exist. * * @since 2.0.0 * * @param string $role Role name. * @param string $display_name Display name for role. * @param bool[] $capabilities List of capabilities keyed by the capability name, * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). * @return WP_Role|void WP_Role object, if the role is added. */
$query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;

        if ( isset( $filter['orderby'] ) ) {
            $query['orderby'] = $filter['orderby'];

            if ( isset( $filter['order'] ) ) {
                $query['order'] = $filter['order'];
            }
        }

        if ( isset( $filter['role'] ) ) {
            if ( get_role( $filter['role'] ) === null ) {
                return new IXR_Error( 403, __( 'Invalid role.' ) );
            }

            $query['role'] = $filter['role'];
        }

        if ( isset( $filter['who'] ) ) {
            $query['who'] = $filter['who'];
        }

        $users = get_users( $query );

        
&& '' !== $value && ! preg_match( '/%[^\/%]+%/', $value )
            ) {
                $error = sprintf(
                    /* translators: %s: Documentation URL. */
                    __( 'A structure tag is required when using custom permalinks. <a href="%s">Learn more</a>' ),
                    __( 'https://wordpress.org/documentation/article/customize-permalinks/#choosing-your-permalink-structure' )
                );
            }
            break;

        case 'default_role':
            if ( ! get_role( $value ) && get_role( 'subscriber' ) ) {
                $value = 'subscriber';
            }
            break;

        case 'moderation_keys':
        case 'disallowed_keys':
            $value = $wpdb->strip_invalid_text_for_column( $wpdb->options, 'option_value', $value );
            if ( is_wp_error( $value ) ) {
                $error = $value->get_error_message();
            } else {
                $value = explode( "\n", $value );
                
Home | Imprint | This part of the site doesn't use cookies.