Last active
March 17, 2020 13:43
-
-
Save New0/d548764bc719dcf4e9fbbe313281442a to your computer and use it in GitHub Desktop.
Set a featured image via an advanced file field 2.0 and Save as post type processor in Caldera Forms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: CF featured image | |
* Author: New0 | |
* Description: Use an advanced File Field 2.0 to save a file as a featured image in a post created via the Save as Post Type processor. | |
* | |
*/ | |
//Get File and Post ID before setting the featured image | |
add_action('caldera_forms_submit_complete', function($form, $referrer, $process_id, $entry_id){ | |
//SET CORRESPONDING FORM AND FIELD ID | |
$form_id = "CF5e32b6d71e30b"; | |
$field_id = "fld_1234802"; | |
if($form['ID'] !== $form_id){ | |
return; | |
} | |
//Get submission data | |
$submission = Caldera_Forms::get_entry( $entry_id, $form); | |
//Get File | |
if(is_array($submission['data'][$field_id ])){ | |
$file = $submission['data'][$field_id ]['value'][0]; | |
} | |
//Get Post ID | |
if(is_array($submission['meta']['post_type']['data'])){ | |
$fp = $submission['meta']['post_type']['data']; | |
if(is_array($fp)){ | |
$fp_data = reset($fp); | |
if(is_array($fp_data)){ | |
$post_id = $fp_data['entry']['ID']['meta_value']; | |
} | |
} | |
} | |
//Set featured image | |
if(isset($post_id) && isset($file)){ | |
$attachment_id = custom_featured_file( $file, $entry_id ); | |
$featured = set_post_thumbnail( $post_id, $attachment_id ); | |
} | |
}, 99, 4); | |
//Convert the file in attachment and get attachment ID | |
function custom_featured_file( $file, $entry_id ){ | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Check the type of file. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( basename( $file ), null ); | |
// Get the path to the upload directory. | |
$wp_upload_dir = wp_upload_dir(); | |
$filename = $wp_upload_dir['path'] . '/' . basename( $file ); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $file ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file ) ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $filename, $entry_id ); | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
return $attach_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment