Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created July 2, 2012 20:07
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save chrisguitarguy/3035374 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/3035374 to your computer and use it in GitHub Desktop.
Set a default author for posts on your WordPress site.
<?php
/*
Plugin Name: Default Author
Plugin URI: http://pmg.co
Description: Set a default author for all posts and pages
Version: 1.0
Text Domain: default-author
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Performance Media Group
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class PMG_Default_Author
{
/**
* Where we'll story the default author
*
* @since 1.0
*/
const SETTING = 'apex_default_author';
/**
* The section to which we'll add our setting
*
* @since 1.0
*/
const SECTION = 'default';
/**
* Page to which the setting is added
*
* @since 1.0
*/
const PAGE = 'writing';
/**
* Adds the actions and such
*
* @access public
* @since 1.0
* @return null
*/
public static function init()
{
add_action(
'admin_init',
array(get_class(), 'settings')
);
add_filter(
'wp_insert_post_data',
array(get_class(), 'change_author')
);
}
/**
* Hooked into `admin_init`
*
* Registers the plugin's settings and adds a field to the writing options
* page
*
* @access public
* @since 1.0
* @uses register_setting
* @uses add_settings_field
* @return null
*/
public static function settings()
{
register_setting(
self::PAGE,
self::SETTING,
array(get_class(), 'clean_cb')
);
add_settings_field(
'apex-default-author',
__('Default Author', 'default-author'),
array(get_class(), 'field_cb'),
self::PAGE,
self::SECTION
);
}
/**
* Callback function for settings sanitazation. Thin wrapper around absint
*
* @access public
* @since 1.0
* @uses absint
* @param string $in the Author ID to sanitaze
* @return int The author ID as an absolute integer
*/
public static function clean_cb($in)
{
return absint($in);
}
/**
* Callback function for the settings field. Spits out the field itself
*
* @access public
* @since 1.0
* @uses get_users
* @uses absint
* @uses esc_attr
* @uses selected
* @uses esc_html
* @return null
*/
public static function field_cb()
{
$users = get_users(array(
'orderby' => 'nicename'
));
printf('<select id="%1$ss" name="%1$s">', esc_attr(self::SETTING));
foreach($users as $u)
{
printf(
'<option value="%s" %s>%s</option>',
absint($u->ID),
selected(absint($u->ID), self::opt(), false),
esc_html($u->display_name)
);
}
echo '</select>';
}
/**
* Hooked into `wp_insert_post_data`. Changes the author id to whatever
* the default author is set to
*
* @access public
* @since 1.0
* @return array The array of post data
*/
public static function change_author($arr)
{
if(!($aid = self::opt()))
{
return $arr;
}
$arr['post_author'] = $aid;
return $arr;
}
/**
* Fetch the global author id.
*
* @access protected
* @since 1.0
* @uses get_option
* @uses absint
* @return integer The author ID
*/
protected static function opt()
{
return get_option(self::SETTING, 0);
}
} // end class
PMG_Default_Author::init();
@cliffordp
Copy link

Thanks for this plugin. It seems along the lines of what I'm looking for, but I activated it but don't see any settings or anything working. Where am I supposed to be able to specify the default author? Also, is it set per author (like 'kevin' should always post as 'sammy' unless he manually overrides it back to 'kevin') or is it global (all authors are defaulted to 'susan')? Thank you. -Cliff

@HKandulla
Copy link

works like a charm, thanks!

@HKandulla
Copy link

after updating to 3.5.2, I had to do the following changes to get it working again:

public static function change_author($arr)
{
$timezone = new DateTimeZone("Europe/Berlin");
$dateCreated = DateTime::createFromFormat('Y-m-d H:i:s', $arr['post_date'], $timezone);
$dateNow = new DateTime('now', $timezone);

if($dateCreated == $dateNow) {
  $arr['post_author'] = self::opt();
}

return $arr;

}

@borisebe
Copy link

Thanks! Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment