联系表格7以获取特色图像

时间:2018-01-19 作者:Pipo

我正在尝试检索一个在前端上传的文件,使用联系人表单7,并将其分配给一个特色图片自定义帖子类型。以下是我目前的代码:

function form_to_post( $posted_data ) {

    $args = array(
        \'post_type\' => \'projects\',
        \'post_status\'=> \'draft\',
        \'post_title\'=> wp_strip_all_tags( $posted_data[\'title\'] ),
        \'post_content\'=> wp_strip_all_tags( $posted_data[\'pitch\'] ),
    );
    $post_id = wp_insert_post($args);

    if( ! is_wp_error( $post_id ) ) {

        if( isset($posted_data[\'featured\']) ){
            $featuredUpload = wp_upload_bits($posted_data[\'featured\'][\'name\'], null, file_get_contents($posted_data[\'featured\'][\'tmp_name\']));

            $filename = $featuredUpload[\'file\'];
            $wp_filetype = wp_check_filetype($filename, null );
            $attachment = array(
                \'post_mime_type\' => $wp_filetype[\'type\'],
                \'post_parent\' => $post_id,
                \'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 );
            }
        }
    }

    return $posted_data;
}
add_filter( \'wpcf7_posted_data\', \'form_to_post\' );
Theerror_log 将1显示为的值$posted_data[\'featured\'] 这意味着文件数据不存储在此变量中。我已经看过了Contact Form 7 doc 他们说,在邮件发送之前,文件会被移动到一个临时目录(wp-content/uploads/wpcf7\\u-uploads)。那么,有人知道如何获取文件数据吗?

谢谢

2 个回复
最合适的回答,由SO网友:Pipo 整理而成

感谢贾德·富兰克林的指导。我也失踪了$submission->uploaded_files();.

以下是寻找相同答案的人的工作代码:

function image_form_to_featured_image( $contact_form ) {

    $submission = WPCF7_Submission::get_instance();
    $posted_data = $submission->get_posted_data();

    // Creating a new post with contact form values
    $args = array(
        \'post_type\' => \'projects\',
        \'post_status\'=> \'draft\',
        \'post_title\'=> wp_strip_all_tags( $posted_data[\'title\'] ),
        \'post_content\'=> wp_strip_all_tags( $posted_data[\'pitch\'] ),
    );
    $post_id = wp_insert_post($args);

    // Retrieving and inserting uploaded image as featured image
    $uploadedFiles = $submission->uploaded_files();

    if( isset($posted_data[\'featured\']) ){
        $featuredUpload = wp_upload_bits($posted_data[\'featured\'], null, file_get_contents($uploadedFiles[\'featured\']));


        require_once(ABSPATH . \'wp-admin/includes/admin.php\');
        $filename = $featuredUpload[\'file\'];
        $attachment = array(
            \'post_mime_type\' => $featuredUpload[\'type\'],
            \'post_parent\' => $post_id,
            \'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 );
        }
    }
}
add_action( \'wpcf7_before_send_mail\', \'image_form_to_featured_image\' );

SO网友:Judd Franklin

您所挂接的筛选器似乎无权访问post数据。

如果您使用wpcf7_before_send_mail 操作,然后访问提交实例数据,就可以访问附件的文件名。

function form_to_post( $posted_data ) {
    $post_id = wp_insert_post($args);
    $submission = WPCF7_Submission::get_instance();
    $postData = $submission->get_posted_data();
    die(var_dump($postData["featured"]));

}
add_action(\'wpcf7_before_send_mail\', \'form_to_post\' );
这对我在当地很管用。这对你有用吗?

结束

相关推荐

联系表格7以获取特色图像 - 小码农CODE - 行之有效找到问题解决它

联系表格7以获取特色图像

时间:2018-01-19 作者:Pipo

我正在尝试检索一个在前端上传的文件,使用联系人表单7,并将其分配给一个特色图片自定义帖子类型。以下是我目前的代码:

function form_to_post( $posted_data ) {

    $args = array(
        \'post_type\' => \'projects\',
        \'post_status\'=> \'draft\',
        \'post_title\'=> wp_strip_all_tags( $posted_data[\'title\'] ),
        \'post_content\'=> wp_strip_all_tags( $posted_data[\'pitch\'] ),
    );
    $post_id = wp_insert_post($args);

    if( ! is_wp_error( $post_id ) ) {

        if( isset($posted_data[\'featured\']) ){
            $featuredUpload = wp_upload_bits($posted_data[\'featured\'][\'name\'], null, file_get_contents($posted_data[\'featured\'][\'tmp_name\']));

            $filename = $featuredUpload[\'file\'];
            $wp_filetype = wp_check_filetype($filename, null );
            $attachment = array(
                \'post_mime_type\' => $wp_filetype[\'type\'],
                \'post_parent\' => $post_id,
                \'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 );
            }
        }
    }

    return $posted_data;
}
add_filter( \'wpcf7_posted_data\', \'form_to_post\' );
Theerror_log 将1显示为的值$posted_data[\'featured\'] 这意味着文件数据不存储在此变量中。我已经看过了Contact Form 7 doc 他们说,在邮件发送之前,文件会被移动到一个临时目录(wp-content/uploads/wpcf7\\u-uploads)。那么,有人知道如何获取文件数据吗?

谢谢

2 个回复
最合适的回答,由SO网友:Pipo 整理而成

感谢贾德·富兰克林的指导。我也失踪了$submission->uploaded_files();.

以下是寻找相同答案的人的工作代码:

function image_form_to_featured_image( $contact_form ) {

    $submission = WPCF7_Submission::get_instance();
    $posted_data = $submission->get_posted_data();

    // Creating a new post with contact form values
    $args = array(
        \'post_type\' => \'projects\',
        \'post_status\'=> \'draft\',
        \'post_title\'=> wp_strip_all_tags( $posted_data[\'title\'] ),
        \'post_content\'=> wp_strip_all_tags( $posted_data[\'pitch\'] ),
    );
    $post_id = wp_insert_post($args);

    // Retrieving and inserting uploaded image as featured image
    $uploadedFiles = $submission->uploaded_files();

    if( isset($posted_data[\'featured\']) ){
        $featuredUpload = wp_upload_bits($posted_data[\'featured\'], null, file_get_contents($uploadedFiles[\'featured\']));


        require_once(ABSPATH . \'wp-admin/includes/admin.php\');
        $filename = $featuredUpload[\'file\'];
        $attachment = array(
            \'post_mime_type\' => $featuredUpload[\'type\'],
            \'post_parent\' => $post_id,
            \'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 );
        }
    }
}
add_action( \'wpcf7_before_send_mail\', \'image_form_to_featured_image\' );

SO网友:Judd Franklin

您所挂接的筛选器似乎无权访问post数据。

如果您使用wpcf7_before_send_mail 操作,然后访问提交实例数据,就可以访问附件的文件名。

function form_to_post( $posted_data ) {
    $post_id = wp_insert_post($args);
    $submission = WPCF7_Submission::get_instance();
    $postData = $submission->get_posted_data();
    die(var_dump($postData["featured"]));

}
add_action(\'wpcf7_before_send_mail\', \'form_to_post\' );
这对我在当地很管用。这对你有用吗?

相关推荐