問題描述

我想要能夠在管理員中選擇一個作者的作者,所以它顯示他們的名字是寫的,但我不想給他們任何額外的權限 (如果他們登錄唯一可以訪問的是他們的個人資料) 。

有沒有一個簡單的方法來做,而不必改變角色和功能?

謝謝

最佳解決方案

這是我在類似情況下寫的一個簡單的黑客。它將在 Author 下拉列表中顯示編輯/添加帖子/頁面上的所有 Subscribers,從中可以選擇任意一個。我覺得應該適合你

add_filter('wp_dropdown_users', 'MySwitchUser');
function MySwitchUser($output)
{

    //global $post is available here, hence you can check for the post type here
    $users = get_users('role=subscriber');

    $output = "<select id="post_author_override" name="post_author_override" class="">";

    //Leave the admin in the list
    $output .= "<option value="1">Admin</option>";
    foreach($users as $user)
    {
        $sel = ($post->post_author == $user->ID)?"selected='selected'":'';
        $output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
    }
    $output .= "</select>";

    return $output;
}

提交此提示後,WP 提交此頁面後,WP 僅從 $ _POST 數組中的下拉列表中讀取 $ user-> ID,並將其分配為帖子作者。這就是你想要的!

次佳解決方案

從 WordPress 4.4.0 起,您現在可以使用 wp_dropdown_users_args 過濾器。現在的代碼要簡單得多:

add_filter( 'wp_dropdown_users_args', 'add_subscribers_to_dropdown', 10, 2 );
function add_subscribers_to_dropdown( $query_args, $r ) {

    $query_args['who'] = '';
    return $query_args;

}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。