2

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.

1 Answer 1

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

4
  • 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, 2013 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, 2013 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, 2013 at 18:10
  • 2
    Just saw your updated note in the question. My recent update of the code should work for that.
    – Jake Lisby
    Feb 7, 2013 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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