0

enter image description here

The img alt attribute is dsplaying as caption and laos the caption is displaying, I do not want to display the img alt attribute as caption , how can I remove it.

my code

 <p>
    [caption id="attachment_2103" align="alignnone" width="200"]
<a href="http://realwaymedia.info/aiwa-stage2/wp-content/uploads/2016/03/pid_24063.jpg">
<img class="wp-image-2103 size-medium" alt="Book Cover image" src="http://realwaymedia.info/aiwa-stage2/wp-content/uploads/2016/03/pid_24063-200x300.jpg"  width="200" height="300" /></a>
This is a caption[/caption]</p>
1

To remove caption text, you can override the caption short-code using below code:

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );

function my_img_caption_shortcode( $empty, $attr, $content ){
    $attr = shortcode_atts( array(
        'id'      => '',
        'align'   => 'alignnone',
        'width'   => '',
        'caption' => ''
    ), $attr );

    if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
        return '';
    }

    if ( $attr['id'] ) {
        $attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
    }

    return '<div ' . $attr['id']
    . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
    . 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
    . do_shortcode( $content )
    . '</div>';

}

For more details, refer CODEX

UPDATE:

Try changing below mentioned line.

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 999, 3 );

Hope this helps!

  • 1
    It seems it is already overridden , It is not showing the default behavior, as per codex, It should not display the image alt attribute , when the image is displaying .but in my case as the image shows, it can be seen. – Prafulla Kumar Sahu Mar 28 '16 at 13:12
  • The code you have post is from wp-include/media.php file, I am on that page, but we should not override this, It seems there is some other problem. – Prafulla Kumar Sahu Mar 28 '16 at 13:13
  • Try UPDATE code of my answer. Also the answer posted is not same as codex, it's a bit modified. – Mehul Gohil Mar 28 '16 at 13:15
  • It seems my question is little bit unclear to you, still your answer is helpful. As it will be a workaround, if I will be unable to solve it.It deserves an upvote. – Prafulla Kumar Sahu Mar 28 '16 at 13:40
  • The client was using another plugin name "Velvet Blues Image Captions" to display alt as caption :P – Prafulla Kumar Sahu Mar 28 '16 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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