WordPress: Disable “Add New” on Custom Post Type

前端 未结 10 909
执笔经年
执笔经年 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:06

    The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.

    The solution mentioned @3pepe3 relies on get_post_type() which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:

    function disable_new_posts() {
        // Hide sidebar link
        global $submenu;
        unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);
    
        // Hide link on listing page
        if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
            echo '<style type="text/css">
            #favorite-actions, .add-new-h2, .tablenav { display:none; }
            </style>';
        }
    }
    add_action('admin_menu', 'disable_new_posts');
    

    EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003

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

    Full credit to Seamus Leahy

    There is a meta capability create_posts that is documented here and 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(
      'capability_type' => 'post',
      'capabilities' => array(
        'create_posts' => 'do_not_allow', // false < WP 4.5, credit @Ewout
      ),
      'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
    ));
    

    You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.

    May I ask why you want to do this?

    I would at first have suggested changing the capabilities for your custom post type, but I don't think there's one that limits who can add posts, but only who can edit or publish them.

    It looks a little dirty, but you could try unsetting the item in the $submenu global;

    function hide_add_new_custom_type()
    {
        global $submenu;
        // replace my_type with the name of your post type
        unset($submenu['edit.php?post_type=my_type'][10]);
    }
    add_action('admin_menu', 'hide_add_new_custom_type');
    

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

    As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :

    add_action( 'admin_head', function(){
        ob_start(); ?>
        <style>
            #wp-admin-bar-new-content{
                display: none;
            }
            a.page-title-action{
                display: none !important;
            }
            #menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
                display:none;
            }
        </style>
    <?php ob_end_flush();
    });
    
    0 讨论(0)
  • 2020-12-07 15:08

    There is a meta capability create_posts that is documented here and 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(
      'capability_type' => 'post',
      'capabilities' => array(
        'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
      ),
      'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
    ));
    

    You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.

    0 讨论(0)
  • 2020-12-07 15:08
    add_action("load-post-new.php", 'block_post');
    
    function block_post()
    {
        if($_GET["post_type"] == "custom_type") 
            wp_redirect("edit.php?post_type=custom_type");
    }
    
    0 讨论(0)
  • 2020-12-07 15:09

    I found this simplest way for this. Just ad this code into theme’s function.php.

    function hd_add_buttons() {
        global $pagenow;
        if (is_admin()) {
            if ($_GET['post_type'] == 'custom_post_type_name') {
                echo '<style>.add-new-h2{display: none !important;}</style>';
            }
        }
    }
    add_action('admin_head', 'hd_add_buttons');
    
    0 讨论(0)
提交回复
热议问题