EN RU

WP Author Switcher

Technical Documentation
Module version 1.05 · PHP 8.3‑ready

Description

WP Author Switcher is a WordPress plugin that allows administrators to easily bulk reassign posts from one author to another. It provides a straightforward interface in the WordPress dashboard to select a batch of posts (by post type, status, and current author) and then assign them to a new author with a single click. This is especially useful for sites with multiple authors, content migrations, or when an author leaves and you need to transfer their posts to someone else.

Key Features:

  • Bulk Author Switching: Update the author on any number of posts/pages at once, saving time over editing each post individually.
  • Flexible Filtering: Choose which posts to affect by filtering by post type (posts, pages, or any public custom post type), post status (published, draft, pending, etc.), and/or the current author. You can target posts by a specific author or even all authors.
  • Single Click Operation: After selecting your filters and the new author, simply click the Run Author Switcher button. The plugin will update all matching posts and report how many posts were changed.
  • Admin Dashboard Integration: The tool is conveniently accessible from the WordPress admin area (under the Tools menu for each site). Only users with administrative privileges can use the author switcher, preventing unauthorized changes.
  • Multisite Compatible: Fully supports WordPress Multisite installations. Network administrators can activate the plugin network-wide, and each site admin can then use the tool on their individual site. (See Multisite Support below for details.)
  • Internationalization (i18n) Ready: All interface text can be translated. The plugin includes a .pot file and loads translations from the standard WordPress languages directory, making it easy to localize into any language.

WP Author Switcher makes it simple to manage and reassign content authors without any coding. It does not modify post content or other post data — it only updates the author attribution of posts. This ensures that aside from the author field, all other content remains unchanged. After running the tool, you will get a confirmation message indicating how many posts were updated to the new author.

Installation

Installing the WP Author Switcher plugin is very straightforward. You can install it directly from the WordPress plugin dashboard or manually by adding the plugin files to your server:

Automatic Installation

  1. Log in to your WordPress dashboard. From the admin menu, go to Plugins → Add New.
  2. In the Add Plugins page, search for «WP Author Switcher» in the plugin repository search bar.
  3. Once you find WP Author Switcher in the results, click Install Now, and wait for the installation to complete.
  4. After installation, click Activate. The plugin will be activated and ready to use. You will find the author switcher tool under the Tools menu in your WordPress admin sidebar.

Manual Installation

  1. Download the plugin ZIP file (for example, from the WordPress.org plugin page or another source).
  2. Unzip the downloaded file. You should get a folder named wp-author-switcher containing the plugin files (including wp-author-switcher.php).
  3. Using an FTP client or your hosting file manager, upload the entire wp-author-switcher folder to your WordPress installation’s wp-content/plugins/ directory.
  4. Log in to your WordPress admin dashboard and navigate to the Plugins page. Find WP Author Switcher in the list of installed plugins.
  5. Click Activate to activate the plugin. Once activated, the author switching tool will be available under Tools → WP Author Switcher in the admin menu.

Usage

Using WP Author Switcher is simple and intuitive. Once the plugin is installed and activated, follow these steps to bulk update post authors:

  1. Navigate to the Author Switcher tool: In the WordPress admin dashboard, go to Tools → WP Author Switcher. This will open the plugin’s author switching interface. (Note: You must be an Administrator or have the ability to manage site options to access this page.)
  2. Select a Post Type: Choose the type of content you want to update. For example, you can select Posts, Pages, or any other public custom post type active on your site. This will restrict the operation to only that content type.
  3. Select a Post Status: Choose the status of posts to target. Options include Published, Draft, Pending Review, Scheduled, Private, or All (any status). For instance, if you only want to reassign published posts, select «Published». Selecting «All (any status)» will include posts in any status except those in Trash.
  4. Select the Current Author (to change from): From the dropdown list of users, select the author whose posts you want to reassign. You can choose a specific user or select All authors to change posts from any author. (Choosing «All authors» will include posts by everyone on the site.)
  5. Select the New Author (to assign to): From the second dropdown, select the user account that should become the new author of the chosen posts. This list shows all users on the site. Important: Make sure to select a different user than the one in the previous step.
  6. Run the tool: Double-check your selections, then click the Run Author Switcher button. The plugin will process all posts that match your filters and update their author to the new selected user. Depending on how many posts are being updated, this may take a moment.
  7. Review the results: After running, the page will display a notification message at the top indicating the outcome. For example, «25 posts have been reassigned to the new author.» This confirms how many posts were affected. If no posts matched your criteria, you’ll see a message stating that no posts were found and no changes were made. If there was an issue (such as not selecting a new author), an error message will appear guiding you to correct the input.

