Skip to content

Instantly share code, notes, and snippets.

@mzdebo
Forked from chrisguitarguy/default-author.php
Created September 25, 2020 11:50
Show Gist options
  • Save mzdebo/9cd535f8a1b5be3c02f0cb871a3c2159 to your computer and use it in GitHub Desktop.
Save mzdebo/9cd535f8a1b5be3c02f0cb871a3c2159 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment