By kristina childs


2012-12-04 01:36:13 8 Comments

I have a custom role that allows access to only a sigle custom post type. This is all well and good, but now it is only showing up for this role, not for admin and super admin. I'm having a hard time getting it to show up in the admin dashboard.

add_role( 'artists_relations', 'Artist Relations', array( 'post_artists' ) );
function add_theme_caps() {
    $role = get_role( 'artists_relations');
    $role->add_cap('delete_artists');
    $role->add_cap('delete_published_artists');
    $role->add_cap('delete_others_artists');
    $role->add_cap('edit_artists');
    $role->add_cap('edit_published_artists');
    $role->add_cap('edit_others_artists');
    $role->add_cap('publish_artists');
    $role->add_cap('read');
    $role->add_cap('upload_files');
    $role->add_cap('manage_artist_categories');
}
add_action( 'admin_init', 'add_theme_caps');

works great. I tried many suggestions from the internet, but none worked. The latest attempt, though, looked like this

function add_admin_caps() {
    $role = get_role( 'administrator');
    $role->add_cap('delete_artists');
    $role->add_cap('delete_published_artists');
    $role->add_cap('delete_others_artists');
    $role->add_cap('edit_artists');
    $role->add_cap('edit_published_artists');
    $role->add_cap('edit_others_artists');
    $role->add_cap('publish_artists');
    $role->add_cap('read');
    $role->add_cap('upload_files');
    $role->add_cap('manage_artist_categories');
}
add_action( 'admin_init', 'add_admin_caps');

I've read a few posts that mentioned removing filters. I don't understand why this would work for a new role but not let me augment an existing one. But isn't admin supposed to have rights to everything without having to specify it anyway?

-- EDIT -- (adding post type registration)

function register_artists_post_type() {
    register_post_type('artists',array(
        'labels' => array(
            'name' => __( 'Artists' ),
            'singular_name' => __( 'Artists' ),
            'add_new' => __( 'Add Artist','Artist' ),
            'add_new_item' => __( 'Add New Artist' ),
            'edit_item' => __( 'Edit Artist' ),
            'new_item' => __( 'New Artist' ),
            'view_item' => __( 'View Artist' ),
            'search_items' => __( 'Search Artists' ),
            'not_found' => __( 'No Artists Found' ),
            'not_found_in_trash' => __( 'No Artists In Trash' ),
            'parent_item_colon' => ''
        ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive' => true,
        'supports' => array( 'title','editor','excerpt','custom-fields','thumbnail' ),
        'rewrite' => array('slug' => 'artists','with_front' => false),
        'taxonomies' => array('large_feature','small_feature'),
        'capability_type' => 'post',
        'hierarchical' => false,
        'capabilities' => array(
            'publish_posts' => 'publish_artists',
            'edit_posts' => 'edit_artists',
            'edit_others_posts' => 'edit_others_artists',
            'delete_posts' => 'delete_artists',
            'delete_others_posts' => 'delete_others_artists',
            'read_private_posts' => 'read_private_artists',
            'edit_post' => 'edit_artists',
            'delete_post' => 'delete_artists',
            'read_post' => 'read_artists',
            'manage_categories' => 'manage_artist_categories',
        )
    ));
}
add_action('init','register_artists_post_type');

1 comments

@Matthew Boynes 2012-12-04 04:10:15

First off, @Wyck is right, it would be helpful to see your register_post_type code. If it were me, I'd make sure I had something like this in the $args array:

register_post_type( ... array(
    'capability_type' => 'artists',
    'map_meta_cap' => true
) );

Next, you don't want to reset capabilities on every admin page load, that's unnecessary work for your server. A simple trick I employ is to add ?reload_caps=1 after /wp-admin/ and check for that in my theme's functions.php file. Here's some code I used successfully on a site, modified to use the role artists_relations and capability type artists:

if ( is_admin() && '1' == $_GET['reload_caps'] ) {
    $administrator     = get_role('administrator');
    $artists_relations = get_role('artists_relations');

    $administrator->add_cap( 'assign_custom_taxes' );
    $artists_relations->add_cap( 'assign_custom_taxes' );

    foreach ( array('publish','delete','delete_others','delete_private','delete_published','edit','edit_others','edit_private','edit_published','read_private') as $cap ) {
        $administrator->add_cap( "{$cap}_artists" );
        $artists_relations->add_cap( "{$cap}_artists" );
    }
}

@kristina childs 2012-12-04 17:42:46

Thanks, I've added the registration. I guess I didn't wholly grasp what the capability_type really meant. I thought if it was post then it would behave like a post. Re-reading the codex just makes me more confused :( Unfortunately, though, making that change didn't cause it to show up in admin view.

@kristina childs 2012-12-04 17:44:16

Also, what do you mean by reset capabilities? What in this code "resets" it?

@Matthew Boynes 2012-12-04 17:47:16

Running add_cap is only necessary to do once, not on every page load. So what we've done here is, if reload_caps=1 is in the URL, we're running all the commands. Otherwise, we're not running them unnecessarily. So you added all this, and then went to http://your-site.com/wp-admin/?reload_caps=1?

@kristina childs 2012-12-04 20:42:44

Yes, and still no artist tab in the sidebar. Blarg.

@kristina childs 2012-12-04 22:17:57

Nevermind, the file wasn't updating for some reason! Yes, this works beautifully, thank you so much. For anyone else coming across this, all you need is this code and the single line add_role( 'artists_relations', 'Artist Relations', array( 'post_artists' ) ); to register the role. No capabilities array in register_post_type and add_theme_caps isn't needed at all. Again, Matthew, thank you for such an elegant solution! Best I've seen yet. I did have to add read to the array, though :)

@kristina childs 2012-12-04 22:40:26

Also, to add capabilities to the custom taxonomy I added 'capability_type' => 'artist_cat' to my taxonomy reg and a new foreach array for the other capability_type

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How to update role capabilities

1 Answered Questions

[SOLVED] Adding an additional role to an Administrator

1 Answered Questions

2 Answered Questions

[SOLVED] Administrator role capabilities

  • 2014-02-12 15:22:31
  • andys
  • 317 View
  • 0 Score
  • 2 Answer
  • Tags:   capabilities

1 Answered Questions

[SOLVED] Role Capabilities: Add New Ones?

Sponsored Content