Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

3 Answers 3

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.

share|improve this answer

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!

share|improve this answer

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).

share|improve this answer
2  
The question is about the regular post edit mechanics. GF would be useless here. –  toscho Jan 3 '13 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.