-1

Hello everybody I've followed many guides around but my custom role user still can't manage my custom post type... Thanks in advance.

I've created a custom post type and a custom user role, and I wanted to add the capability to manage the custum post type to this new custom role. I've putted all inside WP plugin template files.

custom post plugin code here:


/* Creating a function to create our CPT */

function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Riparazioni', 'Post Type General Name', 'porto-child' ),
        'singular_name'       => _x( 'Riparazione', 'Post Type Singular Name', 'porto-child' ),
        'menu_name'           => __( 'Riparazioni', 'porto-child' ),
        'parent_item_colon'   => __( 'Riparazione parente', 'porto-child' ),
        'all_items'           => __( 'Tutte le Riparazioni', 'porto-child' ),
        'view_item'           => __( 'Visiona Riparazione', 'porto-child' ),
        'add_new_item'        => __( 'Aggiungi nuova riparazione', 'porto-child' ),
        'add_new'             => __( 'Aggiungi nuova', 'porto-child' ),
        'edit_item'           => __( 'Modifica riparazione', 'porto-child' ),
        'update_item'         => __( 'Aggiorna riparazione', 'porto-child' ),
        'search_items'        => __( 'Cerca riparazione', 'porto-child' ),
        'not_found'           => __( 'Non trovato', 'porto-child' ),
        'not_found_in_trash'  => __( 'Nessuna riparazione trovata nel cestino', 'porto-child' ),
    );

    $capabilities = array(
        'publish_posts' => 'publish_riparazioni',
        'edit_posts' => 'edit_riparazioni',
        'edit_others_posts' => 'edit_others_riparazioni',
        'delete_posts' => 'delete_riparazioni',
        'delete_others_posts' => 'delete_others_riparazioni',
        'read_private_posts' => 'read_private_riparazioni',
        'edit_post' => 'edit_riparazione',
        'delete_post' => 'delete_riparazione',
        'read_post' => 'read_riparazione',
    );   


// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'riparazioni', 'porto-child' ),
        'description'         => __( 'Gestione riparazioni', 'porto-child' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have Parent and child items. A non-hierarchical CPT is like Posts. */ 
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
        'capabilities' => $capabilities,
        'map_meta_cap'    => true,
    );

    // Registering your Custom Post Type
    register_post_type( 'riparazioni', $args );

}

/* Hook into the 'init' action so that the function * Containing our post type registration is not  * unnecessarily executed.  */

add_action( 'init', 'custom_post_type', 0 );

// ADD Custom meta capabilities --- Dario

add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 2 );

function my_map_meta_cap( $caps, $cap, $user_id, $args ) {

    /* If editing, deleting, or reading a riparazioni, get the post and post type object. */
    if ( 'edit_riparazioni' == $cap || 'delete_riparazioni' == $cap || 'read_riparazioni' == $cap ) {
        $post = get_post( $args[0] );
        $post_type = get_post_type_object( $post->post_type );

        /* Set an empty array for the caps. */
        $caps = array();
    }

    /* If editing a riparazioni, assign the required capability. */
    if ( 'edit_riparazioni' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->edit_posts;
        else
            $caps[] = $post_type->cap->edit_others_posts;
    }

    /* If deleting a riparazioni, assign the required capability. */
    elseif ( 'delete_riparazione' == $cap ) {
        if ( $user_id == $post->post_author )
            $caps[] = $post_type->cap->delete_posts;
        else
            $caps[] = $post_type->cap->delete_others_posts;
    }

    /* If reading a private riparazioni, assign the required capability. */
    elseif ( 'read_riparazioni' == $cap ) {

        if ( 'private' != $post->post_status )
            $caps[] = 'read';
        elseif ( $user_id == $post->post_author )
            $caps[] = 'read';
        else
            $caps[] = $post_type->cap->read_private_posts;
    }

    /* Return the capabilities required by the user. */
    return $caps;
}


