Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save felipeelia/9f06fc046de9508bc87da91e30416afc to your computer and use it in GitHub Desktop.
Save felipeelia/9f06fc046de9508bc87da91e30416afc to your computer and use it in GitHub Desktop.
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 ) {
const fullInfo = $( this ).children( 'strong' ).first().html(),
name = fullInfo.replace( /\s\([^\)]+\)/gi, '' ),
username = /\(([^)]*)\)/.exec( fullInfo )[ 1 ];
let selectName = '';
$( this ).find( 'select' ).first().children( 'option' ).each( function() {
selectName = $( this ).html();
if ( selectName == fullInfo ) {
console.log( 'Comparing ' + selectName + ' to ' + fullInfo + '.\n' );
$( this ).attr( 'selected', 'selected' );
} else if ( selectName == name ) {
console.log( 'Comparing ' + selectName + ' to ' + name + '.\n' );
$( this ).attr( 'selected', 'selected' );
} else if ( selectName == username ) {
console.log( 'Comparing ' + selectName + ' to ' + username + '.\n' );
$( this ).attr( 'selected', 'selected' );
}
} );
if ( '0' === $( this ).find( 'select' ).first().val() ) {
$( this ).find( 'select' ).first().val( 1 ); // Default User.
}
} );
}( jQuery ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment