Last active
February 6, 2023 03:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 );