0

i've create a custom user role using this code:

    /* aggiungi ruolo */
add_role('brokers', 'Brokers', array(
'read' => true, // True allows that capability
'edit_pages' => true,
'edit_others_pages' => true,
'edit_published_pages' => true,
'publish_pages' => true,
));

Then i've create two CPT using this code

// Sinistri
add_action( 'init', 'register_cpt_sinistri' );
function register_cpt_sinistri() {
$labels = array( 
    'name' => _x( 'Sinistri', 'sinistri' ),
    'singular_name' => _x( 'Sinistro', 'sinistri' ),
    'add_new' => _x( 'Aggiungi Sinistro', 'sinistri' ),
    'add_new_item' => _x( 'Aggiungi Nuovo Sinistro', 'sinistri' ),
    'edit_item' => _x( 'Modifica Sinistro', 'sinistri' ),
    'new_item' => _x( 'Nuovo Sinistro', 'sinistri' ),
    'view_item' => _x( 'Guarda Sinistro', 'sinistri' ),
    'search_items' => _x( 'Cerca Sinistri', 'sinistri' ),
    'not_found' => _x( 'Nessun Sinistro Trovato', 'sinistri' ),
    'not_found_in_trash' => _x( 'Sinistri non trovati', 'sinistri' ),
    'parent_item_colon' => _x( 'Parent Sinistro:', 'sinistri' ),
    'menu_name' => _x( 'Gestione Sinistri', 'gallery' ),
);
$args = array( 
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'Image galleries for teachers classes',
    'supports' => array( 'title', 'editor', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => true,
    'has_archive' => false,
    'query_var' => true,
    'can_export' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'capabilities' => array(
        'edit_post' => 'edit_sinistri',
        'edit_posts' => 'edit_sinistris',
        'edit_others_posts' => 'edit_other_sinistris',
        'publish_posts' => 'publish_sinistris',
        'read_post' => 'read_sinistri',
        'read_private_posts' => 'read_private_sinistris',
        'delete_post' => 'delete_sinistri'
    ),
    // as pointed out by iEmanuele, adding map_meta_cap will map the meta correctly 
    'map_meta_cap' => true
);
register_post_type( 'sinistri', $args );
flush_rewrite_rules();
}

and this code:

//Area Clienti Riservata
add_action( 'init', 'register_cpt_area_clienti' );
function register_cpt_area_clienti() {
    $labels = array( 
        'name' => _x( 'Area Clienti', 'area_clienti' ),
        'singular_name' => _x( 'Area Clienti', 'area_clienti' ),
        'add_new' => _x( 'Add New', 'area_clienti' ),
        'add_new_item' => _x( 'Add New Area Clienti', 'area_clienti' ),
        'edit_item' => _x( 'Edit Area Clienti', 'area_clienti' ),
        'new_item' => _x( 'New Area Clienti', 'area_clienti' ),
        'view_item' => _x( 'View Area Clienti', 'area_clienti' ),
        'search_items' => _x( 'Search Area Clienti', 'area_clienti' ),
        'not_found' => _x( 'No area clienti found', 'area_clienti' ),
        'not_found_in_trash' => _x( 'No area clienti found in Trash', 'area_clienti' ),
        'parent_item_colon' => _x( 'Parent Area Clienti:', 'area_clienti' ),
        'menu_name' => _x( 'Area Clienti', 'area_clienti' ),
    );
    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Gestione clienti agenzia assicurativa',
        'supports' => array( 'title', 'editor' ),

        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,

        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => true,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capabilities' => array(
        'edit_post' => 'edit_sinistri',
        'edit_posts' => 'edit_sinistris',
        'edit_others_posts' => 'edit_other_sinistris',
        'publish_posts' => 'publish_sinistris',
        'read_post' => 'read_sinistri',
        'read_private_posts' => 'read_private_sinistris',
        'delete_post' => 'delete_sinistri'
    ),
    );
    register_post_type( 'area_clienti', $args );
    flush_rewrite_rules();
}

Then i've setting the user role with this code:

function add_theme_caps() {
    // gets the administrator role
    $roles = get_role( 'brokers');
    $roles->add_cap( 'edit_sinistri' ); 
    $roles->add_cap( 'edit_sinistris' ); 
    $roles->add_cap( 'edit_other_sinistris' ); 
    $roles->add_cap( 'publish_sinistris' ); 
    $roles->add_cap( 'read_sinistri' ); 
    $roles->add_cap( 'read_private_sinistris' ); 
    $roles->add_cap( 'delete_sinistri' ); 

    $admins = get_role( 'brokers','administrator' );
    $admins->add_cap( 'edit_area_clienti' ); 
    $admins->add_cap( 'edit_area_clientis' ); 
    $admins->add_cap( 'edit_other_area_clientis' ); 
    $admins->add_cap( 'publish_area_clientis' ); 
    $admins->add_cap( 'read_area_clienti' ); 
    $admins->add_cap( 'read_private_area_clientis' ); 
    $admins->add_cap( 'delete_area_clienti' ); 
}
add_action( 'admin_init', 'add_theme_caps');

But i've a problem. I can see the CPT only with brokers role and not also with administrator role. How can i see the two CPT with brokers and administrator roles? Thanks

0

$admins = get_role( 'brokers','administrator' ); part doesn't make sense.

get_role() only accepts one role name and only ever returns one role. You need to make these changes to the two roles separately.

  • Thnk's much. Now i have an other problem. Everyone can access on frontend at the post of custom post tye. I try to use the UAM plugin to setting private all post but i not have a result. How can i setting that only subscriber user can access and read the post of custom post type? (on frontend)? – skifast Aug 20 '14 at 17:17
  • I try to setup the post ad private post and add this code '$subrole = get_role( 'subscriber' ); // For private pages $subrole->add_cap( 'read_private_area_clientis' );' but if i try to see the page ad subscriber i have a 404 error :( – skifast Aug 20 '14 at 17:56
  • @Federica please ask new question if you have one :) Comments aren't meant for such. – Rarst Aug 20 '14 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.