Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto-select authors for posts during WordPress import. They must already be assigned to the project. Enable jQuery in a plugin or functions.php, and paste this in the browser console.
(function($){
$('#authors li').each(function(key, value) {
var name = $(this).children('strong').first().html();
var re = /\s\([^\)]+\)/gi;
name = name.replace(re, '');
$(this).find('select').first().children('option').each(function(){
if ($(this).html() == name) {
console.log('Comparing ' + $(this).html() + ' to ' + name + ".\n");
$(this).attr('selected', 'selected');
}
});
});
})(jQuery);
@el-rotny
Copy link

el-rotny commented Jul 5, 2017

This is actually pretty clever. Thanks.

@christopherhodges
Copy link

christopherhodges commented Jan 6, 2018

Awesome work! Thank you!!

@MediaMaquina
Copy link

MediaMaquina commented Feb 4, 2018

Incredible that after 6 years we still have to rely on this.
This is an essential feature for large backups.
Yet useless emoji and vanity features get developed rapidly for the joy of none and the bloat of the core.

@yongzhihuang
Copy link

yongzhihuang commented Jun 18, 2018

best snippet ever! Thanks a million

@taylorleejones
Copy link

taylorleejones commented Jun 18, 2018

Wow. Incredible. Thank you!

@apherio
Copy link

apherio commented Sep 11, 2018

Give this man a cookie

@markfrommn
Copy link

markfrommn commented Nov 27, 2018

Seven years later, still needed and phreaking awesome.

@jcd20160929
Copy link

jcd20160929 commented Dec 1, 2018

There is a bug, I think. The macro doesn't like parenthesis () in names. But even so it saved me half a day's work.

@ashish8141
Copy link

ashish8141 commented Dec 3, 2018

That's great piece of code.

@Jack89ita
Copy link

Jack89ita commented Sep 12, 2019

You are the number one!

@thibaultseynaeve
Copy link

thibaultseynaeve commented Dec 29, 2019

Magic 😱 Works great, thanks!

@iamkingsleyf
Copy link

iamkingsleyf commented Mar 6, 2020

Still existing

@kylerboudreau
Copy link

kylerboudreau commented Oct 28, 2020

Can anyone expound on this for me? I'm not sure how to make this code work on my site. I've tried saving as a js file and including in functions.php but it's not working for me. Thanks anyone! (WordPress 5.5.1. Attempting to import custom post type with multiple authors)

@ggedde
Copy link

ggedde commented May 31, 2021

Thanks for this, but it didn't work for me as the author list now includes the Author login.
So I modified it a bit and this worked for me:

jQuery('#authors li').each(function(key, value) {
      var name = jQuery(this).children('strong').first().html();
      jQuery(this).find('select').first().children('option').each(function(){
          if (jQuery(this).html() === name) {
              jQuery(this).attr('selected', 'selected');
          }
      });
  });

I didn't need to include jQuery as it is already included in WP-Admin. So all that is needed is to paste it into your console and hit enter.

@itsmereal
Copy link

itsmereal commented Nov 11, 2021

Thanks for this, but it didn't work for me as the author list now includes the Author login. So I modified it a bit and this worked for me:

jQuery('#authors li').each(function(key, value) {
      var name = jQuery(this).children('strong').first().html();
      jQuery(this).find('select').first().children('option').each(function(){
          if (jQuery(this).html() === name) {
              jQuery(this).attr('selected', 'selected');
          }
      });
  });

I didn't need to include jQuery as it is already included in WP-Admin. So all that is needed is to paste it into your console and hit enter.

This is confirmed working as of Nov 2021 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment