3

I want to automatically add caption for each picture appears in the name of the image or the alternative text

example Caption add automatic

example

2
  • webtide.wordpress.com/2009/04/27/… but for alternate text repeat in caption
    – Daniela
    Apr 19 '13 at 21:08
  • Daniela, please explain and show the code you tried to adapt. You can do that updating the Question: edit. How this "automatic" works? Where does the caption text coming from? The screenshot you're showing indicates WordPress pre-3.5. After 3.5 everything changed and became more difficult.
    – brasofilo
    Apr 20 '13 at 3:42
0

You can add programmatically caption, alt, description every time you upload image at backend.

https://brutalbusiness.com/automatically-set-the-wordpress-image-title-alt-text-other-meta/

I tried myself this and worked nice as your expectation. This is for all your future upload images.

Try this and let me know.

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
--------------------------------------------------------------------------------------*/
add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );
function my_set_image_meta_upon_image_upload( $post_ID ) {

    // Check if uploaded file is an image, else do nothing

    if ( wp_attachment_is_image( $post_ID ) ) {

        $my_image_title = get_post( $post_ID )->post_title;

        // Sanitize the title:  remove hyphens, underscores & extra spaces:
        $my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',  $my_image_title );

        // Sanitize the title:  capitalize first letter of every word (other letters lower case):
        $my_image_title = ucwords( strtolower( $my_image_title ) );

        // Create an array with the image meta (Title, Caption, Description) to be updated
        // Note:  comment out the Excerpt/Caption or Content/Description lines if not needed
        $my_image_meta = array(
            'ID'        => $post_ID,            // Specify the image (ID) to be updated
            'post_title'    => $my_image_title,     // Set image Title to sanitized title
            'post_excerpt'  => $my_image_title,     // Set image Caption (Excerpt) to sanitized title
            'post_content'  => $my_image_title,     // Set image Description (Content) to sanitized title
        );

        // Set the image Alt-Text
        update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

        // Set the image meta (e.g. Title, Excerpt, Content)
        wp_update_post( $my_image_meta );

    } 
}
-2

To put in a plugin, these solutions came from eskimo and sam.. we simply select all the attachment where the post_excpert is empty and udpate it , put the title of it in description and caption when they are empty

    $s_query = "SELECT ID, post_title FROM `wp_posts` WHERE NOT post_content <=> post_title AND post_type='attachment'";
        $r_result = mysql_query($s_query);

        if($r_result)
        {
            while($row = mysql_fetch_row($r_result)){
                $s_update_query = 'UPDATE `wp_posts` SET post_content="'.$row[1].'", post_excerpt="'.$row[1].'" WHERE ID="'.$row[0].'"';
                mysql_query($s_update_query);
            }
        }

Here another similar query for the same result:

    mysql_select_db(DB_NAME)
or die("Unable to select database: " . mysql_error());
$query = "SELECT * FROM wp_posts WHERE post_type= 'attachment' AND post_excerpt= ''";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
$query = "UPDATE wp_posts SET post_excerpt=post_title WHERE post_type='attachment' AND post_excerpt=''";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());

So with these solution we can import(SELECT) many image in wp gallery.Firstly the caption doesn't appear, we need to update (UPDATE)the post to see it in front end.

3
  • 3
    Please explain what your code does and how it works. "these solution came from eskimo and sam.." doesn't count as an explanation. Just a point of note, if you add code to the editor, highlight it, and click on the {} symbol above the code to add it in a code block like in the edit I've done Jun 4 '14 at 13:44
  • 3
    If you're manipulating the WordPress database, you should probably be using the wpdb class.
    – Pat J
    Jun 4 '14 at 14:01
  • yes thank you for your reply i tryied using wpdb class but couldn't figure it out...not an expert, unfortunetly!!if someone can improve these queries, it could be great...
    – yuyazz
    Jun 4 '14 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.