Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Always add caption element to images even if caption is empty? WordPress

  • SOLVED

I always want to add the caption element to images even when it is empty. This is my filter but nothing happens:


add_filter('img_caption_shortcode', 'caption_shortcode_inline_text', 10, 3);
function caption_shortcode_inline_text($empty, $attr, $content) {
extract(shortcode_atts(array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr));

// in this part I amtrying to always add the caption element but it doesn't work:
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>';
}

Answers (3)

2015-10-17

Rempty answers:

The caption shortcode is added when you send the image from image uploader to editor, add this filter, don't delete your filter.(will force caption shortcode even if caption ="" )

add_filter( 'image_send_to_editor', 'tiny_force_caption', 100 );
function tiny_force_caption( $html ) {
$a = strpos($html, 'caption');
if ($a!=1) {
preg_match('/(alignnone|alignleft|alignright|aligncenter)/', $html,$c);
preg_match('/width="(\d*)"/', $html,$w);
preg_match('/alt="([^"]*)"/', $html,$m);
preg_match('/wp-image-(\d*)"/', $html,$n);
if (!isset($n[1])) $n[1] = '0';
$html = '[caption id="attachment_'.$n[1].'" align="'.($c?$c[1]:'alignnone').'" width="'.$w[1].'"]'.$html."[/caption]";
}
return $html;
}


herrfischer comments:

Nothing happens :-(


Rempty comments:

Is adding the "[caption]" when you insert the image, dont forget to use with your filter, i am testing on.
Switch tinymce from visual to text and insert image.


Rempty comments:

I found the error, just work on text mode not with visual editor.
Other solution is remove the caption shortcode, and include it as html, delete the filters, and add this to your functions.php
add_filter( 'disable_captions', create_function('$a', 'return true;') );

function image_send_to_editor_2($html, $id, $caption, $title, $align, $url, $size, $alt) {
$width = 'auto';
if ( preg_match( '/width="([0-9]+)/', $html, $matches ) ) {
$width = $matches[1] . 'px';
}

$output = '<div id="attachment-' . $id . '" class="wp-caption align' . $align . '" style="width: ' . $width . ';">';
$output .= $html;
$output .= '<p class="wp-caption-text">' . $caption . '</p>';
$output .= '</div>';

return $output;
}

add_filter('image_send_to_editor', 'image_send_to_editor_2', 10, 8);



herrfischer comments:

Yes it is adding caption when in text mode! … But unfortunately deletes it when switching to visual mode :-(


herrfischer comments:

No option to tell the customer he has to work in text mode ;) - but with your second approach it is working! YES baby! THX a lot!


herrfischer comments:

Oh, there is one problem: the regular wordpress caption field is gone now in the editor, i need it.


herrfischer comments:

only the "alt" text field is there: http://fs5.directupload.net/images/151017/idrkmknd.png

2015-10-17

Francisco Javier Carazo Gil answers:

Maybe with some jQuery you can do it.

Do you like this client side solution? I could prepare the code for you. Give me an URL and I prepare the JS code.


herrfischer comments:

No jquery is no option here because of several reasons.

2015-10-17

Romel Apuya answers:

here

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

function cleaner_caption( $output, $attr, $content ) {

/* We're not worried abut captions in feeds, so just return the output here. */
if ( is_feed() )
return $output;

/* Set up the default arguments. */
$defaults = array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
);

/* Merge the defaults with user input. */
$attr = shortcode_atts( $defaults, $attr );

/* If the width is less than 1 or there is no caption, return the content wrapped between the [caption]< tags. */
if ( 1 > $attr['width'])
return $content;

if(empty( $attr['caption'] )){
$attr['caption'] = 'Set Caption here';

}

/* Set up the attributes for the caption <div>. */
$attributes = ( !empty( $attr['id'] ) ? ' id="' . esc_attr( $attr['id'] ) . '"' : '' );
$attributes .= ' class="wp-caption ' . esc_attr( $attr['align'] ) . '"';
$attributes .= ' style="width: ' . esc_attr( $attr['width'] ) . 'px"';

/* Open the caption <div>. */
$output = '<div' . $attributes .'>';

/* Allow shortcodes for the content the caption was created for. */
$output .= do_shortcode( $content );

/* Append the caption text. */
$output .= '<p class="wp-caption-text">' . $attr['caption'] . '</p>';

/* Close the caption </div>. */
$output .= '</div>';

/* Return the formatted, clean caption. */
return $output;
}


herrfischer comments:

This does not work. Caption element is still only added when caption text is entered.


Romel Apuya comments:

Is the caption be displayed during the upload?? or just on the front end??


Romel Apuya comments:

alright...


remove_filter( 'image_send_to_editor', 'image_add_caption');
add_filter( 'image_send_to_editor', 'add_default_caption', 100, 8);
function add_default_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
if (substr($html,0,strlen('([0-9]+)/', $html, $matches ) )
return $html;

$width = $matches[1];

$caption = str_replace( array("rn", "r"), "n", $caption);
$caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );

$caption = preg_replace( '/[ nt]*n[ t]*/', '<br />', $caption );

$html = preg_replace( '/(class=["'][^'"]*)align(none|left|right|center)s?/', '$1', $html );
if ( empty($align) )
$align = 'none';

if (substr($html,0,strlen('[caption'))=='[caption')
return $html;

if (empty($caption))
$caption="Set Caption here";

$shcode = '' . $html . ' ' . $caption . '';
return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
}