Creating a role to edit a specific CPT
-
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!
- The topic ‘Creating a role to edit a specific CPT’ is closed to new replies.