Created
April 8, 2016 14:49
-
-
Save Jany-M/5aa65d518c324daecb4ddbd9a10c53f4 to your computer and use it in GitHub Desktop.
[WordPress] Hide Trash link depending on User Role and place it for last
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 | |
// Display Trash depending on where and who - and for last | |
function trash_action( $actions, $post ) { | |
global $post, $typenow; | |
$user_id = get_current_user_id(); | |
$user_data = get_userdata($user_id); | |
// Can this user see the Trash link? | |
$my_custom_roles_array = array('custom-role-1', 'custom-role-2'); | |
$check_user = array_intersect($my_custom_roles_array, (array) $user_data->roles); | |
if(count($check_user) > 0 /* you could perform some more cheks here if you want */) | |
$is_authorized = true; | |
// Let's remove the default Trash link | |
unset( $actions['trash'] ); | |
// Only display Trash if user is authorized | |
if ($is_authorized == true || current_user_can('moderate_comments')) { | |
$actions['trash'] .= '<a class="submitdelete" href="'.wp_nonce_url("post.php?action=trash&post=$post->ID", 'trash-post_'.$post->ID).'"">'. __( 'Trash' ).'</a>'; | |
} | |
return $actions; | |
} | |
add_filter( 'post_row_actions', 'trash_action', 10, 2 ); | |
add_filter( 'page_row_actions', 'trash_action', 10, 2 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment