I have been looking for ways to completely remove the All, Published, and Trash webpages for USERS other than the me, the Administrator.
So the scenario for me is: I have registered users (which I would like to assign with the roles of authors, contributors, etc). Now, when they log in to my website and access the Posts area, I wish for them not to see these three links (All, Published, Trash).
I have tried using the jquery function, which is basically the same as display: none, but I don’t think it’s the optimal choice. I know there’s better ways for me to do this.
I did try using plugins, such as Member and Adminimize, among other things, but I can’t find the right one.
I hope there’s someone out there who can help me. Thanks in advance.
2 s
The WP_Posts_List_Table
class extends WP_List_Table
and within the WP_List_Table::views()
method we have the following dynamic views filter:
/**
* Filter the list of available list table views.
*
* The dynamic portion of the hook name, `$this->screen->id`, refers
* to the ID of the current screen, usually a string.
*
* @since 3.5.0
*
* @param array $views An array of available list table views.
*/
$views = apply_filters( "views_{$this->screen->id}", $views );
So we can use the generated views_edit-post
filter to adjust the views of the post list table.
Example:
Let’s remove the all, publish, future, sticky, draft, pending and trash for non-admins (PHP 5.4+):
/**
* Remove the 'all', 'publish', 'future', 'sticky', 'draft', 'pending', 'trash'
* views for non-admins
*/
add_filter( 'views_edit-post', function( $views )
{
if( current_user_can( 'manage_options' ) )
return $views;
$remove_views = [ 'all','publish','future','sticky','draft','pending','trash' ];
foreach( (array) $remove_views as $view )
{
if( isset( $views[$view] ) )
unset( $views[$view] );
}
return $views;
} );
where you can modify the $remove_views
to your needs. Another approach would be to only pick up mine and only return that view.
Then we could try to force the mine view further with:
/**
* Force the 'mine' view on the 'edit-post' screen
*/
add_action( 'pre_get_posts', function( \WP_Query $q )
{
if(
is_admin()
&& $q->is_main_query()
&& 'edit-post' === get_current_screen()->id
&& ! current_user_can( 'manage_options' )
)
$q->set( 'author', get_current_user_id() );
} );
Before:
After: