wiki

Settings for default author in WordPress

This code will allow to select default author in General Settings. Available users aare those with role Author, Editor or Admin (cn be tweaked in code). If none i chosen setting ddefaults to user ID 1 – usual superadmin (also can be chaanged).

// Add the field to the general settings page
function itsg_add_author_setting_field() {
add_settings_field(
'default_post_author', // ID of the settings field
'Default Post Author', // Title of the field
'itsg_default_post_author_field_html', // Function to display the field
'general' // Page to add the field to (General Settings page)
);

// Register the setting
register_setting('general', 'default_post_author', array(
'type' => 'integer',
'description' => 'Default author ID for new posts',
'sanitize_callback' => 'absint', // Ensure the value is an absolute integer
'default' => 1 // Default value if not set
));
}
add_action('admin_init', 'itsg_add_author_setting_field');

// HTML for the settings field
function itsg_default_post_author_field_html() {
$value = get_option('default_post_author', 1); // Get the current value, default to 1

// Fetch users with 'Author', 'Editor', or 'Administrator' roles
$user_query = new WP_User_Query(array(
'role__in' => array('Author', 'Editor', 'Administrator'), // Array of roles
'orderby' => 'display_name',
'order' => 'ASC'
));

$users = $user_query->get_results();

// Create a dropdown list of users
echo '<select id="default_post_author" name="default_post_author">';
foreach ($users as $user) {
echo '<option value="' . esc_attr($user->ID) . '"' . selected($user->ID, $value, false) . '>' . esc_html($user->display_name) . '</option>';
}
echo '</select>';
}

// Set the default author for new posts
function itsg_force_post_author( $data, $postarr ) {
// Retrieve the default author ID from WordPress options, default to 1 if not set
$default_author_id = get_option('default_post_author', 1);

// Set the author for new posts
if (empty($postarr['ID'])) {
$data['post_author'] = $default_author_id;
}
return $data;
}
add_filter( 'wp_insert_post_data', 'itsg_force_post_author', 10, 2 );

// Set the revision author to the same author as the post
function itsg_set_revision_author($post_has_changed, $last_revision, $post) {
global $wpdb;

// Update the post_author of the revision to match the original post
$result = $wpdb->update(
$wpdb->posts,
array('post_author' => $post->post_author),
array('ID' => $last_revision->ID),
array('%d'),
array('%d')
);

// Basic error handling
if (false === $result) {
error_log('Failed to update revision author for post ID ' . $post->ID);
}

return $post_has_changed;
}
add_filter('wp_save_post_revision_check_for_changes', 'itsg_set_revision_author', 10, 3);