WordPress make admin post as a different user by default. You should never post a admin user, so you do not expose the admin username to others. But it would be nice if you would have a script that sets the post author to another user anyway. Now you do.
Create a new plugin pluginauthor
and add the following code to it. The following code will set post author to AUTHOR ID 2 if you are posting as an admin.
<?php
/**
* @package pluginauthor
* @version 1.0
*/
/*
Plugin Name: Plugin Author
Plugin URI: http://www.lehelmatyus.com
Description: Changing the author id from 1 to 2
Author: Lehel Matyus
Version: 1.0
Author URI: http://www.lehelmatyus.com
*/
function pluginauthor_set_author( $post_id ) {
// If this is just a revision, don't do anything.
if ( wp_is_post_revision( $post_id ) )
return;
//Check it's not an auto save routine
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
//Perform permission checks! For example:
if ( !current_user_can('edit_post', $post_id) )
return;
// If post author is admin -> set it to author id "2"
// special user set up for this purpouse
// unhook this function so it doesn't loop infinitely
remove_action('save_post','pluginauthor_set_author');
if ( is_super_admin() ) {
// if ( isset($_GET['auth_id']) ) {
$args = array('ID'=>$post_id, 'post_author' => 2 ); // put ID of Default Author here
wp_update_post( $args );
// }
}
// re-hook this function
add_action( 'save_post', 'pluginauthor_set_author' );
}
add_action( 'save_post', 'pluginauthor_set_author' );
?>
Be sure to change 2 to the default user ID you want to use a different one.
The above code is released under MIT License.
Cheers!
You can do this without digging into the code:
Admin > Posts > All Posts > Screen Options (top right) a box will slide down and check the Author.
This will add a dropdown to the edit post page where you can select the author.
Hi htmlg, indeed that is the way you should do it for a post by post basis. This code just provides a safety fallback for my clients who keep logging in as admin.
Thanks,
Lehel
The plugin broke my install. Not sure whats wrong, but I liked the idea.