Skip to content

Instantly share code, notes, and snippets.

@flackend
Last active October 8, 2015 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flackend/9f2956f3d682f9099e47 to your computer and use it in GitHub Desktop.
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.
// 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));
});
// 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