2 fixed answer
source | link

Please replace $post_type with your post_type in question, e.g. post, page, cpt_slug,…

The function echoing the meta box with the preview button is called post_submit_meta_box. The condition to show the button is set with the function is_post_type_viewable. Following that: If you set the flag {$post_type}s flags publicly_queryable or _builtin and public are set to true the preview button will be shown. That means all publicly queryable built-in post types will have that button anyway. For CPTs you can adapt the flags, if you don't need them publicly queryable.

What if I want to publicly query my CPT$post_type, but don't want a preview button?

You are out of luck for an easy solution. But there is always a workaround:

  1. Hide the Button with custom CSS that you load on condition of screens post.php/post-new.php and your CPT$post_type.
  2. Hide the Button with JS on the same condition.
  3. Remove the meta box and replace it with your own. Here some code to get you started.

    function add_custom_submit_box() {
        $publish_callback_args = null;
        //TODO: Add revisions support if needed, see wp-admin/edit-form-advanced.php Line 219 if needed.
        remove_meta_box( 'submitdiv', 'my_cpt_slug'$post_type, 'side');
        add_meta_box( 'submitdiv', __( 'Publish' ), 'my_cpt_submit_meta_box''my_{$post_type}_submit_meta_box', 'my_cpt_slug'$post_type, 'side', 'core', $publish_callback_args );
    }
    
    add_action('init','add_custom_submit_box');
    
    function my_cpt_submit_meta_boxmy_{$post_type}_submit_meta_box( $post, $args = array() ) {
        //TODO: Copy & paste post_submit_meta_box function and adapt to your needs.
    }
    

The function echoing the meta box with the preview button is called post_submit_meta_box. The condition to show the button is set with the function is_post_type_viewable. Following that: If you set the flag publicly_queryable or _builtin and public to true the preview button will be shown.

What if I want to publicly query my CPT, but don't want a preview button?

You are out of luck for an easy solution. But there is always a workaround:

  1. Hide the Button with custom CSS that you load on condition of screens post.php/post-new.php and your CPT.
  2. Hide the Button with JS on the same condition.
  3. Remove the meta box and replace it with your own. Here some code to get you started.

    function add_custom_submit_box() {
        $publish_callback_args = null;
        //TODO: Add revisions support if needed, see wp-admin/edit-form-advanced.php Line 219 if needed.
        remove_meta_box( 'submitdiv', 'my_cpt_slug', 'side');
        add_meta_box( 'submitdiv', __( 'Publish' ), 'my_cpt_submit_meta_box', 'my_cpt_slug', 'side', 'core', $publish_callback_args );
    }
    
    add_action('init','add_custom_submit_box');
    
    function my_cpt_submit_meta_box( $post, $args = array() ) {
        //TODO: Copy & paste post_submit_meta_box function and adapt to your needs.
    }
    

Please replace $post_type with your post_type in question, e.g. post, page, cpt_slug,…

The function echoing the meta box with the preview button is called post_submit_meta_box. The condition to show the button is set with the function is_post_type_viewable. Following that: If the {$post_type}s flags publicly_queryable or _builtin and public are set to true the preview button will be shown. That means all publicly queryable built-in post types will have that button anyway. For CPTs you can adapt the flags, if you don't need them publicly queryable.

What if I want to publicly query my $post_type, but don't want a preview button?

You are out of luck for an easy solution. But there is always a workaround:

  1. Hide the Button with custom CSS that you load on condition of screens post.php/post-new.php and your $post_type.
  2. Hide the Button with JS on the same condition.
  3. Remove the meta box and replace it with your own. Here some code to get you started.

    function add_custom_submit_box() {
        $publish_callback_args = null;
        //TODO: Add revisions support if needed, see wp-admin/edit-form-advanced.php Line 219 if needed.
        remove_meta_box( 'submitdiv', $post_type, 'side');
        add_meta_box( 'submitdiv', __( 'Publish' ), 'my_{$post_type}_submit_meta_box', $post_type, 'side', 'core', $publish_callback_args );
    }
    
    add_action('init','add_custom_submit_box');
    
    function my_{$post_type}_submit_meta_box( $post, $args = array() ) {
        //TODO: Copy & paste post_submit_meta_box function and adapt to your needs.
    }
    
1
source | link

The function echoing the meta box with the preview button is called post_submit_meta_box. The condition to show the button is set with the function is_post_type_viewable. Following that: If you set the flag publicly_queryable or _builtin and public to true the preview button will be shown.

What if I want to publicly query my CPT, but don't want a preview button?

You are out of luck for an easy solution. But there is always a workaround:

  1. Hide the Button with custom CSS that you load on condition of screens post.php/post-new.php and your CPT.
  2. Hide the Button with JS on the same condition.
  3. Remove the meta box and replace it with your own. Here some code to get you started.

    function add_custom_submit_box() {
        $publish_callback_args = null;
        //TODO: Add revisions support if needed, see wp-admin/edit-form-advanced.php Line 219 if needed.
        remove_meta_box( 'submitdiv', 'my_cpt_slug', 'side');
        add_meta_box( 'submitdiv', __( 'Publish' ), 'my_cpt_submit_meta_box', 'my_cpt_slug', 'side', 'core', $publish_callback_args );
    }
    
    add_action('init','add_custom_submit_box');
    
    function my_cpt_submit_meta_box( $post, $args = array() ) {
        //TODO: Copy & paste post_submit_meta_box function and adapt to your needs.
    }