Search code examples
wordpresswordpress-themingcustom-post-type

How to remove "add new" button from the editor page of a custom post type in wordpress?


I have a custom post type named "jxta_home". I have removed add new button from the submenu and edit page by using the following code-

<?php
function disable_new_posts() {

    global $submenu;
    unset($submenu['edit.php?post_type=jxta_home'][10]);

    // Hide link on listing page
    if (isset($_GET['post_type']) && $_GET['post_type'] == 'jxta_home')  {
        echo '<style type="text/css">
        .page-title-action, .submitdelete { display:none; }
        </style>';
    } 
}

But the add new button is still showing on the inner editor page. I want to remove it from there too. How can I remove it from the inner editor page?

enter image description here


Solution

  • There is two option once is with css and another is will coding.

    Option 1 :

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

    Option 2 :

    You disable the add new capabilities while passing the parameter in the register post type.

    The parameter is :

    create_posts' => false

    Assuming you have the code like below :

    $args = array(
        'label'               => __( 'Custom Post Type', 'text_domain' ),
        'description'         => __( 'Custom Post Type', 'text_domain' ),
        'capability_type' => 'custom_post_type',
        'capabilities' => array(
            'create_posts' => false
        )
    );
    register_post_type( 'custom_post_type', $args );