Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.