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

Function for adding description to all uploaded images WordPress

  • SOLVED

Hi,

I want to be able to add a function that adds in the description field a certain text.

So I was thinking something like this.

1. The function adds the text - uploaded at "site" community.
2. The functions doesn't overrides any added description. only add to it at the end "- uploaded at "site" community"
3. It adds to all images, even any those images uploaded long time ago.

so for example, we say that an user has already written a description:

My tomato looks so goods and it's going to be a wonderful experience to eat it up. - Uploaded at "site" community.

Answers (1)

2015-02-07

Dbranes answers:

You can try the following to modify the image caption:

/**
* Append text to the image caption during upload.
*
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/10593
*/

add_filter( 'wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2 );

function wpq_insert_attachment_data( $data, $postarr ) {

// String to append:
$append = ' Uploaded at "site" community.';

// Append to the image caption field:
if( false == stripos( $data['post_excerpt'], $append ) ) {
$data['post_excerpt'] .= $append;
}

// Append to the image description field:
if( false == stripos( $data['post_content'], $append ) ) {
$data['post_content'] .= $append;
}

return $data;
}


where you can modify the <em>$append</em> to your needs.

For older images you might need to try something like:

UPDATE wp_posts
SET post_excerpt = CONCAT( post_excerpt, ' Uploaded at "site" community.' )
WHERE
post_type = 'attachment'
AND LOCATE(' Uploaded at "site" community.', post_excerpt ) = 0
AND post_mime_type = 'image/jpeg'


in your PHPMyAdmin, after you have taken db backup.

Notice that the <em>post_excerpt</em> field is for the image caption and the <em>post_content</em> field is for the image description.


Dbranes comments:

Here's another approach:

add_filter( 'shortcode_atts_caption', 'wpq_shortcode_atts_caption' );

function wpq_shortcode_atts_caption( $attr ) {

$append = ' Uploaded at "site" community.';

if( false === stripos( $attr['caption'], $append ) ) {
$attr['caption'] .= $append;
}

return $attr;
}


where we just filter the caption directly.


Veritus comments:

It seems that this is the easy one.. does this one add to all images description?


Dbranes comments:

yes, it's the easy one, it will work for every

[caption]...[/caption]

instance in your post content.


Veritus comments:

So I understand what you mean correct. It's added like in the picture. https://www.dropbox.com/s/cisq730y40lqy33/Description.PNG?dl=0 So there isn't any other easier way to add "-upload at "site" community at the image description. (post content) without going through php my admin?


Dbranes comments:

It all depends on where and how you want to display the image <em>caption</em> and <em>description</em>.

Everytime you insert an image with a caption into your post editor, you will get a code that contains the [caption] shortcode.

The [caption] solution is the most flexible one, because if you want to change your append string, you don't have to adjust your database, since we are only modifying the [caption] output.

Where do you need to output the image description?


Veritus comments:

It don't need to output anything, it's only to fill in that info. It looks better than an empty image description and also it's good for seo practices. That is the plan. Correct me if I'm wrong. :)


Veritus comments:

The info will be displayed when a user looks at an image if it's added to the description but it's the caption it won't be visible.


Dbranes comments:

I guess you're using <em>the_content()</em> to display the image description on the attachment's page.

Then you can easily setup a <em>the_content</em> filter with <em>is_attachment()</em> check to append the string.

- Just trying to give you the most flexible solution ;-)

ps: Did the [caption] solution work for you?



Veritus comments:

I haven't test that out yet. But I will and see how that works out. :)


Veritus comments:

Nope, it doesn't add anything to the caption..


Dbranes comments:

This works on all the default themes.

Here are screenshots from the <em>Twenty Fifteen</em> theme test:

Insert an image with a caption into the post editor:

[[LINK href="http://i.imgur.com/vl61uVZ.jpg"]]http://i.imgur.com/vl61uVZ.jpg[[/LINK]]

and then view it on the front:

[[LINK href="http://i.imgur.com/pOV89AX.jpg"]]http://i.imgur.com/pOV89AX.jpg[[/LINK]]

as expected when I use the [caption] filtering code above.


Veritus comments:

aa, right. This only works with new uploaded images right?


Dbranes comments:

no, this works for all images with caption, old or new ;-)


Veritus comments:

Sorry for delaying with answers. Anyway, looks nice. I'll check this further tomorrow.