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.

An Author 'XYZ' is need to be set as the Default author of all New Posts. Irrespective of the actual Author posting the content, the post should be saved by this author 'XYZ'.

Is there a Plugin or custom functions, which serves this purpose?

Note : The existing posts should stay as it is, no 'change of author' for old posts, only new one should be effected.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
function wp84782_replace_author( $post_ID )  
{
  $my_post = array();
  $my_post['ID'] = $post_ID;
  $my_post['post_author'] = 1 ; //This is the ID number of whatever author you want to assign

// Update the post into the database
  wp_update_post( $my_post );
}
add_action( 'publish_post', 'wp84782_replace_author' );

Update: This hook runs as the post is being published, not after, so the command is trying to overwrite what the system is doing at the same time. So here's a modified version that cancels all of this is the post was previously published. If you're wanting it to never allow user's to update the author, perhaps you could hide that meta field. I'm not aware of a hook that runs immediately after publish of a post, but if there were one, hooking into it would allow solve this problem.

function wp84782_replace_author( $post_ID )  
{
    if(get_post_status( $post_ID ) == 'publish'){
        return;
    }
    else {
        $my_post = array();
        $my_post['ID'] = $post_ID;
        $my_post['post_author'] = 1 ; //This is the ID number of whatever author you want to assign

        // Update the post into the database
        wp_update_post( $my_post );
    }
}
add_action( 'publish_post', 'wp84782_replace_author' );

Disclaimer: All this code is not tested, so there may be minor edits needed.

share|improve this answer
    
Is this function be replaced with something else, or just need to add in the theme funtions.php file? Thnaks for the Answer though. –  uzair Feb 6 '13 at 16:17
1  
Sorry, you'll need to add it to your functions.php file. The one thing you might want to edit is the ID of the post author. Change that to whatever author you would like to assign. –  Jake Lisby Feb 6 '13 at 16:44
    
This did serve the purpose, however there is some problem when i'm trying to update the post. The circular wait image keeps on rotating and post exceeds the max response time. And in Quick Edit mode, when i change the author from dropdown, the same thing is happening. Any input from you is helpful, Thanks in advance! –  uzair Feb 6 '13 at 18:10
1  
Just saw your updated note in the question. My recent update of the code should work for that. –  Jake Lisby Feb 7 '13 at 13:16

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.