WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add new caption shortcode attribute (8 posts)

  1. Phoat
    Member
    Posted 5 years ago #

    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

  2. Phoat
    Member
    Posted 5 years ago #

    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.

  3. Mark / t31os
    Moderator
    Posted 5 years ago #

    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

  4. Phoat
    Member
    Posted 5 years ago #

    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.

  5. Phoat
    Member
    Posted 5 years ago #

    @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.

  6. Mark / t31os
    Moderator
    Posted 5 years ago #

    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)...

  7. rmvgaines
    Member
    Posted 4 years ago #

    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.

  8. jimisdon
    Member
    Posted 4 years ago #

    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>';
    }

Topic Closed

This topic has been closed to new replies.

About this Topic