Automatically Assigning Users with the WordPress Importer

Automatically Assigning Users with the WordPress Importer

Recently, one of our clients enlisted our help with migrating thousands of WordPress posts to another server. At 515 Media, we like to use the official WordPress Importer plugin for such assignments – it reliably preserves all content formatting, and works perfectly in tandem with WordPress’ native XML generator.

Here’s the problem…

As much as we like the WordPress Importer, we ran into a frustrating limitation of the software this time around. You see, our client’s posts were authored by nearly 300 different user accounts, and the WordPress Importer tool did not automatically map these accounts to our new database.

Instead, the plugin will prompt you to create associations for each user by hand:

As you can imagine, this is quite a laborious process if you’re dealing with a database containing dozens or hundreds of published users! Our hearts go out to those of you who have had to map this many fields by hand – we’ve definitely been there before!

That’s why we came up with a simple JavaScript-based solution to completely automate the user assignment process. Today, we would like to share our code with all of you:

Our solution

// ==UserScript==
// @name         Import WordPress - Auto Assign Authors
// @namespace    https://515.media
// @version      0.1
// @description  Quickly fill out post authors
// @author       Eric Black of 515 Media
// @include       */wp-admin/admin.php?import=wordpress*
// @grant        none
// ==/UserScript==
var $ = window.jQuery;
$(document).ready(function() {
    $("#authors").before("<input type='button' id='auto-assign-authors' value='Automatically assign authors'>");
    $("#auto-assign-authors").click(function() {
        var missing = 0;
        $("#authors > li").each(function() {
            var full_author = $("strong",this).text();
            var this_author = full_author.substr(0, full_author.indexOf(' ('));
            var is_missing = true;
            $("select option",this).each(function(){
                if ($(this).text() == this_author) {
                    $(this).attr('selected', true);
                    is_missing = false;
                    return false;
                }
            });
            if (is_missing) missing++;
        });
        $("#auto-assign-authors").after("<p>Finished! Total fields without matches: " + missing + "</p>");
    });
});

 

You may install our code to any major web browser with an extension that manages userscripts. We recommend TamperMonkey for this purpose (Firefox / Chrome).

Simply choose to “Create a New Script” and paste our code into the TamperMonkey editor.

The next time WordPress Importer prompts you for user assignment, you will find a new button at the top of the page to fully automate the procedure:

In a matter of seconds, our script will populate every field without any further input.

Plus, in the event that some of your database’s usernames don’t quite match those from your imported XML, our script will tell you how many fields are still blank after the operation is complete (# of unmatched fields.)

You are more than welcome to use this script for free and adapt it to your needs. (We only ask that you don’t claim it as your own or try to profit from it!)

If you have any questions about the installation process or general usage, feel free to get in touch with us today. Also, don’t forget to check out our managed hosting plans if you want our help developing custom scripts for your own projects!

Leave a Reply

Your email address will not be published. Required fields are marked *