PHP Developer

Wednesday, 24 June 2015

Select Subscriber and Admin Users as author of post in admin panel

 Select Subscriber and Admin Users as author of post in admin panel : 


In this select a subscriber or admin an author of a post in the admin so it displays their name as having written the post.

Add this code in functions.php file, then all users with role subscriber or administrator is show in dropdown in Author section of post

Add this code in functions.php file : 

<?php
add_filter('wp_dropdown_users', 'MySwitchUser');
function MySwitchUser($output)
{
//global $post is available here, hence you can check for the post type here
global $post;
$roles = array('administrator', 'editor', 'author', 'contributor', 'subscriber', 'byt_frontend_contributor', 'customer', 'shop_manager', 'pending');

$output = "<select id=\"post_author_override\" name=\"post_author_override\" class=\"\">";
foreach($roles as $role){
$users = get_users('role='.$role);
foreach($users as $user)
{
global $post;
$sel = ($post->post_author == $user->ID)?"selected='selected'":'';
$output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
}
}
//Leave the admin in the list    
    $output .= "</select>";

    return $output;
}
?>

No comments:

Post a Comment