WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do you add a new class to the HTML generated when an image with captions is added?

For example, from this:

<div id="attachment_xyz" class="wp-caption alignleft"...

to this:

<div id="attachment_xyz" class="wp-caption alignleft my_new_class"...

Adding, in this case, my_new_class.

Thanks, Scott

share|improve this question
Did you read this answer? It is almost the same, the second parameter for str_replace just has to be changed to 'class="wp-caption '. – toscho Dec 3 '12 at 10:26
I wonder why when people try to help they actually get downvoted. Anyways, I still don't understand how to do this - that is, implement the code. Which code exactly would I use? Any help, with good intentions, would be appreciated. Exact code, in kindergarten language :) – scottdaris Dec 4 '12 at 1:50
up vote 3 down vote accepted

Adapted from this answer

Add this code to your functions.php file:

add_action( 'after_setup_theme', 'wpse_74735_replace_wp_caption_shortcode' );

 * Replace the default caption shortcode handler.
 * @return void
function wpse_74735_replace_wp_caption_shortcode() {
    remove_shortcode( 'caption', 'img_caption_shortcode' );
    remove_shortcode( 'wp_caption', 'img_caption_shortcode' );
    add_shortcode( 'caption', 'wpse_74735_caption_shortcode' );
    add_shortcode( 'wp_caption', 'wpse_74735_caption_shortcode' );

 * Add the new class to the caption.
 * @param  array  $attr    Shortcode attributes
 * @param  string $content Caption text
 * @return string
function wpse_74735_caption_shortcode( $attr, $content = NULL )
    $caption = img_caption_shortcode( $attr, $content );
    $caption = str_replace( 'class="wp-caption', 'class="wp-caption my_new_class', $caption );
    return $caption;
share|improve this answer
Any particular reason for the downvote? – shea Dec 3 '12 at 20:04
Mr. Bunge, I have no idea why people do this. Thanks for trying to help anyways! I still don't know how to do this - I'm one of those graphic designer types just getting into code. – scottdaris Dec 4 '12 at 1:51
Install the Code Snippets plugin and, after activation, visit the Snippets > Add New menu. Enter the above code into the code textarea and name the snippet. After adding the snippet, visit the Snippets > Manage Snippets menu to activate the snippet. Once these steps have been completed there will be an additional class on wp-caption (my_new_class). – shea Dec 4 '12 at 2:46
If you're designing a theme, you can also create a file in the theme directory called functions.php and paste this code into it. (Remember to add a <?php tag to the start of the file) – shea Dec 4 '12 at 2:47
OK, I know why people downvoted your answer. On the first line of add_action, your newly named function is called 'wpse_74735_replace_img_shortcodes' yet when that function is called it is misnamed as 'wpse_74735_replace_wp_caption_shortcode'. Also I noticed one more thing (you learn SO much from stuff that breaks!). The following line: 'class="wp-caption my_new_class' should have a space at the end so the newly named class name doesn't run into the next class as one long class name. But after these fixes, it works. Thanks for making me learn some new stuff by your mistakes :D – scottdaris Dec 4 '12 at 8:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.