Skip to content

Instantly share code, notes, and snippets.

@grappler
Last active February 6, 2023 03:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grappler/6046201 to your computer and use it in GitHub Desktop.
Save grappler/6046201 to your computer and use it in GitHub Desktop.
Here is how to remove the "view" button from all three locations for a custom post type.
<?php
/**
* Hides the 'view' button in the post edit page
*
*/
function hv_hide_view_button() {
$current_screen = get_current_screen();
if( $current_screen->post_type === 'post-type' ) {
echo '<style>#edit-slug-box{display: none;}</style>';
}
return;
}
add_action( 'admin_head', 'hv_hide_view_button' );
/**
* Removes the 'view' link in the admin bar
*
*/
function hv_remove_view_button_admin_bar() {
global $wp_admin_bar;
if( get_post_type() === 'post-type'){
$wp_admin_bar->remove_menu('view');
}
}
add_action( 'wp_before_admin_bar_render', 'hv_remove_view_button_admin_bar' );
/**
* Removes the 'view' button in the posts list page
*
* @param $actions
*/
function hv_remove_view_row_action( $actions ) {
if( get_post_type() === 'post-type' )
unset( $actions['view'] );
return $actions;
}
// Applies to non-hierarchical CPT
add_filter( 'post_row_actions', 'hv_remove_view_row_action', 10, 1 );
// Applies to hierarchical CPT
add_filter( 'page_row_actions', 'hv_remove_view_row_action', 10, 1 );
@datafeedr
Copy link

Note that if your Custom Post Type is hierarchical, you need to replace this:

add_filter( 'post_row_actions', 'hv_remove_view_row_action', 10, 1 );

With this:

add_filter( 'page_row_actions', 'hv_remove_view_row_action', 10, 1 );

See line #605 in ~/wp-admin/includes/class-wp-posts-list-table.php (WP Version 3.6.1):

$actions = apply_filters( is_post_type_hierarchical( $post->post_type ) ? 'page_row_actions' : 'post_row_actions', $actions, $post );

@grappler
Copy link
Author

grappler commented Mar 1, 2014

@datafeedr - Thanks, updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment