Title: Add new caption shortcode attribute
Last modified: August 19, 2016

---

# Add new caption shortcode attribute

 *  Resolved [Phoat](https://wordpress.org/support/users/phoat/)
 * (@phoat)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/)
 * Right now, when inserting an image with a caption, WordPress adds a shortcode
   similar to this one…
 * `[caption id="attachment_68" align="alignnone" width="900" caption="Caption goes
   here!"]<img />[/caption]`
 * What I want to do is along with the id, align, width, and caption attributes 
   to assign another attribute called title so that I will get this…
 * `[caption id="attachment_68" align="alignnone" width="900" caption="Caption goes
   here!" title="Title of image caption"]<img />[/caption]`
 * Any way to do this?
 * Thanks

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [Phoat](https://wordpress.org/support/users/phoat/)
 * (@phoat)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1126805)
 * BTW, before anyone says it, I know I can type the title manually, but since I’m
   making this for someone not that familiar with code, I would prefer if it was
   done automatically by WP’s image editor.
 * Thanks for the help.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1126809)
 * File that handles that stuff is…
 * wp-includes/media.php ….
 * Not sure how to handle your query, but that would be where i start…
 * Around line 577
 *  Thread Starter [Phoat](https://wordpress.org/support/users/phoat/)
 * (@phoat)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1126985)
 * tried changing the function that handles the specific short code… but it didn’t
   do anything.
 * I’m pretty sure tho that it can be done without touching core files, but I have
   no problem doing so if there is no other way.
 *  Thread Starter [Phoat](https://wordpress.org/support/users/phoat/)
 * (@phoat)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1126986)
 * [@t31os_](https://wordpress.org/support/users/t31os_/)
    That function is for 
   what is generated when someone views a post. What I’m looking for should be in
   wp-admin/media.php, but when I tried to change the code, nothing happened.
 * Thanks for your comment.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1126995)
 * Yes that’s right…
 * You’d udpate this line..
 *     ```
       $shcode = '[caption id="' . $id . '" align="align' . $align
       	. '" width="' . $width . '" caption="' . $alt . '"]' . $html . '[/caption]';
       ```
   
 * with..
 *     ```
       $shcode = '[caption id="' . $id . '" align="align' . $align
       	. '" width="' . $width . '" caption="' . $alt . '" title="' . $title . '"]' . $html . '[/caption]';
       ```
   
 * Of course that’s a core modification solution…(it does work though)…
 *  [rmvgaines](https://wordpress.org/support/users/rmvgaines/)
 * (@rmvgaines)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1127213)
 * I tried the code above and I did not see any change. I’m not sure if anyone is
   still following this thread, but if so could they let me know if this should 
   still work correctly, and if not what a solution might be?
 * Thank you.
 *  [jimisdon](https://wordpress.org/support/users/jimisdon/)
 * (@jimisdon)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1127214)
 * Don’t update core files when you are dealing with shortcode! There is a neat 
   trick you can take advantage of. I know this doesn’t help your particular issue,
   but here is the trick:
 * First, find the function definition and the `add_shortcode()` that gets used 
   in `wp-includes/media.php` (or wherever). Copy and paste all of that into the`
   functions.php` file in your current theme.
 * Caption Shortcode example:
 *     ```
       add_shortcode('wp_caption', 'img_caption_shortcode');
       add_shortcode('caption', 'img_caption_shortcode');
   
       /**
        * The Caption shortcode.
        *
        * Allows a plugin to replace the content that would otherwise be returned. The
        * filter is 'img_caption_shortcode' and passes an empty string, the attr
        * parameter and the content parameter values.
        *
        * The supported attributes for the shortcode are 'id', 'align', 'width', and
        * 'caption'.
        *
        * @since 2.6.0
        *
        * @param array $attr Attributes attributed to the shortcode.
        * @param string $content Optional. Shortcode content.
        * @return string
        */
       function img_caption_shortcode($attr, $content = null) {
   
       	// Allow plugins/themes to override the default caption template.
       	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
       	if ( $output != '' )
       		return $output;
   
       	extract(shortcode_atts(array(
       		'id'	=> '',
       		'align'	=> 'alignnone',
       		'width'	=> '',
       		'caption' => ''
       	), $attr));
   
       	if ( 1 > (int) $width || empty($caption) )
       		return $content;
   
       	if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
   
       	return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
       	. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
       }
       ```
   
 * Next, rename the functions so you avoid errors when they are defined twice.
 * Last, rename the function that the shortcode calls to match the new name of your
   function. Don’t rename the shortcode. It will simply be overwritten! Now your
   shortcode will do what you want and it will work wherever your theme is installed!
 * example of renamed caption code:
 *     ```
       add_shortcode('wp_caption', 'MY_VERY_OWN_img_caption_shortcode');
       add_shortcode('caption', 'MY_VERY_OWN_img_caption_shortcode');
   
       /**
        * The Caption shortcode.
        *
        * Allows a plugin to replace the content that would otherwise be returned. The
        * filter is 'img_caption_shortcode' and passes an empty string, the attr
        * parameter and the content parameter values.
        *
        * The supported attributes for the shortcode are 'id', 'align', 'width', and
        * 'caption'.
        *
        * @since 2.6.0
        *
        * @param array $attr Attributes attributed to the shortcode.
        * @param string $content Optional. Shortcode content.
        * @return string
        */
       function MY_VERY_OWN_img_caption_shortcode($attr, $content = null) {
   
       	// Allow plugins/themes to override the default caption template.
       	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
       	if ( $output != '' )
       		return $output;
   
       	extract(shortcode_atts(array(
       		'id'	=> '',
       		'align'	=> 'alignnone',
       		'width'	=> '',
       		'caption' => ''
       	), $attr));
   
       	if ( 1 > (int) $width || empty($caption) )
       		return $content;
   
       	if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
   
       	return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
       	. do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
       }
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Add new caption shortcode attribute’ is closed to new replies.

## Tags

 * [attributes](https://wordpress.org/support/topic-tag/attributes/)
 * [caption](https://wordpress.org/support/topic-tag/caption/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 7 replies
 * 4 participants
 * Last reply from: [jimisdon](https://wordpress.org/support/users/jimisdon/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/add-new-caption-shortcode-attribute/#post-1127214)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
