re-register custom post type with custom capabilities

admin2025-01-07  4

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?

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?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 27, 2014 at 11:39 Kamall A JoshiKamall A Joshi 1157 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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
));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261873a774.html

最新回复(0)