首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想上传图像从前端与高级自定义字段

我想上传图像从前端与高级自定义字段
EN

Stack Overflow用户
提问于 2021-03-04 17:13:07
回答 1查看 529关注 0票数 1

我有很多高级的自定义字段。它们之间有三种图像类型。我已经用add_post_meta添加了ACF文本字段。但我不能添加图片。以下是我的代码示例

代码语言:javascript
复制
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['new_post'] )) {
 
 $title="New Property for Sale";
    
  $custom_tags = $_POST['tax_input']; //load thread tags (custom tax) into array  
  $post = array( 
    'post_title'=>$title,
      'post_content'    => $_POST['property_description'],
      'tax_input'   => $custom_tags,
      'post_status' => 'pending',
      'post_type'   => 'property'
    );

    $post_id = wp_insert_post($post);
    
    //send our post, save the resulting ID
if($post_id){
add_post_meta($post_id, 'type', $_POST['acf']['field_60338ebdd3ca4'], true);
add_post_meta($post_id, 'locality', $_POST['acf']['field_603374e8f8d62'],true);
add_post_meta($post_id, 'address', $_POST['acf']['field_6034ed6a0cd29'], true);
add_post_meta($post_id, 'facing', $_POST['acf']['field_6034eda30cd2b'],true);
add_post_meta($post_id, 'bed_number', $_POST['acf']['field_60337452f8d5f'], true);
add_post_meta($post_id, 'balconies', $_POST['acf']['field_6034f2180cd2c'], true);
   //send the user along to their newly created post
  }
}

我已经添加了这篇文章的特色图片。

代码语言:javascript
复制
  if ( $_FILES['image']['name']!="" ) { 
$upload = wp_upload_bits($_FILES["image"]["name"], null, file_get_contents($_FILES["image"]["tmp_name"])); 
   // $post_id = $post_id; //set post id to which you need to set featured image
    $filename = $upload['file'];
    $wp_filetype = wp_check_filetype($filename, null);
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id ); 
    if ( ! is_wp_error( $attachment_id ) ) {
        require_once(ABSPATH . 'wp-admin/includes/image.php'); 
        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
        wp_update_attachment_metadata( $attachment_id, $attachment_data );
        set_post_thumbnail( $post_id, $attachment_id );
    }
}

我必须使用ACF添加更多的两个图像。请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2021-11-23 21:46:44

这是上传到Media的WP codex上最直接的完整代码:

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

因此,尝试在wordpress页面模板中创建此文件,在附加之后,您必须在ACF元数据上设置ID

代码语言:javascript
复制
<?php 
if ( 
isset( $_POST['my_image_upload_nonce'], $_POST['post_id'] ) 
&& wp_verify_nonce( $_POST['my_image_upload_nonce'], 'my_image_upload' )
&& current_user_can( 'edit_post', $_POST['post_id'] )
) {
// The nonce was valid and the user has the capabilities, it is safe to continue.

  // These files need to be included as dependencies when on the front end.
  require_once( ABSPATH . 'wp-admin/includes/image.php' );
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
  require_once( ABSPATH . 'wp-admin/includes/media.php' );
 
  // Let WordPress handle the upload.
  // Remember, 'my_image_upload' is the name of our file input in our form above.
  $attachment_id = media_handle_upload( 'my_image_upload', $_POST['post_id'] );
 
  if ( is_wp_error( $attachment_id ) ) {
    // There was an error uploading the image.
    echo "ERROR";
  } else {
    // The image was uploaded successfully!
    echo "Image ID is: " . $attachment_id;
    //Now set your image ACF field to the $attachment_id 
    // My case because its a user metadata update: 
    //update_user_meta( $userID, 'YOUR_IMAGE_NAME_ACF_FIELD_HERE!!', $attachment_id );
    // update post meta of your ACF field with the new attachment id
    update_post_meta ( $_POST['post_id'], 'YOUR_IMAGE_NAME_ACF_FIELD_HERE!!' , $attachment_id );
  }
} else {
// The security check failed, maybe show the user an error.
}

?>

<!-- HTML code for uploading this single file -->

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
<input type="file" name="my_image_upload" id="my_image_upload"  multiple="false" />
<input type="hidden" name="post_id" id="post_id" value="55" />
<?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
<input id="submit_my_image_upload" name="submit_my_image_upload" type="submit" value="Upload" />
</form>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66471906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档