• Resolved juliamb

    (@juliamb)


    Hi!

    I’m trying to create a role in WordPress that will only work (create, edit, delete) with a custom post type that I’ve created (Mapas).
    The problem is that when I log in to the site with a user that has assigned that role, I cannot enter the dashboard to edit the posts of that CPT. I can see the admin bar in the top of the page but if I clic on the dashboard link or inside Add > Media it is loading every time to the same page, the published page, not loading the admin area. It’s like I have not permission to enter the dashboard.

    Here’s the code I’m using:

    <?php
    
    // New CPT Mapas
    function cpt_maps_init() {
    	$labels = array(
    		'name'               => _x( 'Mapas', 'post type general name', 'wow' ),
    		'singular_name'      => _x( 'Mapa', 'post type singular name', 'wow' ),
    		'menu_name'          => _x( 'Cartoteca', 'admin menu', 'wow' ),
    		'name_admin_bar'     => _x( 'Mapa', 'add new on admin bar', 'wow' ),
    		'add_new'            => _x( 'Añadir nuevo', 'book', 'wow' ),
    		'add_new_item'       => __( 'Añadir nuevo mapa', 'wow' ),
    		'new_item'           => __( 'Nuevo mapa', 'wow' ),
    		'edit_item'          => __( 'Editar mapa', 'wow' ),
    		'view_item'          => __( 'Ver mapa', 'wow' ),
    		'all_items'          => __( 'Todos los mapas', 'wow' ),
    		'search_items'       => __( 'Buscar mapas', 'wow' ),
    		'parent_item_colon'  => __( 'Mapas superiores:', 'wow' ),
    		'not_found'          => __( 'No se han encontrado mapas.', 'wow' ),
    		'not_found_in_trash' => __( 'No se han encontrado mapas en la papelera.', 'wow' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
        'description'        => __( 'Colección de mapas', 'wow' ),
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'mapas' ),     			            
    		'capability_type'     => array('mapas', 'mapa'),
            'map_meta_cap'        => true,
    		'has_archive'        => true,
    		'hierarchical'       => false,
    		'menu_position'      => null,
    		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions', 'custom-fields' ),
    		'menu_icon'				=> 'dashicons-admin-site',
    		'taxonomies'  			=> array( 'category', 'post_tag', 'lugar' )
    	);
    	
    	register_post_type( 'map', $args );
    }
    add_action( 'init', 'cpt_maps_init' );
    
    // New role Cartógrafo
    function add_cartografo_role() {
    	add_role('cartografo',
    		'Cartógrafo',
    		array(
    		'read' => true,
    		'edit_posts' => false,
    		'delete_posts' => false,
    		'publish_posts' => false,
    		'upload_files' => true,
    		)
    	);
    }
    add_action('admin_init', 'add_cartografo_role' );
    
    // Add capabilities to role
    function add_maps_role_caps() {
        // Add the roles you'd like to administer the custom post types
        $roles = array('editor','administrator', 'cartografo');
    
        // Loop through each role and assign capabilities
        foreach($roles as $the_role) { 
    
            $role = get_role($the_role);
    
            $role->add_cap( 'read' );
            $role->add_cap( 'read_mapas');
            $role->add_cap( 'read_private_mapas' );
            $role->add_cap( 'edit_mapas' );
            $role->add_cap( 'edit_others_mapas' );
            $role->add_cap( 'edit_published_mapas' );
            $role->add_cap( 'publish_mapas' );
            $role->add_cap( 'delete_others_mapas' );
            $role->add_cap( 'delete_private_mapas' );
            $role->add_cap( 'delete_published_mapas' );
     }
    }
    add_action('admin_init','add_maps_role_caps',999);

    I’ve tried changing post capabilities when creating the role to true, and that way I can access the dashboard but I can edit any type of post and that is not what I want, I just only need access to edit Mapas posts.

    Can anyone help me? Thanks!

    • This topic was modified 4 years, 1 month ago by juliamb.
    • This topic was modified 4 years, 1 month ago by juliamb.
    • This topic was modified 4 years, 1 month ago by juliamb.
Viewing 5 replies - 1 through 5 (of 5 total)
  • I’m not certain of the answer, but let me just say what I see so far.
    The post type has show_ui=true, but menu_position=null.
    The capabilities only need to be done once, as they are stored in the database.
    https://developer.wordpress.org/reference/classes/wp_roles/add_cap/

    The core capability checks are for the generic ‘edit_posts’, ‘delete_posts’, etc. But the custom capability is supposed to map to that. By using map_meta_cap=true it’s unclear if the plurals are mapped to the singulars or if the custom are mapped to the generic.

    Thread Starter juliamb

    (@juliamb)

    Hi Joy, thank you so much for your answer!

    I’ve added the menu_postion when registering the CPT and also changed the capanility_type argument to just ‘mapa’ (since the plural is the same adding just the ‘s’ I guess I can leave with just the singular, right?).
    But I’m still not able to access the Dashboard or to Add a Mapa CPT, although I’m seeing the option Add > Mapa in the admin bar.

    I really don’t know what am I missing…

    Thread Starter juliamb

    (@juliamb)

    I’ve tried creating another role with just read capability set to true (according to the documentation this should give access to the dashboard and the user profile, right?) and not associated with the CPT Mapas and I can’t access to the Dashboard or the profile, it’s redirecting me to the same page. Maybe the problem is something not related to the CPT…

    Moderator bcworkz

    (@bcworkz)

    Right, adding a user with a role with only “read” capability should give the user access to dashboard and profile. Try disabling plugins and switching to a default theme. The user should have access then. Restore your configuration to narrow down where the issue is coming from.

    You could ensure a role has full capabilities for your CPT by executing

    global $wp_post_types;
    print_r($wp_post_types['map']->cap);

    and verifying the role has all the same capabilities listed in the output.

    Thread Starter juliamb

    (@juliamb)

    Thanks for the answer. I followed your steps and found out the problem, which was a plugin installed on the website.

    Thanks again for taking the time to check my problem and answer me, I really appreciate it 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Creating a role to edit a specific CPT’ is closed to new replies.