Notes:

  • You cannot assign posts to the same author they already belong to. The plugin will alert you if you accidentally choose the same person as both the old and new author (in which case no change is necessary). Simply pick a different new author and run again.
  • The tool will not delete or hide any content. If some posts were already authored by the new author, they will remain authored by that user (those posts essentially remain unchanged, even though they are counted in the processing). All other matching posts will be updated to the new author.
  • This operation is irreversible via the tool, so if you want to revert changes, you would need to run the tool again swapping the authors back, or manually edit posts. It’s always a good idea to double-check your selections before running the switch.

Multisite Support

WP Author Switcher fully supports WordPress Multisite installations:

  • Network Activation: As a Network Administrator, you can Network Activate the plugin to enable it on all sites in your multisite network. The plugin’s functionality is scoped to individual sites, meaning each site administrator can use the author switcher for their site’s content.
  • Site Administration: Once network-activated, each site’s admin dashboard will have the WP Author Switcher tool under Tools. Site admins can bulk reassign authors on their site only. This ensures that administrators of each site can manage their own content authors without affecting other sites in the network.
  • Per-Site Operation: The plugin does not currently provide a way to switch authors across all sites in one action. If you need to reassign authors on multiple sites, you should run the tool separately on each site. This design prevents any accidental changes network-wide and keeps the scope of changes limited to the intended site.
  • User Accounts: In a multisite environment, users might exist network-wide. WP Author Switcher will list the users who are members of the specific site you’re working on. Make sure the user you want to assign as the new author is added to the site (with any role) so that they appear in the dropdown. The plugin will still assign posts to a new author even if that user doesn’t currently have a role on the site, but for consistency and proper permissions, it’s recommended that the new author has an account on the site.

In summary, no special configuration is required for multisite beyond network activating the plugin. The author switching process works the same on each site in the network, and only users with the appropriate permissions on a given site (typically Administrators) can perform author changes for that site’s content.

Internationalization (Translation)

This plugin is fully internationalized and ready for translation. All front-end and admin interface strings are wrapped in WordPress translation functions, so they can be translated into any language.

  • Text Domain: wp-author-switcher – This is the unique identifier for translation of this plugin’s strings.
  • Translation Files: A template .pot file is provided in the /languages/ folder of the plugin. Translators can use this file to create .po and .mo files for their target language.
  • Loading Translations: WP Author Switcher automatically loads the corresponding language file if it exists. For example, if your site language is set to Spanish and a wp-author-switcher-es_ES.mo file is present in the plugin’s languages folder (or in the global WP languages directory), the plugin’s interface will appear in Spanish.
  • Contributing Translations: Contributions for new translations or updates are welcome. You can use a tool like Poedit or the Loco Translate plugin to create translation files. If the plugin is hosted on WordPress.org, you may also contribute translations via the translate.wordpress.org interface for the wp-author-switcher text domain.

By providing full internationalization support, we ensure that WP Author Switcher can be used by people all over the world in their native languages.

Changelog

1.06.0

  • Initial public release of WP Author Switcher (renamed from the previous «Author Switcher» module).
  • Updated the plugin for compatibility with WordPress 6.x (tested up to WordPress 6.8.2).
  • Added full internationalization (i18n) support with a text domain and included a POT file for translations.
  • Ensured support for both single-site and multisite installations of WordPress.
  • Code refactored and documentation added to meet WordPress Plugin Directory guidelines (sanitization, capability checks, etc.).

(This is the first release on the WordPress.org plugin repository. Older internal versions up to 1.05/1.06 were used privately and have now been polished for public use.)

Development & Support

WP Author Switcher is an open-source project licensed under the GPL v2 (or later). We welcome user feedback and contributions. If you encounter any issues or have suggestions for improvements, please feel free to reach out (for example, via the plugin support forum on WordPress.org once the plugin is published). Future development will aim to address any reported bugs, add new features if beneficial, and maintain compatibility with the latest WordPress versions.


