php 重力形成多文件上传器字段到wordpress媒体附件并添加到CPT的元

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力形成多文件上传器字段到wordpress媒体附件并添加到CPT的元相关的知识,希望对你有一定的参考价值。

add_action('gform_after_submission_2', 'set_post_content', 10, 2);

function set_post_content($entry, $form) {
 $logo_retailer = array();
    $logo = $entry[44];
    $logo = stripslashes($logo);
    $logo = json_decode($logo, true);
    $cover_image = $entry[45];
    $cover_image = stripslashes($cover_image);
    $cover_image = json_decode($cover_image, true);
     foreach ($logo as $key => $val) {
        $logo_retailer_attach_id = gform_image_to_wp_attach_id($val, $new_post_id);
        update_post_meta($new_post_id, 'logo', $logo_attach_id);
    }
    foreach ($cover_image as $key => $val) {
        $cover_image_retailer_attach_id = gform_image_to_wp_attach_id($val, $new_post_id);
        update_post_meta($new_post_id, 'cover_image', $cover_image_attach_id);
    }
}

function gform_image_to_wp_attach_id($entry_img, $new_post_id) {
    $upload_dir = wp_upload_dir();
    /* $arrContextOptions = array(
      "ssl" => array(
      "verify_peer" => false,
      "verify_peer_name" => false,
      ),
      );
      $image_data = file_get_contents($entry_img, false, stream_context_create($arrContextOptions)); */ //$image_url = $entry_img
    $image_data = file_get_contents($entry_img);
    $filename = basename($entry_img);             //$image_url = $entry_img
    if (wp_mkdir_p($upload_dir['path']))
        $file = $upload_dir['path'] . '/' . $filename;
    else
        $file = $upload_dir['basedir'] . '/' . $filename;
    file_put_contents($file, $image_data);
    $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'
    );
    $attach_id = wp_insert_attachment($attachment, $file, $new_post_id); //$post_id = $new_post_id
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);
    wp_update_attachment_metadata($attach_id, $attach_data);
    return $attach_id;
//    set_post_thumbnail($new_post_id, $attach_id); // Sets a post thumbnail.  Post ID where image should be attached
}

以上是关于php 重力形成多文件上传器字段到wordpress媒体附件并添加到CPT的元的主要内容,如果未能解决你的问题,请参考以下文章

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段

php 连接重力表单多文件上载和高级自定义字段重复文件字段