WordPress article types distinguish different content into different content objects, which facilitates our management in the background. Some content submitted by users on the front end can also be written directly into a certain article type and then maintained and managed in the background. At this time, we may need to prohibit administrators from creating new content in the background to ensure that these contents are submitted by users on the front end. We can disable the user’s ability to create content of a certain article type in the background. Here are two ways to achieve this.
When creating an article type, disable it directly
useregister_post_typeWhen creating a new article type, WordPress directly sets the article type for us.create_postsThe permission is ‘do_not_allow’. After adding this parameter, the newly created file type will not have the menu and button to create new content.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => 'do_not_allow', // 移除 "新建" 功能
),
'map_meta_cap' => true, // 如果不用许用户修改/删除文章,设置为 `false`
));
If we need to enable creating articles for users in a certain rolePermissions, we can setcreate_postsThe value is the name of the role, such as ‘administrator’.
Disabled after creating post type
Sometimes, our target article type is created in advance through a plug-in. At this time, it is unwise to modify the relevant code in the theme or plug-in, because once the plug-in is updated, the modifications we made will be overwritten and we will have to do it all over again. In various situations, we can modify the permissions of the target article type through WordPress Action. As follows, when initializing WordPress, we set the new article permission of the “Article” article type to “do_not_allow”, and we can also remove the permission to create the article type.
add_action('init',function () {
global $wp_post_types;
$wp_post_types['post']->cap->create_posts = 'do_not_allow';
});
In addition, we can also prevent users from creating new articles by hiding the background menu of new articles. However, this method is unsafe. If the user knows the URL address of the new article, he can directly enter the address in the browser to open the page of the new article, so this method is not recommended. The safest bet is to use one of the two methods described in this article.
