I have a custom post type that is created using a form on the site. I want to disable new post creation from wordpress admin. Is there a way to do that ?
3 Answers
There are several ways (depending on what you're trying to do):
If it's about the "publish" button
You could…
- …remove the capability from the targeted role with
remove_cap()
…remove the publish button 1)
1) see bottom of the answer by @toscho
…or the whole meta box 2)
remove_meta_box( 'submitdiv', 'custom_post_id', 'side' );
2) simplified version
If it's about the built in `post` post type
You could…
- …simply hide the menu entry via css or js
…unset the menu entry with
add_action( 'admin_menu', 'myprefix_adjust_the_wp_menu', 999 ); function myprefix_adjust_the_wp_menu() { $page = remove_submenu_page( 'edit.php', 'post-new.php' ); //or for custom post type 'myposttype'. //$page = remove_submenu_page( 'edit.php?post_type=myposttype', 'post-new.php?post_type=myposttype' ); }
…prevent saving at all with the
per_save_post
hook and$_GET['action']
- …do a redirect when the
post-new.php
is load
There is a meta capability create_posts
that is not documented but is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities
(not to be confused with cap
) and then set it to false
as below.
register_post_type( 'custom_post_type_name', array(
'capabilities' => array(
'create_posts' => false,
)
));
-
This has now changed to: ` 'capabilities' => [ 'create_posts' => 'do_not_allow' ], 'map_meta_cap' => true, ` Jul 28, 2015 at 21:16
@ hannit cohen,
This is best way to hide the Add New or Create New button in custom postypes
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
It disable to create new post in custom post types both side in admin menu and above the list of post type.