A picture of Jeff Brigman
Overcome
& Conquer

How to set a Default Post Author in WordPress

January 2, 2024

Unfortunately WordPress doesn’t allow you to set a default Post author but instead defaults to the current user logged in (assuming they have author and above permissions) as the author. Well not anymore! This code snippet allows you to set the default author in Settings -> General -> Default Post Author. This will display all users who have the user roles of Author, Editor, and Admin. Simply select the user you want as the default and then click save. Going forward all new posts and pages will have the new default author set. You can change this author at any time like normal, this snippet does not override that function but simply sets the user you choose as the default.

This code does several things. First if a default author is not set, it will default to user 1 (if you have changed the Admin to a different user number, you will need to update the snippet to number associated so it can default to that), which is typically the Super Admin. Second, it only sets the default author for new posts, not already published posts. Third, it does some basic error handling in the event it doesn’t properly set the default editor you’ve chosen to prevent a fatal error on the site. So no matter what, it will always default back to user 1 or the currently logged in user (unless you change it in the code) to prevent any database errors.

<?php 

// 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);

Help support my work by buying me a cup of coffee