最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to add "alt" attribute for image during upload at WP front-end?

programmeradmin1浏览0评论

At WP Front-end I create a upload form for users.

That form has three input fields (title, tags and upload).

So I want to add "Title - Tag - file_name" as alt attribute for images during the file upload.

How can I do with my code below?

if ($gui == 'upload') {
    if (!function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
    if ($_FILES) {
       foreach ($_FILES as $file => $array) {
           if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
              return "Upload Err: " . $_FILES[$file]['error'];
           }
           $attach_id = media_handle_upload( $file, $newPost );
       }                        
   }
   if ($attach_id > 0){
       //and if you want to set that image as Post  then use:
       update_post_meta($newPost,'Uploaded_Image_ID',$attach_id);
   }
}

At WP Front-end I create a upload form for users.

That form has three input fields (title, tags and upload).

So I want to add "Title - Tag - file_name" as alt attribute for images during the file upload.

How can I do with my code below?

if ($gui == 'upload') {
    if (!function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
    if ($_FILES) {
       foreach ($_FILES as $file => $array) {
           if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
              return "Upload Err: " . $_FILES[$file]['error'];
           }
           $attach_id = media_handle_upload( $file, $newPost );
       }                        
   }
   if ($attach_id > 0){
       //and if you want to set that image as Post  then use:
       update_post_meta($newPost,'Uploaded_Image_ID',$attach_id);
   }
}
Share Improve this question edited Jul 11, 2013 at 15:10 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 11, 2013 at 10:54 Zen NguyễnZen Nguyễn 474 silver badges11 bronze badges 2
  • 1 First, better use admin_url(). Second, how exactly is the alt-HTML tag displayed? Does it come from caption, or something else? HInt: There's no alt-Tag in the meta data. – kaiser Commented Jul 11, 2013 at 11:11
  • 3 An alt attribute must be a meaningful, functional replacement for an image. File names are the opposite. – fuxia Commented Jul 11, 2013 at 15:11
Add a comment  | 

1 Answer 1

Reset to default 1

The alt text is stored in the postmeta database table by WordPress, therefore we have to update the postmeta of the given attachment. The reference is done via the unique attachment ID, and the meta_key we are targeting is the _wp_attachment_image_alt.

So, to set the alt text, we can do the following:

update_post_meta($attach_id, '_wp_attachment_image_alt', 'Your alt text');

https://developer.wordpress.org/reference/functions/update_post_meta/

发布评论

评论列表(0)

  1. 暂无评论