Post author is changed to admin after his post is modified by admin

I am writing a plugin which will enable to write posts from front-end. Those posts have to be checked by admin before pu

I am writing a plugin which will enable to write posts from front-end. Those posts have to be checked by admin before published. Now if admin edits the post or publishes the post, the post author is changed from the original author to admin. How can I prevent that?

I am writing a plugin which will enable to write posts from front-end. Those posts have to be checked by admin before published. Now if admin edits the post or publishes the post, the post author is changed from the original author to admin. How can I prevent that?

Share Improve this question edited Sep 23, 2013 at 17:45 kaiser 50.9k27 gold badges151 silver badges245 bronze badges asked Jan 2, 2013 at 17:28 HoGoHoGo 3471 gold badge5 silver badges15 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

The administrative editors could fix the author manually.

Alternatively, you could add custom post meta-data to designate the original author. Then, hooking into the publish_post or transition_post_status actions (or even save_post for that matter) you could check for the presence of the meta-data when a post is being published, and if it exists, replace the post's author with the original from the meta-data.

Attempting to knock it out with one hook:

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' );

A check for !is_admin() in there somewhere could also be useful to confirm that the user is somewhere on the front-end of the site.

I experienced the same problem a couple of weeks ago. My problem was that I was using a custom post type and I didn't add support for author. It was always published by the correct author but when admin changed post status or updated the post the admin becomes the post author.

Try adding support for author and see if that helps!

That seems like a very strange issue. The status of a post should not affect the user setting.

Have you considered using Gravity Forms? It is a paid plugin but it does a nice job with creating the forms and it is relatively simple to create a front end form that creates a post automatically (in either published or draft or review state).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745067809a4609359.html

相关推荐

  • Post author is changed to admin after his post is modified by admin

    I am writing a plugin which will enable to write posts from front-end. Those posts have to be checked by admin before pu

    21小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信