Ask your WordPress questions! Pay money and get answers fast! (more info)

display image captions, descriptions and additional media fields WordPress

  • SOLVED

Hi - Im trying to find a way to display captions, descriptions and an additional media field called "file_number" of images inserted to pages.

I don't want to use short codes for all images as there are already 500+ placed images in pages that need this data displayed.

for example - below each of these images inserted onto the page id like to pull the caption, description, title and "file_number" to appear below the image:

http://www.bobgruen.com/beta/the-clash/

thanks for your help

GG

Answers (5)

2015-09-03

Bob answers:

I think it is possible.

1. You need to find each images added in post. you have to use [[LINK href="https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content"]]the_content[[/LINK]] filter
preg_match( '/src="([^"]*)"/i', $content, $inlineImages ) ;
or
preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $matches, PREG_PATTERN_ORDER);

2. If you have image tag you can find url from it. Then using image url you can find attachment id.
[[LINK href="https://pippinsplugins.com/retrieve-attachment-id-from-image-url/"]]https://pippinsplugins.com/retrieve-attachment-id-from-image-url/[[/LINK]]

3. using attachment id you can find related data. Like jayram did in above post.

4. then you have to replace old image html to with new html having image tag and other information.


Bob comments:

Can you try to put this code in your theme's functions.php file


// find attachment id
function pn_get_attachment_id_from_url( $attachment_url = '' ) {

global $wpdb;
$attachment_id = false;

// If there is no url, return.
if ( '' == $attachment_url )
return;

// Get the upload directory paths
$upload_dir_paths = wp_upload_dir();

// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

}

return $attachment_id;
}

add_filter('the_content', 'add_image_extra_data');
function add_image_extra_data($content){
global $post;
if($post->post_type == "post"){
//print_r($content);
//preg_match( '/src="([^"]*)"/i', $content, $inlineImages ) ;
preg_match_all("/<img[^']*?src=\"([^']*?)\"[^']*?>/", $post->post_content, $inlineImages, PREG_PATTERN_ORDER);
if($inlineImages){
$i = 0;
foreach($inlineImages[1] as $image){
$attachment_id = pn_get_attachment_id_from_url($image);
$content = replace_image_tag($attachment_id, $content,$inlineImages[0][$i]);
$i++;
}
}
}
return $content;
}
//replace simple img tag with image+content
function replace_image_tag($attachment_id,$content,$img_tag){
$attachment = get_post( $attachment_id );
$meta = 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' => $attachment->guid,
'title' => $attachment->post_title
);
$new_img = $img_tag."<div class='caption'>".$meta['caption']."</div>";
$new_img .= "<div class='desc'>".$meta['description']."</div>";
return str_replace($img_tag, $new_img,$content);
}


dataharvest comments:

I have one more additional piece of Image meta data that i’d like to have appear below the captions and descriptions - I’ve made a custom meta called “file-number” for the images in the media library - wonder if you could tell me how i might modify your code to bring that bit of information to the page.

thank you again for your help, very much.

2015-09-02

Jayaram Y answers:

Hi,

The simplest way is to have a function which calls all the details and use them where ever you want to display. You can use the below function in functions.php file

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' => $attachment->guid,
'title' => $attachment->post_title
);
}


Once done, call it as


$attachment_meta = wp_get_attachment(your_attachment_id);


And then either loop through the array values or simply reference by the key name of what you want (ie: caption, description, etc.):


echo $attachment_meta['caption'];


[[LINK href="https://wordpress.org/ideas/topic/functions-to-get-an-attachments-caption-title-alt-description#post-22155"]]source[[/LINK]]

2015-09-03

Arnav Joy answers:

How you are calling image here ?

http://www.bobgruen.com/beta/the-clash/


dataharvest comments:

the images are placed into the post via HTML - inserted from the wordpress media tool. I wasn't able to get your solution working - Ive been thru that wordpress.org solution you referenced without success.

2015-09-03

Andrea P answers:

hello!

this function will add title and caption below all the images posted within a post/page content (if caption and title are provided).


add_filter( 'image_send_to_editor', 'ap_add_caption_to_img', 10 );
function ap_add_caption_to_img( $html, $id, $caption, $title ) {

// create the html to add after the image
$additional_html = '';
if ( $title ){
$additional_html .= '<h4 class="img-title">'.$title.'</h4>';
}
if ( $caption ){
$additional_html .= '<p class="wp-caption-text">'.$caption.'</p>';
}
// get the custom field value and add it if any
$file_number = get_post_meta($id, 'file_number', true);
if ( $file_number ){
$additional_html .= '<p class="img-file-number">'.$file_number.'</p>';
}


// add it to the html and return it
$html .= $additional_html;

return $html;

}


then of course we can play a bit with the html output, and eventually wrap image and caption into another div (as it seems you are doing with the first image in your sample page).

also, I see you have caption and description, but looking at the wp documentation, an image should have title and caption.. are you adding those by using a plugin? or maybe it's just a different way to name them and you mean title when you say caption, and caption when you say description..

I have not tested it, so let me know how it goes


dataharvest comments:

No response on the post when i dropped your code into the functions.php

natively you have Url, title, caption, alt and description in the media library. to that I've added "file_number"

thanks!

GG