Skip to content

Instantly share code, notes, and snippets.

@simongcc
Last active January 4, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simongcc/8628038 to your computer and use it in GitHub Desktop.
Save simongcc/8628038 to your computer and use it in GitHub Desktop.
/*
Edit Page Customization
Customizable:
Column Head (filter: manage_posts_columns => manage_{$post_type}_columns )
Column Contents (filter: manage_posts_custom_column => manage_{$post_type}_custom_column)
Function link (called $views)
Version Tested: 3.8
*/
// example 1: remove unwanted links
// because mine is available when user cannot edit_others_posts, it make sense for all items only display current user's posts'
if( !current_user_can("edit_others_posts"):
function custom_wo_shop_edit_view( $views ) {
// var_dump( $views );
// by default, this including all users' post, not only for current user
unset($views['publish']);
unset($views['draft']);
unset($views['trash']);
unset($views['pending']);
unset($views['all']);
unset($views['mine']); // only when using author query
return $views;
}
add_filter('views_edit-wo_shop', 'custom_wo_shop_edit_view');
endif;
// example 2: redefine links for current viewer who do not have edit other post capability
if( !current_user_can("edit_others_posts") ):
function custom_wo_shop_edit_view( $views ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$pending = (int) count_user_posts_by_status('pending',$user_id);
$draft = (int) count_user_posts_by_status('draft',$user_id );
$publish = (int) count_user_posts_by_status('publish',$user_id);
$trash = (int) count_user_posts_by_status('trash',$user_id);
$allposts = $pending + $draft + $publish;
// create new name vs number of post array
$num_posts = array(
'pending' => $pending,
'draft' => $draft,
'publish' => $publish,
'trash' => $trash,
'all' => $allposts,
);
// reset all links to nothing
foreach( array_keys( $num_posts ) as $key ):
// echo $key."<br>";
unset( $views[$key] );
endforeach;
// get the current post type
if ( isset( $_GET['post'] ) ) {
$current_post_id = (int) $_GET['post'];
$current_post_type = get_post_type( $current_post_id );
}
if( empty( $current_post_type ) && isset( $_GET['post_type'] ) ) $current_post_type = $_GET['post_type'];
foreach ( get_post_stati(array('show_in_admin_status_list' => true), 'objects') as $status ):
$class = '';
$status_name = $status->name;
// var_dump( $status_name );echo "<br><br>";
if ( !in_array( $status_name, $num_posts ) )
continue;
if ( empty( $num_posts[$status_name] ) )
continue;
if ( isset($_REQUEST['post_status']) && $status_name == $_REQUEST['post_status'] )
$class = ' class="current"';
// var_dump( $status_name );echo "<br><br>";
$views[$status_name] = "<a href='edit.php?post_status=$status_name&amp;post_type=$current_post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $num_posts[$status_name] ), number_format_i18n( $num_posts[$status_name] ) ) . '</a>';
// $views[$status_name] = "";
endforeach;
$views['all'] = "<a href='edit.php?post_type=$current_post_type'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $allposts, 'posts' ), number_format_i18n( $allposts ) ) . '</a>';
// var_dump( $views['all'] );echo "<br><br>";
$views['trash'] = "<a href='edit.php?post_status=$status_name&amp;post_type=$current_post_type'$class>" . sprintf( translate_nooped_plural( $status->label_count, $trash ), number_format_i18n( $trash ) ) . '</a>';
// //this including all published and all of the post, not only for current user, so hide it out
return $views;
}
add_filter('views_edit-wo_shop', 'custom_wo_shop_edit_view');
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment