【问题标题】:Can't add the URL of saved attachment from contact form 7 to flamingo inbound messages无法将联系人表单 7 中已保存附件的 URL 添加到 flamingo 入站消息
【发布时间】:2018-08-30 22:32:06
【问题描述】:

我正在使用 flamingo 插件保存联系表单提交。我还使用以下代码将表单上传的文件保存为 WordPress 附件:

//Save CF7 data
function cf7_create_post($WPCF7_ContactForm) {
    //In case you wanna check for a especific form
    /* $form_id = $data->id;
      if (224 == $WPCF7_ContactForm->id()) { */

    //Get current form
    $wpcf7      = WPCF7_ContactForm::get_current();
    // get current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $formData       = $submission->get_posted_data(); // Get all data from the posted form
        $uploaded_files = $submission->uploaded_files(); // this allows you access to the upload file in the temp location
    }

    // We need to get the CF7 field name from FILE
    $cf7_file_field_name = 'file-cv'; // [file uploadyourfile]

    //Do the magic the same as the refer link above
    $image_name     = $formData[$cf7_file_field_name];
    $image_location = $uploaded_files[$cf7_file_field_name];
    $image_content  = file_get_contents($image_location);
    $wud            = wp_upload_dir();
    $upload         = wp_upload_bits($image_name, null, $image_content);
    $chemin_final   = $upload['url'];
    $filename       = $upload['file'];
    if ($filename > '') {
        require_once(ABSPATH . 'wp-admin/includes/admin.php');
        $wp_filetype = wp_check_filetype(basename($filename), null);
        $attachment  = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $filename); // $newpostid optional
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
        wp_update_attachment_metadata($attach_id, $attach_data);
        $attach_url = get_attached_file( $attach_id );
    }
}
add_action('wpcf7_before_send_mail', 'cf7_create_post', 10);

我想将附件的 URL 保存在 flamingo 中(将其添加到已发布的数据中),当我使用 ‘wpcf7_before_send_mail’ 过滤器时,‘wpcf7_posted_data’ 过滤器已被触发,无法再修改已发布的数据。

我尝试使用‘wpcf7_posted_data’ 过滤器本身将文件保存为附件(我可以在其中添加带有 URL 的字段,它将显示在 flamingo 帖子上),但是当‘wpcf7_posted_data’ 过滤器被触发时,上传的文件不是在发布的数据或uploaded_files() 方法中可用。

我想我以后可以通过修改 flamingo 帖子来实现购买,但要确定如何获取它的信息以及它是否是一个好的解决方案。 谢谢

【问题讨论】:

  • 你好拉米,你找到解决办法了吗?
  • 嗨@LucasBarros,很遗憾没有。我最终使用了我自己的自定义帖子类型。

标签: wordpress contact-form-7 wordpress-flamingo-plugin


【解决方案1】:

我发现完成任务的唯一方法是将文件路径/文件名添加到全局变量中,并使用 flamingo 插件的动作钩子之一插入文件路径。

在 modules/flamingo.php 下的联系表格 7 中有一个钩子“wpcf7_after_flamingo”:

do_action( 'wpcf7_after_flamingo', $result );

我们可以使用它来更新字段。

'wpcf7_before_send_mail' 在 flamingo-hook 之前触发,将文件路径保存在全局变量中。

function my_before_cf7_send_mail(\WPCF7_ContactForm $contactForm){
    $submission = WPCF7_Submission::get_instance();
    //$contactForm;
    if($submission && ($contactForm->id() == 'FORM_ID')) {
        // get the name of the user
       
        $uploaded_files = $submission->uploaded_files();

        if ($uploaded_files) {
            // get the temp path of the file 
            $tempFilepath = $uploaded_files["FIELDNAME"];
            
            // Get the path to the upload directory.
            $wp_upload_dir = WP_CONTENT_DIR;

            //get original filename 
            $origFilename = sanitize_title(basename($tempFilepath));
            $newDir = $wp_upload_dir . '/uploads/customfolder/';
            $newFilepath = $newDir . $originFilename;


            copy($tempFilepath, $newFilepath);
            global $FieldnameFilePath;
            $FieldnameFilePath= $newFilepath;
        }
    }
}
add_action('wpcf7_before_send_mail', 'my_before_cf7_send_mail');

function my_add_filepath_to_flamingo($result){
    // get our global var
    global $FieldnameFilePath;

    $filepath = $FieldnameFilePath;
    
    // get the id from $result
    $flamingoEntryID = $result['flamingo_inbound_id'];

    if($flamingoEntryID){
        update_post_meta( $flamingoEntryID, '_field_FIELDNAME', $filepath );
        
        // also update the serialized post_meta with the same ID
        // i did not need that one!
    }

}
add_action('wpcf7_after_flamingo', 'my_add_filepath_to_flamingo');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2011-05-26
    • 2023-03-18
    • 2013-03-15
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多