20

function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => get_permalink( $attachment->ID ), 'src' =&...


11

You're going to want to use: @media (max-width: 988px){ .wp-caption { /* Force the box to be 100% */ width: 100% !important; } #content .wp-caption a img { /* Scale down if too big */ max-width: 99.03225806%; /* 614/620 */ height: auto; } }


8

Register the shortcode, ideally in a plugin or functions.php if you have to. add_shortcode('thumbnail', 'thumbnail_in_content'); function thumbnail_in_content($atts) { global $post; return get_the_post_thumbnail($post->ID); } Add the shortcode to you post content. [thumbnail] If you want more features, see this post or the pastebin. ADDING ...


7

Another possibility is to change the shortcode output so that the width is no longer hard-coded. Modifying the Codex example to have no width: add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3); /** * Filter to replace the [caption] shortcode text with HTML5 compliant code * * @return text HTML content describing embedded ...


4

Adapted from this answer Add this code to your functions.php file: add_action( 'after_setup_theme', 'wpse_74735_replace_wp_caption_shortcode' ); /** * Replace the default caption shortcode handler. * * @return void */ function wpse_74735_replace_wp_caption_shortcode() { remove_shortcode( 'caption', 'img_caption_shortcode' ); remove_shortcode( '...


4

You can hook into the filter img_caption_shortcode and replace the whole captioned image. Here I've copied the caption shortcode function from WP4.5, left the version used if your theme declares HTML5 support as it is (using figcaption) and modified the non-HTML5 version to use h2. function wpse_233354_img_caption_shortcode( $empty, $attr, $content = null )...


3

The latest versions of WP have really improved the filterability of caption arguments, so I think this new answer will have the smallest footprint and safest operation. What we need to do is directly filter $atts['caption'] during shortcode_atts() for the [caption] shortcode. We can do this with the shortcode_atts_caption filter which only affects the ...


3

There is a hook inside the caption shortcode that will allow you to hijack the whole thing. Most of the following is copied from the Core img_caption_shortcode function. function nested_img_caption_shortcode($nada, $attr, $content = null) { extract( shortcode_atts( array( 'id' => '', 'align' => 'alignnone', 'width' =&...


3

Here's a much simpler and cleaner solution: function my_img_caption_shortcode_width($width, $atts, $content) { return 0; } add_filter('img_caption_shortcode_width', 'my_img_caption_shortcode_width', 10, 3);


3

That caption shortcode is built into WordPress. It should generate the same markup for any theme, unless the theme or a plugin has unregistered it and registered a replacement. I don't know if that is the case with your theme or with TwentyEleven. Look for 'img_caption_shortcode' in your theme files, probably functions.php. If your theme is unregistering it, ...


3

There's really no documentation for it yet, but you'll probably be able to do it hooking to the attachment_fields_to_save filter and inserting the default caption there. From the Codex: attachment_fields_to_save applied to fields associated with an attachment prior to saving them in the database. Called in the media_upload_form_handler function. ...


3

Removing wpautop: This is most likely from wpautop. To fix that, first write the following CODE in your theme's functions.php file: remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' ); If it doesn't solve the issue, that may mean <p></p> got into your database or you have empty newline inside your [caption][/...


3

It is not a good practice to override the WordPress native functionality. Instead you can hide the empty elements using CSS p:empty { display: none; } This will the empty elements.


3

You need to get the metadata of each image, add this to your functions.php file: function get_post_gallery_images_with_info($postvar = NULL) { if(!isset($postvar)){ global $post; $postvar = $post;//if the param wasnt sent } $post_content = $postvar->post_content; preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids)...


2

Since 3.7 there is a filter to change the width of the wrapping div directly. This will fix the extra 10px, and won't let the caption extend past the width of the image (unlike width: auto !important). function wp456_img_caption_width( $width, $atts, $content){ //by default 10 is added if image caption return $width - 10; } add_filter( '...


2

Find the line /* Remove [caption] in-line styling in function.php. Comment out: /* add_shortcode('wp_caption', 'fixed_img_caption_shortcode'); add_shortcode('caption', 'fixed_img_caption_shortcode'); function fixed_img_caption_shortcode($attr, $content = null) { // Allow plugins/themes to override the default caption template. $output = ...


2

Image captions in Wordpress are actually shortcodes. Shortcodes are applied by the filter: $content = apply_filters('the_content', $content); For example, Wordpress creates the following code in your content when you enter an image caption: [caption id="attachment_55" align="alignleft" width="127" caption="Here is my caption"][/caption] You need to still ...


2

Have you tried this for NextGen? - http://wordpress.org/support/topic/nextgen-image-gallery-captions Excerpt (many other useful tips at the linked thread): ENABLING CAPTIONS BELOW GALLERY IMAGES When you add a gallery to a post/page, you get this by default: [nggallery id=1] You need to add this: [nggallery id=1 template=caption] ...


2

Using latest function has_shortcode() introduced on v3.6 add_filter( 'the_content', 'process_wp_caption_shortcodes' ); // hook it late function process_wp_caption_shortcodes( $content ){ if( !function_exists ( 'has_shortcode' ) ) // no luck for user using older versions :) return $content; if( has_shortcode( ...


2

That markup is created by the caption shortcode. If you look at the the source for that shortcode you will see this: // Allow plugins/themes to override the default caption template. $output = apply_filters('img_caption_shortcode', '', $attr, $content); if ( $output != '' ) return $output; That means that you can hook a function into ...


2

SOLVED I'm using roots theme. So I open cleanup.php file and comment/remove this line: $attributes .= ' style="width: ' . (esc_attr($attr['width']) + 10) . 'px"';


2

In the filter, you will need to find the post parent of $post, get the value of the custom field of parent post and then add that value to $post['post_excerpt'] (where the caption is stored): add_filter('attachment_fields_to_save', 'wpse_insert_custom_caption', 10, 2); function insert_custom_default_caption($post, $attachment) { //Check if the $post is ...


2

WordPress stores image (attachment) data as follows: Description: post_content field Caption: post_excerpt field Alt: _wp_attachment_image_alt meta value And in code, that translates to: // Description echo $post->post_content; // Raw the_content(); // Caption (description as fallback) the_excerpt(); // Caption (explicitly) echo $post->...


2

The caption for an image is actually meta_data attached to the image and the get_post_gallery_images only returns a url, so in the array you won't have any other information. You could try something like: <?php $gallery = get_post_gallery_images( $post ); foreach( $gallery as $image_url ) : //get the id of the image post. $...


2

It will be interesting to see how the new evolving WP Gutenberg editor will handle shortcodes and embeds. If you use the [embed] shortcode for embedding, then a semi workaround, without UI support: [embed src="https://www.youtube.com/watch?v=jfNOdsvMke4" desc="some description"] where the custom desc attribute is supported with with this kind of wrapper: ...


2

You can directly get an attachment's meta data if you have it's ID. The alt data for an attachment is stored in _wp_attachment_image_alt. So, you can use: $alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); To get the caption of an image, you can use wp_get_attachment_metadata(). $metadata = wp_get_attachment_metadata( $id ); $...


2

The caption is stored in the image data. The only way to achieve what you require is to use multiple copies of the image and change the image data on each image. To better understand image data go ahead and download an image to your desktop then right click it, go to properties then the details tab. this is where the image meta data is set. WordPress ...


2

It turned out that the method called strip_tags(preg_replace(" (\[.*?\])", '', $output)) before strip_shortcodes($output), which caused aforementioned issue, since the code removed shortcode in square brackets, but retained contained image captions. I could fix it by swapping the two method calls like this: $output = get_the_content(); $output = ...


1

It looks like your WP Jquery Lightbox plugin is generating these extra captions. Check out the source of the jquery.lightbox.js file: ... cut ... var s = ''; if (title != '') { s = '<span id="titleText">' + title + '</span>'; } if (caption != '') { if (title != ''){ ...


1

Use the shortcode handler img_caption_shortcode to render the HTML for you, though you do need to pass it a width (which we can easily get with wp_get_attachement_image_src: function wpse_138126_thumbnail_caption( $html, $post_id, $post_thumbnail_id, $size, $attr ) { if ( $post = get_post( $post_thumbnail_id ) ) { if ( $size = ...


Only top voted, non community-wiki answers of a minimum length are eligible