Skip to content

Instantly share code, notes, and snippets.

@rxnlabs
Created December 22, 2019 14:53
Show Gist options
  • Save rxnlabs/2f195fc586f27a748ff6f8b2731c5bf7 to your computer and use it in GitHub Desktop.
Save rxnlabs/2f195fc586f27a748ff6f8b2731c5bf7 to your computer and use it in GitHub Desktop.
JS - WP (WordPress) admin replace featured image when we don't have access to SSH
/**
Run as a bookmarklet in your browser. Run in the WordPress admin area. Set the WordPress admin posts view number really high from the default 20 posts to something like 500 posts to decrease the number of times we need to run this.
Make sure to disable your browser's popup blocker since this will open a LOT of windows.
Update the featured image of all of the posts that in the WordPress admin on the Posts table page
*/
(function() {
/*Check if a value is a positive, whole number integer*/
function isNormalInteger(str) {
varn = Math.floor(Number(str));
returnString(n) === str && n >= 0;
}
// Go through the WordPress Admin Posts table and click the edit button of each of the rows
jQuery.map(jQuery('span.edita'), function(node, index) {
var $node = jQuery(node);
var popup = window.open($node.prop('href'), '_blank',
'width=800,height=800');
popup.onload = function() {
var this_popup = this;
setTimeout(function() {
// use the popup's document and search for the Node with the ID of #content
var content = popup.document.documentElement.querySelector('#content').textContent;
/* if the post has no content and the post does not have a featured image set already, then set a featured image for the post */
if ( content.indexOf('img src') == -1 && !isNormalInteger(popup.document.documentElement.querySelector('#_thumbnail_id').value ) ) {
console.log( 'found NO image in post content on post ' + popup.document.documentElement.querySelector('#title').textContent +' that has a post id '+ popup.document.documentElement.querySelector('#post_ID').value );
// update the featured image to teh specified thumbnail
popup.document.documentElement.querySelector('#_thumbnail_id').value = '9886';
popup.document.documentElement.querySelector('#publish').click();
popup.document.documentElement.querySelector('#publish').click();
} else {
/* if we have found a image for this post, just close the window instead of waiting for */
this_popup.close();
}
}, 1000);
}
setTimeout(function() {
popup.close();
}, 30000);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment