Auto Add Image Title,Caption,Alt Text,Description while uploading Images in WordPress

Can anyone tell me how to auto fill/add same image title in title, caption, alt text and description while uploading any image in my WordPress Posts.

desired screenshot

Related posts

3 comments

  1. added_post_meta seems like a good time to hook into a new image. Not only is the default meta already set but the function gives you the $post_id along with $meta_value which holds the attachment metadata. From there you can get all the fields and set the ones you want.

    add_action('added_post_meta', 'wpse_20151219_after_post_meta', 10, 4);
    
    function wpse_20151219_after_post_meta($meta_id, $post_id, $meta_key, $meta_value) {
    
        // _wp_attachment_metadata added
        if($meta_key === '_wp_attachment_metadata') {
    
            // ----------------------------------------------------------------------
            // POST
            // ----------------------------------------------------------------------
    
            // Change basic fields on attachment post
            wp_update_post(array(
                               'ID'           => $post_id,
                               'post_title'   => "This is a TITLE for $post_id",
                               'post_content' => "This is the DESCRIPTION for $post_id",
                               'post_excerpt' => "This is the CAPTION for $post_id",
                           ));
    
            // ----------------------------------------------------------------------
            // POST META
            // ----------------------------------------------------------------------
    
            // Change ALT Text
            update_post_meta($post_id, '_wp_attachment_image_alt', "This is the ALT Text for $post_id");
    
            // Add Custom Field
            update_post_meta($post_id, '_wpse_20121219_my_custom_meta', 'MyCustomMetaValue');
    
            // ----------------------------------------------------------------------
            // POST META ( ATTACHMENT METADATA )
            // ----------------------------------------------------------------------
    
            // Change Image Metadata
            $meta_value[ 'image_meta' ] = array_merge($meta_value[ 'image_meta' ], array(
                'credit'    => 'https://black-buddha.com',
                'camera'    => 'The Best Camera EVER!',
                'copyright' => date('Y'),
                'title'     => "This is a META TITLE for $post_id",
                'caption'   => "This is a META CAPTION for $post_id",
            ));
    
            // Update The Image Metadata
            wp_update_attachment_metadata($post_id, $meta_value);
    
            // _wp_attached_file
            // _wp_attachment_metadata (serialized)
            // _wp_attachment_image_alt
            // _wpse_20121219_my_custom_meta
    
            $attachment_meta = get_post_meta($post_id);
    
            // width
            // height
            // file
            // sizes
            // image_meta
            //      aperture
            //      credit
            //      camera
            //      caption
            //      created_timestamp
            //      copyright
            //      focal_length
            //      iso
            //      shutter_speed
            //      title
            //      orientation
            //      title
            //      keywords
    
            $attachment_metadata = wp_get_attachment_metadata($post_id);
        }
    }
    
  2. You can hook into the ‘add_attachment’ action from wp-includes/post.php Line:3332. (Version 4.4)
    It passes in the post_id and from there you can get the file name and then update the post meta with anything you need to.

    Reference taken from

    add_action( 'add_attachment', 'wpse_125805_add_image_meta_data' );
    
    function wpse_125805_add_image_meta_data( $attachment_ID ) {
    
        $filename   =   $_REQUEST['name']; // or get_post by ID
        $withoutExt =   preg_replace('/\.[^.\s]{3,4}$/', '', $filename);
        $withoutExt =   str_replace(array('-','_'), ' ', $withoutExt);
    
        $my_post = array(
            'ID'           => $attachment_ID,
            'post_excerpt' => $withoutExt,  // caption
            'post_content' => $withoutExt,  // description
        );
        wp_update_post( $my_post );
    
        // update alt text for post
        update_post_meta($attachment_ID, '_wp_attachment_image_alt', $withoutExt );
    }
    
  3. For a simpler solution, you can use this WordPress plugin that I made a while back.

    The plugin comes with a bulk updater too that will update the image attributes for images already in your media library if you want to do that as well.

Comments are closed.