// Rendo il post type Riparazioni privato === Dario
// rendo il post type privato di default
function force_type_private($post)
{
if ($post['post_type'] != 'riparazioni' || $post['post_status'] == 'trash')
return $post;
$post['post_status'] = 'private';
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');

function add_theme_caps() {
    // gets the administrator role
    $admins = get_role( 'administrator' );

    $admins->add_cap( 'edit_riparazione' ); 
    $admins->add_cap( 'edit_riparazioni' ); 
    $admins->add_cap( 'edit_other_riparazioni' ); 
    $admins->add_cap( 'publish_riparazioni' ); 
    $admins->add_cap( 'read_riparazione' ); 
    $admins->add_cap( 'read_private_riparazioni' ); 
    $admins->add_cap( 'delete_riparazione' );

    $super_editor = get_role( 'super_editor' );

    $super_editor->add_cap( 'edit_riparazione' ); 
    $super_editor->add_cap( 'edit_riparazioni' ); 
    $super_editor->add_cap( 'edit_other_riparazioni' ); 
    $super_editor->add_cap( 'publish_riparazioni' ); 
    $super_editor->add_cap( 'read_riparazione' ); 
    $super_editor->add_cap( 'read_private_riparazioni' ); 
    $super_editor->add_cap( 'delete_riparazione' ); 
}
add_action( 'admin_init', 'register_activation_hook');

?>

custom role plugin code here:

// aggiungo un nuovo ruolo personalizzato agli utenti --- Dario

// Add a custom user role

$result = add_role( 'super_editor', __('Super Editore'),

    array(

    'read' => true, // true allows this capability
    'edit_posts' => true, // Allows user to edit their own posts
    'edit_pages' => false,
    'edit_others_posts' => true, // Allows user to edit others posts not just their own
    'create_posts' => true, // Allows user to create new posts
    'manage_categories' => true, // Allows user to manage post categories
    'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode

    'publish_riparazioni' => true,
    'edit_riparazioni' => true,
    'edit_others_riparazioni' => true,
    'delete_riparazioni' => true,
    'delete_others_riparazioni' => true,
    'read_private_riparazioni' => true,
    'edit_riparazione' => true,
    'delete_riparazione' => true,
    'read_riparazione' => true,
    )
);

function add_super_editor_caps() {
    // get super editor role --- aggangia il ruolo di super editore --- Dario
    $super_editor = get_role( 'super_editor' );


    $super_editor->add_cap('moderate_comments');
    $super_editor->add_cap('manage_categories');
    $super_editor->add_cap('manage_links');
    $super_editor->add_cap('upload_files');

    // try to manage User (delete, add, edit) ad admin role --- Vorrei far gestire gli utenti come fosse un admin --- Dario

    $super_editor->add_cap('create_users');
    $super_editor->add_cap('edit_users');
    //$super_editor->add_cap('delete_users');
    $super_editor->add_cap('list_users');
    //$super_editor->add_cap('remove_users');
    $super_editor->add_cap('promote_users');

    $super_editor->add_cap( 'edit_riparazione' ); 
    $super_editor->add_cap( 'edit_riparazioni' ); 
    $super_editor->add_cap( 'edit_other_riparazioni' ); 
    $super_editor->add_cap( 'publish_riparazioni' ); 
    $super_editor->add_cap( 'read_riparazioni' ); 
    $super_editor->add_cap( 'read_private_riparazioni' ); 
    $super_editor->add_cap( 'delete_riparazioni' ); 
}
add_action( 'admin_init', 'add_super_editor_caps');



// failsafe function to remove role and flush WP database --- funzione per pulire il db dal ruolo creato --- Dario
//remove_role( 'super_editor' ); 
?>
  • Once you create a CPT and capabilities you may need to use unregister_post_type() right before you re-create the CPT, and also remove_role() right before you re-create the Super Editor role. This will force WP to use your new values rather than keeping old ones before you had capabilities all mapped out correctly. Also make sure to clear your server cache, hit the permalinks page, try an Incognito window or clear your browser history to make sure you are seeing the latest results. – WebElaine Dec 15 '17 at 20:43
  • Thanks for your help @WebElaine now my custom role can create custom post and assign them to other user but once created the custom post can't be edited or deleted. I don't know why... – Dario Filidei Dec 18 '17 at 9:52
  • @WebElaine I've cleaned the code making new plugins, one for the custom post type and one for the custom user role, everything worked fine in exception of the custom capabilities. There was no chance to make work the custom capabilities from my custom post type for the custom role. I had to install User Role Editor plugin after a morning of tests. – Dario Filidei Dec 19 '17 at 9:11
  • The only other thing I can think to change would be to shorten your capability names. The permissions like delete_others_riparazioni may be too long - you could go back and try delete_others_ripi for short and see if that makes a difference. But User Role Editor is a well-known and supported plugin. – WebElaine Dec 19 '17 at 14:16

Your Answer

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

Browse other questions tagged or ask your own question.