1

I have created custom post type as below.

register_post_type( 'connector',
    array(
        'labels' => array(
            'name' => __( 'Connectors' ),
            'singular_name' => __( 'Connector' ),
            'all_items' => __( 'All Connectors' ),
            'add_new' => __( 'Add Connector' ),
            'add_new_item' => __( 'Add New Connector' ),
            'edit' => __( 'Edit'),
            'edit_item' => __( 'Edit Connector' ),
            'new_item' => __( 'New Connector'),
            'view' => __( 'View Connector'),
            'view_item' => __( 'View Connector'),
            'search_items' => __( 'Search Connectors'),
            'not_found' => __( 'No Connectors found' ),
            'not_found_in_trash' => __( 'No Connectors found in trash' ),
            'parent' => __( 'Parent Connector')
        ),      

    'description' => __( 'This is where you can add new Connectors' ),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'page',        
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'hierarchical' => true,
    'rewrite' => array( 'slug' => 'connector', 'with_front' => false ),
    'query_var' => true,
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'excerpt','author',/*, 'page-attributes'*/ ),
    'taxonomies' => array('post_tag'), // this is IMPORTANT for adding tags
    'has_archive' => true,
    'show_in_nav_menus' => true,
    'show_in_menu'=>false
    )
);

and then further used into my website and having large number of authors.

Now, I want to add custom capabilities to my custom post type - 'connector'. I have followed answer from Change custom post type to hierarchical after being registered

function modify_connectors() {
    if ( post_type_exists( 'connector' ) ) {


$capabilities = array(
        'publish_posts' => 'publish_connectors',
        'edit_posts' => 'edit_connectors',
        'edit_others_posts' => 'edit_others_connectors',
        'delete_posts' => 'delete_connectors',
        'delete_others_posts' => 'delete_others_connectors',
        'read_private_posts' => 'read_private_connectors',
        'edit_post' => 'edit_connector',
        'delete_post' => 'delete_connector',
        'read_post' => 'read_connector',
    );


    global $wp_post_types, $wp_rewrite;
    $wp_post_types['connector']->hierarchical = true;
    $wp_post_types['connector']->capability_type = 'connector';
    $wp_post_types['connector']->capabilities = $capabilities;
    //I am not sure from nowonwards am I doing right or wrong
    $args = $wp_post_types['connector'];
    $wp_rewrite->add_rewrite_tag("%connector%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=connector&name=");
    add_post_type_support('connector','page-attributes');
    }
}
add_action( 'init', 'modify_connectors', 1 );

I can't find any custom capabilities under "User Role Editor" menu related to connector. Is this something possible to add custom capabilities after register post type has been completed?

0

Have you tried something like this:

'description' => __( 'This is where you can add new Connectors' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'page',        
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => true,
'capabilities'        => array(
'publish_posts'       => 'update_core',
'edit_others_posts'   => 'update_core',
'delete_posts'        => 'update_core',
'delete_others_posts' => 'update_core',
'read_private_posts'  => 'update_core',
'edit_post'           => 'edit_posts',
'delete_post'         => 'update_core',
'read_post'           => 'edit_posts',
),
'rewrite' => array( 'slug' => 'connector', 'with_front' => false ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'excerpt','author',/*, 'page-attributes'*/ ),
'taxonomies' => array('post_tag'), // this is IMPORTANT for adding tags
'has_archive' => true,
'show_in_nav_menus' => true,
'show_in_menu'=>false
));
| improve this answer | |
  • Added capabilities with update_core but it does nothing. I can't find new custom capabilities in user role editor. Am I doing something wrong? – Kamal Joshi Feb 27 '14 at 13:12
  • You can create a new role with custom capabilities and then add the capability to the code above. Not difficult. codex.wordpress.org/Function_Reference/add_role – Brad Dalton Feb 27 '14 at 14:58
  • Yes, that's correct.. Also, I can add custom capabilities using user role editor. – Kamal Joshi Feb 28 '14 at 6:18

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.