Creating a custom post type, adding custom meta fields, preventing all future editability of posts of this type
I am new to plug-in development and have ran into a bit of a speed bump.
So here are my criteria, I am pretty much struggling to tick off any of them:
I wish to create a custom post type ‘website’ – This bit I can do!
I require only three fields, two of which are custom meta fields (title, website_url & website_source_code) – The extra fields, I believe are being created!
I require an admin menu tab which will link to the list of websites – Doesn’t show when using the extended code below.
I require all forms of editing to be removed, however, still be able to click into each entry and view its contents (without being able to edit or update) – I think this is where the meta_cap features comes in, don’t know how to apply it though.
Here is the full code I am using to declare my custom post type:
// Create Custom Post Type
protected function create_post_type() {
add_action('init', array($this, 'custom_post_type'));
add_action('admin_init', array($this, 'custom_post_type_caps'));
}
// Register Websites Custom Post Type
function custom_post_type() {
/*$labels = array(
'name' => __( 'Websites', 'website' ),
'singular_name' => __( 'Website', 'website' ),
'add_new' => __( 'Add New', 'website' ),
'add_new_item' => __( 'Add New Website', 'website' ),
'edit_item' => __( 'Edit Website', 'website' ),
'new_item' => __( 'New Website', 'website' ),
'view_item' => __( 'View Website', 'website' ),
'search_items' => __( 'Search Websites', 'website' ),
'not_found' => __( 'No websites found', 'website' ),
'not_found_in_trash' => __( 'No websites found in Trash', 'website' ),
'parent_item_colon' => __( 'Parent Website:', 'website' ),
'menu_name' => __( 'Websites', 'website' ),
);*/
$args = array(
//'labels' => $labels,
'label' => 'Websites',
//'hierarchical' => true,
'description' => 'List of websites',
'supports' => array( 'title', 'website_url', 'website_source_code'),
'public' => true,
//'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-admin-site-alt3',
//'show_in_nav_menus' => true,
//'publicly_queryable' => true,
//'exclude_from_search' => false,
//'has_archive' => true,
//'query_var' => true,
//'can_export' => true,
'rewrite' => false,
'capabilities' => array(
'create_posts' => false,
'edit_post' => 'edit_website',
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'read_post' => 'read_website',
'read_private_posts' => false,
'delete_post' => 'delete_website'
),
'map_meta_cap' => true
);
register_post_type('website', $args);
//register_post_type('website', ['public' => true, 'label' => 'Websites']);
}
function custom_post_type_caps() {
// gets the administrator role
$admins = get_role('administrator');
$admins->add_cap('edit_website');
$admins->add_cap('read_website');
$admins->add_cap('delete_website');
}
A lot of things are commented out as I know that it works, the posts are created and can be accessed in a wp_query loop. I am just unable to get any other of the requirements on the list above ticked off so was attempting to dumb the code down a bit in the hopes I had just overcomplicated it.
If I use this line:
register_post_type('website', ['public' => true, 'label' => 'Websites']);
The post type is created and a menu tab appears in the admin left hand menu. But then I am unable to add any other criteria from the list above.
I have read and appreciate there are numerous other similar posts, but none are quite like this one, the combination of requirements are unique.
Leave an answer