Image caption // add attribute to img
-
Hello,
I try to modify the shortcode [caption] to add the description and the my custom field “copyright” to the <img>.
Help with some posts i read to use html5 balise, i add the following code to my function.php:
add_filter( 'img_caption_shortcode', 'cleaner_caption', 10, 3 ); if ( !function_exists('cleaner_caption')) { 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' => '', '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'] || empty( $attr['caption'] ) ) return $content; /* Set up the attributes for the caption. */ $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 = '<figure' . $attributes .'>'; /* Allow shortcodes for the content the caption was created for. */ $output .= do_shortcode( $content ); /* Add information to the $output. */ if ( get_featured_image_copyright() != '© ' ) { $copyright = get_featured_image_copyright(); } else { $copyright = get_post(get_post_thumbnail_id())->post_title; }; $description = get_post(get_post_thumbnail_id())->post_content; $title = get_post(get_post_thumbnail_id())->post_title; $output = str_replace( '></a>','description="'.$description.'" title="'.$copyright.'" title-meta="'.$title.'"></a>',$output); /* Append the caption text. */ $output .= '<figcaption class="wp-caption-text">' . $attr['caption'] . '</figcaption>'; /* Close the caption. */ $output .= '</figure>'; $output .= '<p class="wp-description-text">'.$description.'</p>'; /* Return the formatted, clean caption. */ return $output; } } //endif !function_existsThe problem is the code don’t work for all the post.
For example when it works, i get the normal code:<figure class="wp-caption" itemscope itemtype="http://schema.org/ImageObject" > <a href="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2030/07/mon-beau-camion.jpg" > <img width="625" height="315" src="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2030/07/mon-beau-camion.jpg" class="attachment-large wp-post-image" alt="If you say it loud enough, you’ll always sound precocious; Supercalifragilisticexpialidocious!" itemprop="contentURL" title-meta="Mon beau camion" title="© Mon copyright" description="Ma descritpion" /></a> <figcaption class="wp-caption-text" itemprop="description">Un régal!</figcaption></figure>For image post with link, i get this wrong code:
<figure id="attachment_619" class="wp-caption alignnone" style="width: 640px">magnificPopup=Object { tClose="Fermer (Esc)", type="image", tLoading="Chargement de l'image #%curr%...", more...} <a href="http://wpthemetestdata.files.wordpress.com/2012/06/dsc20040724_152504_532.jpg"> <img class="size-full wp-image-619" width="640" height="480" align="alignnone" href="http://wpthemetestdata.files.wordpress.com/2012/06/dsc20040724_152504_532.jpg" ]<a="" attachment_619"="" description="[caption id=" src="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2011/01/dsc20050901_105100_212.jpg" alt="Seed pods on stem, Woodvale"> <img class="size-full wp-image-619" width="640" height="480" src="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2011/01/dsc20050901_105100_212.jpg" alt="Seed pods on stem, Woodvale"> </a> Seed pods on stem, Woodvale[/caption]" title="" title-meta="Post Format Test: Image (Linked)"> <figcaption class="wp-caption-text">Seed pods on stem, Woodvale</figcaption> </figure>For event organiser post, i get the same wrong code:
<figure id="attachment_946" class="wp-caption alignnone" style="width: 1936px">magnificPopup=Object { tClose="Fermer (Esc)", type="image", tLoading="Chargement de l'image #%curr%...", more...} <a href="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2008/09/montagne-de-beurre.jpg"> <img class="size-full wp-image-946" width="1936" height="2592" align="alignnone" href="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2008/09/montagne-de-beurre.jpg" ]<a="" attachment_946"="" description="Alors merci! [caption id=" src="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2008/09/montagne-de-beurre.jpg" alt="En haut de la montagne"> <img class="size-full wp-image-946" width="1936" height="2592" src="http://jenfaisdestartines.mdl:8080/wp-content/uploads/2008/09/montagne-de-beurre.jpg" alt="En haut de la montagne"> </a> Très belle photo[/caption]" title="" title-meta="Event 3"> <figcaption class="wp-caption-text">Très belle photo</figcaption> </figure>For information, the problem is the same when the post is into the loop, and when it on a single page.
Any idea will be welcomed.
Thank you in advance,
jB
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘Image caption // add attribute to img’ is closed to new replies.