Last active
October 8, 2015 09:45
-
-
Save flackend/9f2956f3d682f9099e47 to your computer and use it in GitHub Desktop.
I wrote this to automate somewhat the task of moving some WordPress data to a new install. Didn't have Administrative permissions or server access so had to manually copy and paste. This method made it a lot faster. One script generates a payload and the other uses that payload to fill the form fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GENERATE | |
function getScript(url, callback) { | |
var head = document.documentElement, | |
script = document.createElement('script'), | |
done = false; | |
script.src = url; | |
script.onload = script.onreadystatechange = function() { | |
if(!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) { | |
done = true; | |
if(callback !== undefined) { | |
callback(); | |
} | |
script.onload = script.onreadystatechange = null; | |
if(head && script.parentNode) { | |
head.removeChild(script); | |
} | |
} | |
}; | |
head.insertBefore(script, head.firstChild); | |
return undefined; | |
} | |
// Get jQuery | |
getScript('https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js', function() { | |
$('[name="lolfmkbox_portfolio_type"]').attr('id', 'lolfmkbox_portfolio_type'); | |
var fieldIds = [ | |
'title', | |
'content', | |
'lolfmkbox_portfolio_type', | |
'lolfmkbox_gallery_shortcode', | |
'lolfmkbox_portfolio_desc', | |
'lolfmkbox_portfolio_date', | |
'lolfmkbox_portfolio_date_label', | |
'lolfmkbox_portfolio_client', | |
'lolfmkbox_portfolio_client_label', | |
'lolfmkbox_portfolio_skills', | |
'lolfmkbox_portfolio_skills_label', | |
'lolfmkbox_portfolio_url', | |
'lolfmkbox_portfolio_url_label', | |
'slide_template', | |
'yoast_wpseo_focuskw', | |
'yoast_wpseo_title', | |
'yoast_wpseo_metadesc' | |
]; | |
var fields = {}; | |
for (var i in fieldIds) { | |
fields[fieldIds[i]] = $('#' + fieldIds[i]).val(); | |
} | |
var tags = []; | |
$('.tagchecklist span').each(function() { | |
$(this).find('a').remove(); | |
tags.push($(this).text()); | |
}); | |
fields['new-tag-portfolio-categories'] = tags.join(','); | |
console.log(JSON.stringify(fields)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// POPULATE | |
function getScript(url, callback) { | |
var head = document.documentElement, | |
script = document.createElement('script'), | |
done = false; | |
script.src = url; | |
script.onload = script.onreadystatechange = function() { | |
if(!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) { | |
done = true; | |
if(callback !== undefined) { | |
callback(); | |
} | |
script.onload = script.onreadystatechange = null; | |
if(head && script.parentNode) { | |
head.removeChild(script); | |
} | |
} | |
}; | |
head.insertBefore(script, head.firstChild); | |
return undefined; | |
} | |
// Get jQuery | |
getScript('https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js', function() { | |
// Add ID for type | |
$('[name="lolfmkbox_portfolio_type"]').attr('id', 'lolfmkbox_portfolio_type'); | |
// Loop and set values | |
for (var i in d) { | |
if (i == 'content') { | |
console.log('setting content', d[i]); | |
tinyMCE.activeEditor.setContent(d[i]); | |
} else { | |
console.log('find #' + i + ' and set to ' + d[i]); | |
$('body').find('#' + i).val(d[i]); | |
} | |
} | |
$('#lolfmkbox_portfolio_type').change(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment