我正在写一个插件,可以从前端写帖子。这些帖子在发布之前必须经过管理员的检查。现在,如果管理员编辑文章或发布文章,则文章作者将从原始作者更改为管理员。我怎样才能防止这种情况?
帖子作者在其帖子被管理员修改后更改为管理员
3 个回复
SO网友:bosco
行政编辑可以fix the author manually.
或者,您可以添加自定义post元数据来指定原始作者。然后,钩住publish_post
or transition_post_status
actions (甚至save_post
因此,您可以在发布帖子时检查元数据的存在,如果存在,请使用元数据中的原始作者替换帖子的作者。
试图用一个钩子将其击倒:
function correct_post_data( $strNewStatus, $strOldStatus, $post ) {
/* Only pay attention to posts (i.e. ignore links, attachments, etc. ) */
if( $post->post_type !== \'post\' )
return;
/* If this is a new post, save the original author into the post\'s meta-data. */
if( $strOldStatus === \'new\' ) {
update_post_meta( $post->ID, \'original_author\', $post->post_author );
}
/* If this post is being published, try to restore the original author */
if( $strNewStatus === \'publish\' ) {
$originalAuthor = get_post_meta( $post->ID, \'original_author\' );
/* If this post has an original author and it\'s not who the post says it is, revert the author field. */
if( !empty( $originalAuthor ) && $originalAuthor != $post->post_author ) {
$postData = array(
\'ID\' => $post->ID,
\'post_author\' => $originalAuthor
);
wp_update_post( $postData ); //May wish to check if this returns 0 for error-handling
}
}
}
add_action( \'transition_post_status\', \'correct_post_data\' );
检查!is_admin()
在那里的某个地方也可以用来确认用户位于站点前端的某个地方。SO网友:Andreas Olsson
几周前我也遇到了同样的问题。我的问题是,我使用的是自定义帖子类型,我没有添加对作者的支持。它总是由正确的作者发布,但当管理员更改帖子状态或更新帖子时,管理员将成为帖子作者。
尝试添加对作者的支持,看看这是否有帮助!
SO网友:Ed Burns
这似乎是一个非常奇怪的问题。帖子的状态不应影响用户设置。
你考虑过使用重力形式吗?这是一个付费插件,但它在创建表单方面做得很好,而且创建一个前端表单来自动创建帖子(在任何一种情况下)相对简单published
或draft
或review
国家)。
结束