Capability. Gestione nei custom post per i custom roles
<?php
//aggiungo al volo le cap giusto per gestire offerte e anagrafiche ai sal e all'admin
add_action( 'admin_init', 'addCapToSAL' );
function addCapToSAL() {
if(is_user_logged_in() ) {
//recupero dati utente corrente
$user_info = get_userdata(get_current_user_id());
$username = $user_info->data->user_login;
$identita = $user_info->roles[0];
//se l'utente appartiene ad uno dei seguenti ruoli aggiungi le seguenti cap
if($identita == 'operatore_sal' || $identita == 'administrator') {
$user->add_cap('edit_offerta');
$user->add_cap('read_offerta');
$user->add_cap('delete_offerta');
$user->add_cap('edit_offerte');
$user->add_cap('edit_other_offerte');
$user->add_cap('publish_offerte');
$user->add_cap('read_private_offerte');
$user->add_cap('read');
$user->add_cap('delete_offerte');
$user->add_cap('delete_private_offerte');
$user->add_cap('delete_published_offerte');
$user->add_cap('delete_others_offerte');
$user->add_cap('edit_private_offerte');
$user->add_cap('edit_published_offerte');
$user->add_cap('edit_offerte');
flush_rewrite_rules();
}
}
}
<?php//CUSTOM TYPE OFFERTE
add_action( 'init', 'salplesk_offerte_init' );
function salplesk_offerte_init() {
$args = array(
'labels' => array(
'name' => 'Offerte',
'singular_name' => 'Offerta',
'add_new' => 'Aggiungi Offerta',
'add_new_item' => 'Aggiungi Nuova Offerta',
'edit_item' => 'Modifica Offerta',
'new_item' => 'Nuova Offerta',
'all_items' => 'Vedi tutti le Offerte',
'view_item' => 'Vedi Offerte',
'search_items' => 'Cerca Offerta',
'not_found' => 'Nessuna Offerta',
'not_found_in_trash' => 'Nessuna Offerte nel cestino',
'parent_item_colon' => '',
'menu_name' => 'Offerte'
),
'public' => true,
'publicly_queryable' => true,
'menu_icon' => 'dashicons-admin-post',
'menu_position' => 10,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'offerte', 'with_front' => true ),
//abilito le cap
'map_meta_cap' => true,
//specifico i tipi singolari e plurali
'capability_type' => array('offerta','offerte'),
//specifico le corrispondenze
'capabilities' => array(
'edit_post' => 'edit_offerta',
'edit_posts' => 'edit_offerte',
'edit_others_posts' => 'edit_other_offerte',
'publish_posts' => 'publish_offerte',
'read_post' => 'read_offerta',
'read_private_posts' => 'read_private_offerte',
'delete_post' => 'delete_offerta'
),
'has_archive' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'author')
);
register_post_type( 'salplesk_offerte', $args );
}
?>
<?php
//stampa le cap relative ad un custom type specifico
print_rb($GLOBALS['wp_post_types']['salplesk_offerte']->cap);
?>