Plugin Authors: Mikhail Deynekin.
License: GNU General Public License v2 (or later) – You are free to use, modify, and distribute this plugin under the same license terms.


Plugin Code

Below is the complete source code of the WP Author Switcher plugin (version 1.06.0), with inline documentation and proper coding standards, ready to be placed in the /wp-content/plugins/ directory as wp-author-switcher.php:

<?php
/**
 * Plugin Name: WP Author Switcher
 * Description: Allows administrators to bulk reassign posts from one author to another. Supports multiple post types and statuses, and is compatible with both single-site and multisite WordPress installations.
 * Version: 1.06.0
 * Author: WP Author Switcher Team
 * Text Domain: wp-author-switcher
 * Domain Path: /languages
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

 // Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Load the plugin textdomain for translation files.
 * This allows the plugin to be translated into other languages.
 */
function wpas_load_textdomain() {
    load_plugin_textdomain( 'wp-author-switcher', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'wpas_load_textdomain' );

/**
 * Register the WP Author Switcher admin page in the WordPress dashboard.
 * The page will appear under the "Tools" menu for users with appropriate capability.
 */
function wpas_register_admin_page() {
    // Only add the menu in regular site admin (not in network admin) and only for users with manage_options capability.
    if ( is_network_admin() || ! current_user_can( 'manage_options' ) ) {
        return;
    }
    add_management_page(
        __( 'WP Author Switcher', 'wp-author-switcher' ),    // Page title (localized)
        __( 'WP Author Switcher', 'wp-author-switcher' ),    // Menu title (localized) as it appears under Tools menu
        'manage_options',                                    // Capability required to access this page
        'wp-author-switcher',                                // Menu slug (unique identifier for the page)
        'wpas_render_admin_page'                             // Callback function that outputs the page HTML
    );
}
add_action( 'admin_menu', 'wpas_register_admin_page' );

/**
 * Render the content of the WP Author Switcher admin page.
 * This function also handles the form submission for switching authors.
 */
function wpas_render_admin_page() {
    // Security check: ensure the current user has permission to manage options (should match the capability in menu registration).
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( __( 'You do not have permission to access this page.', 'wp-author-switcher' ) );
    }

    // Initialize variables for status messages.
    $message      = '';
    $message_class = 'updated';  // CSS class for admin notice: 'updated' (green/info) or 'error' (red).

    // Process form submission if the user has clicked the "Run Author Switcher" button.
    if ( isset( $_POST['wpas_submit'] ) ) {
        // Verify the nonce for security to ensure the request is valid.
        check_admin_referer( 'wpas_switch_authors_nonce' );

        // Sanitize and retrieve form fields.
        $post_type    = isset( $_POST['wpas_post_type'] ) ? sanitize_text_field( $_POST['wpas_post_type'] ) : '';
        $post_status  = isset( $_POST['wpas_status'] ) ? sanitize_text_field( $_POST['wpas_status'] ) : '';
        $old_author_id = isset( $_POST['wpas_old_author'] ) ? intval( $_POST['wpas_old_author'] ) : 0;
        $new_author_id = isset( $_POST['wpas_new_author'] ) ? intval( $_POST['wpas_new_author'] ) : 0;

        // Validate the input to ensure required selections are made.
        if ( empty( $post_type ) || empty( $post_status ) ) {
            // Post type or status was not selected.
            $message       = __( 'Please select both a post type and a post status before running the author switcher.', 'wp-author-switcher' );
            $message_class = 'error';
        } elseif ( $new_author_id <= 0 ) {
            // New author was not selected (or an invalid value).
            $message       = __( 'Please select a valid new author to assign the posts to.', 'wp-author-switcher' );
            $message_class = 'error';
        } elseif ( $old_author_id === $new_author_id && $old_author_id !== 0 ) {
            // The old author and new author are the same (and not using "All authors" option which is 0).
            $message       = __( 'The old author and new author are the same. Please choose a different new author to switch to.', 'wp-author-switcher' );
            $message_class = 'error';
        } else {
            // Set up query arguments to fetch posts that match the selected criteria.
            $query_args = array(
                'post_type'      => $post_type,
                'posts_per_page' => -1,  // Retrieve all matching posts.
                'post_status'    => ( $post_status === 'any' ) 
                                     ? array( 'publish', 'pending', 'draft', 'future', 'private' ) 
                                     : $post_status,
                'fields'         => 'ids'  // We only need an array of post IDs for processing.
            );
            if ( $old_author_id > 0 ) {
                // If a specific current author is selected, filter by that author.
                $query_args['author'] = $old_author_id;
            }
            // Get all posts that meet the criteria.
            $posts_to_update = get_posts( $query_args );

            if ( $posts_to_update && count( $posts_to_update ) > 0 ) {
                $updated_count = 0;
                // Loop through each post and update its author.
                foreach ( $posts_to_update as $post_id ) {
                    // Prepare the post data for update.
                    $updated_post = array(
                        'ID'          => $post_id,
                        'post_author' => $new_author_id
                    );
                    // Use wp_update_post to update the post author.
                    $result = wp_update_post( $updated_post, true );
                    if ( ! is_wp_error( $result ) ) {
                        // Increment the counter if update was successful.
                        $updated_count++;
                    }
                }
                // After processing all posts, set a success message.
                $message = sprintf(
                    // Use _n to get correct pluralization for the number of posts.
                    _n( '%d post has been reassigned to the new author.', '%d posts have been reassigned to the new author.', $updated_count, 'wp-author-switcher' ),
                    $updated_count
                );
                $message_class = 'updated';  // success/info notice
            } else {
                // No posts matched the criteria, so nothing was updated.
                $message       = __( 'No posts were found for the selected criteria. No changes have been made.', 'wp-author-switcher' );
                $message_class = 'updated';  // using 'updated' class for a neutral information message
            }
        }
    }

    // Fetch all suitable post types for the dropdown (publicly visible post types with UI).
    $post_types = get_post_types( array( 'show_ui' => true ), 'objects' );
    // Exclude post types that are not content-bearing or shouldn't be bulk-author-switched.
    unset( $post_types['attachment'], $post_types['revision'], $post_types['nav_menu_item'], $post_types['custom_css'], $post_types['customize_changeset'] );
    // (Attachments are media files – changing their author is usually not needed in bulk. 
    // Revisions, nav_menu_items, custom CSS, etc., are not relevant for author reassignment.)

    // Fetch all users of the current site for the author dropdowns.
    // We will list all users with any role on this site.
    $users = get_users( array(
        'orderby' => 'display_name',
        'order'   => 'ASC'
    ) );

    // Define the set of post statuses for the status dropdown.
    $statuses = array(
        'publish' => __( 'Published', 'wp-author-switcher' ),
        'draft'   => __( 'Draft', 'wp-author-switcher' ),
        'pending' => __( 'Pending Review', 'wp-author-switcher' ),
        'future'  => __( 'Scheduled', 'wp-author-switcher' ),
        'private' => __( 'Private', 'wp-author-switcher' ),
        'any'     => __( 'All (any status)', 'wp-author-switcher' )
    );

    // Begin outputting the admin page HTML.
    echo '<div class="wrap">';
    echo '<h1>' . esc_html__( 'WP Author Switcher', 'wp-author-switcher' ) . '</h1>';

    // If a message is set (error or success), display it as a WordPress admin notice.
    if ( ! empty( $message ) ) {
        echo '<div class="notice ' . esc_attr( $message_class ) . '"><p>' . esc_html( $message ) . '</p></div>';
    }

    // Description/instructions for the tool.
    echo '<p>' . esc_html__( 'Use this tool to reassign posts from one author to another in bulk. Select the content you want to change using the options below, then click the "Run Author Switcher" button to apply the changes.', 'wp-author-switcher' ) . '</p>';

    // Start the form for input selection.
    echo '<form method="post" action="">';
    // Output a security nonce field to protect against CSRF.
    wp_nonce_field( 'wpas_switch_authors_nonce' );

    echo '<table class="form-table">';

    // Post Type selection field.
    echo '<tr>';
    echo '<th scope="row"><label for="wpas_post_type">' . esc_html__( 'Post type', 'wp-author-switcher' ) . ':</label></th>';
    echo '<td><select name="wpas_post_type" id="wpas_post_type" required>';
    echo '<option value="">' . esc_html__( 'Select post type', 'wp-author-switcher' ) . '</option>';
    foreach ( $post_types as $pt ) {
        // $pt is a WP_Post_Type object.
        echo '<option value="' . esc_attr( $pt->name ) . '" ' . selected( isset( $post_type ) ? $post_type : '', $pt->name, false ) . '>';
        echo esc_html( $pt->labels->name );
        echo '</option>';
    }
    echo '</select></td>';
    echo '</tr>';

    // Post Status selection field.
    echo '<tr>';
    echo '<th scope="row"><label for="wpas_status">' . esc_html__( 'Post status', 'wp-author-switcher' ) . ':</label></th>';
    echo '<td><select name="wpas_status" id="wpas_status" required>';
    echo '<option value="">' . esc_html__( 'Select status', 'wp-author-switcher' ) . '</option>';
    foreach ( $statuses as $status_value => $status_label ) {
        echo '<option value="' . esc_attr( $status_value ) . '" ' . selected( isset( $post_status ) ? $post_status : '', $status_value, false ) . '>';
        echo esc_html( $status_label );
        echo '</option>';
    }
    echo '</select></td>';
    echo '</tr>';

    // Current Author (old author) selection field.
    echo '<tr>';
    echo '<th scope="row"><label for="wpas_old_author">' . esc_html__( 'Current author (to change FROM)', 'wp-author-switcher' ) . ':</label></th>';
    echo '<td><select name="wpas_old_author" id="wpas_old_author">';
    // "All authors" option (value 0 means no filtering by specific author).
    echo '<option value="0">' . esc_html__( 'All authors', 'wp-author-switcher' ) . '</option>';
    foreach ( $users as $user ) {
        // Determine a display name for the user (show username in parentheses if display name is different).
        $display_name = $user->display_name;
        $username     = $user->user_login;
        $name_to_show = $display_name ? $display_name : $username;
        if ( $display_name && $display_name !== $username ) {
            $name_to_show .= ' (' . $username . ')';
        }
        echo '<option value="' . esc_attr( $user->ID ) . '" ' . selected( isset( $old_author_id ) ? $old_author_id : '', $user->ID, false ) . '>';
        echo esc_html( $name_to_show );
        echo '</option>';
    }
    echo '</select> ';
    echo '<p class="description">' . esc_html__( 'Choose the current author of the posts you want to change. Select "All authors" to include posts by any author.', 'wp-author-switcher' ) . '</p>';
    echo '</td></tr>';

    // New Author selection field.
    echo '<tr>';
    echo '<th scope="row"><label for="wpas_new_author">' . esc_html__( 'New author (to change TO)', 'wp-author-switcher' ) . ':</label></th>';
    echo '<td><select name="wpas_new_author" id="wpas_new_author" required>';
    echo '<option value="">' . esc_html__( '-- Select new author --', 'wp-author-switcher' ) . '</option>';
    foreach ( $users as $user ) {
        $display_name = $user->display_name;
        $username     = $user->user_login;
        $name_to_show = $display_name ? $display_name : $username;
        if ( $display_name && $display_name !== $username ) {
            $name_to_show .= ' (' . $username . ')';
        }
        echo '<option value="' . esc_attr( $user->ID ) . '" ' . selected( isset( $new_author_id ) ? $new_author_id : '', $user->ID, false ) . '>';
        echo esc_html( $name_to_show );
        echo '</option>';
    }
    echo '</select> ';
    echo '<p class="description">' . esc_html__( 'Select the new author to assign to the chosen posts.', 'wp-author-switcher' ) . '</p>';
    echo '</td></tr>';

    // Submit button to run the author switch.
    echo '<tr>';
    echo '<th scope="row"></th>';  // empty header cell
    echo '<td><input type="submit" name="wpas_submit" class="button button-primary" value="' . esc_attr__( 'Run Author Switcher', 'wp-author-switcher' ) . '"></td>';
    echo '</tr>';

    echo '</table>';
    echo '</form>';
    echo '</div>'; // end .wrap
}
?>

(Save the above code as wp-author-switcher.php in the plugin folder. Ensure there is a corresponding languages directory with a .pot file for translations if you plan to localize the plugin. After that, you can activate the plugin and use it as described.).

Разработка и продвижение сайтов webseed.ru
Прокрутить вверх