WordPress - "Author"下拉菜单不会记住谁是作者



我添加了一个过滤器,以便普通用户可以被选为帖子的作者。在选择作者并更新分配给用户的帖子后,它运行良好。

问题是,这样做之后 - 当你转到该帖子的前端然后回到后端时,作者下拉菜单切换回管理员(好像 WordPress 不记得它设置了什么,所以如果你更新帖子,它将再次分配给管理员)。

似乎wordpress忘记了谁被选为作者。如何修改过滤器以使该下拉菜单记住谁被选为帖子的作者?

add_filter('wp_dropdown_users', 'theme_post_author_override');
function theme_post_author_override($output)
{
  // return if this isn't the theme author override dropdown
  if (!preg_match('/post_author_override/', $output)) return $output;
  // return if we've already replaced the list (end recursion)
  if (preg_match ('/post_author_override_replaced/', $output)) return $output;
  // replacement call to wp_dropdown_users
 $output = wp_dropdown_users(array(
   'echo' => 0,
  'name' => 'post_author_override_replaced',
  'selected' => empty($post->ID) ? $user_ID : $post->post_author,
  'include_selected' => true
 ));
 // put the original name back
 $output = preg_replace('/post_author_override_replaced/', 'post_author_override', $output);
  return $output;
}

这可能已经解决了,但我只是遇到了同样的问题并解决了它,所以想分享解决方案。上面的代码需要在第 4 行中增加一行。我希望它对某人有所帮助。

add_filter('wp_dropdown_users', 'theme_post_author_override');
function theme_post_author_override($output)
{
global $post, $user_ID; // <-- You need this line
// return if this isn't the theme author override dropdown
if (!preg_match('/post_author_override/', $output)) return $output;
// return if we've already replaced the list (end recursion)
if (preg_match ('/post_author_override_replaced/', $output)) return $output;
// replacement call to wp_dropdown_users
$output = wp_dropdown_users(array(
    'echo' => 0,
    'name' => 'post_author_override_replaced',
    'selected' => empty($post->ID) ? $user_ID : $post->post_author,
    'include_selected' => true
));

// put the original name back
$output = preg_replace('/post_author_override_replaced/',         'post_author_override', $output);
return $output;
}

最新更新