Skip to content

Instantly share code, notes, and snippets.

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 justinallen/1d4b91bd7f29caf8f57dc880cec8ec5a to your computer and use it in GitHub Desktop.
Save justinallen/1d4b91bd7f29caf8f57dc880cec8ec5a to your computer and use it in GitHub Desktop.
WordPress / jQuery snippet for moving custom image classes up to the caption on captioned images
/**
* Hoist custom image classes up to the caption that wraps it
*/
$('figure.wp-caption').each(function() {
var caption = $(this);
caption.removeAttr('width').css('width', ''); // remove width attribute
var img = $(this).find('img');
var imgClasses = img.attr('class').split(/\s+/); // list of classes
imgClasses.forEach(function(element) {
img.removeClass(element); // remove from image
caption.addClass(element); // add to image's containing element
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment