WordPress: Disable “Add New” on Custom Post Type

前端 未结 10 910
执笔经年
执笔经年 2020-12-07 14:58

Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I\'ve looked into labels and arguments but can\'t find anything that

相关标签:
10条回答
  • 2020-12-07 15:10

    @ Staffan Estberg,

    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.

    0 讨论(0)
  • 2020-12-07 15:17

    WordPress Networks: I found that Seamus Leahy's answer doesn't work if you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.

    register_post_type( 'custom_post_type_name', array(
      'capability_type' => 'post',
      'capabilities' => array(
        'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
      ),
      'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
    ));
    

    The accepted answer hides the menu item, but the page is still accessible.

    0 讨论(0)
  • 2020-12-07 15:22

    Disable creating new post for registered post-types: (example for post and page)

    function disable_create_newpost() {
        global $wp_post_types;
        $wp_post_types['post']->cap->create_posts = 'do_not_allow';
        //$wp_post_types['page']->cap->create_posts = 'do_not_allow';
        //$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
    }
    add_action('init','disable_create_newpost');
    
    0 讨论(0)
  • 2020-12-07 15:28

    In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :

    1. wp-admin\edit-form-advanced.php
    2. wp-admin\edit.php
    3. wp-admin\includes\post.php
    4. wp-admin\menu.php
    5. wp-admin\post-new.php
    6. wp-admin\press-this.php
    7. wp-includes\admin-bar.php
    8. wp-includes\class-wp-xmlrpc-server.php
    9. wp-includes\post.php

    So if you really want to disable this feautere you must do it per role and per post type. I use the great plugin "User Role Editor" to manage the capabilities per role.

    But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.

    So you can add this piece of code in your functions.php and the you can manage this capability.

    function fix_capability_create(){
        $post_types = get_post_types( array(),'objects' );
        foreach ( $post_types as $post_type ) {
            $cap = "create_".$post_type->name;
            $post_type->cap->create_posts = $cap;
            map_meta_cap( $cap, 1); 
        }
    }
    add_action( 'init', 'fix_capability_create',100);
    

    So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).

    The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).

    0 讨论(0)
提交回复
热议问题