Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Created July 28, 2017 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leepettijohn/fd0263587a7e3aec27afbb3f714a5256 to your computer and use it in GitHub Desktop.
Save leepettijohn/fd0263587a7e3aec27afbb3f714a5256 to your computer and use it in GitHub Desktop.
Gravity Forms - File / Image Upload to Featured Image
<?php
form_id = 1;
add_action("gform_post_submission_".$form_id, "add_new_post_featured_image");
function add_new_post_featured_image($entry){
$title_id = 1;
$content_id = 2;
$file_id = 3;
$new_post = array(
'post_title' => $entry[$title_id],
'post_content' => $entry[$content_id]
);
$new_post_id = wp_insert_post($new_post, TRUE);
if (!empty($new_post_id)){
$upload_path = rgar($entry,$file_id);
if (!empty($upload_path)){
$attachmentid = jdn_create_image_id($upload_path,$new_post_id);
$setfeatimg = set_post_thumbnail($new_post_id,$attachmentid);
}
}
}
/**
* Create the image attachment and return the new media upload id.
* @author Joshua David Nelson, josh@joshuadnelson.com
* @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
* @link https://joshuadnelson.com/programmatically-add-images-to-media-library/
* @param string $image_url The url to the image you're adding to the Media library.
* @param int $parent_post_id Optional. Use to attach the media file to a specific post.
*/
function jdn_create_image_id( $image_url, $parent_post_id = null ) {
// Bail if the image url isn't valid
if( empty( $image_url ) || ! esc_url( $image_url ) )
return false;
// Escape the url, just to be save
$image_url = esc_url( $image_url );
// Cache info on the wp uploads dir
$wp_upload_dir = wp_upload_dir();
// get the file path
$path = parse_url( $image_url, PHP_URL_PATH );
// File base name, e.g. image.jpg
$file_base_name = basename( $image_url );
// Full path, set up to work with a WP in a subdirectory or default location
if( site_url() != home_url() ) {
$home_path = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
} else {
$home_path = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
}
// Remove the trailing slash on the home path
$home_path = untrailingslashit( $home_path );
// Combine the two to get the uploaded file path
$uploaded_file_path = $home_path . $path;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( $file_base_name, null );
// error check
if( !empty( $filetype ) && is_array( $filetype ) ) {
// Create attachment title - basically, pull out the text
$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ),
'post_mime_type' => $filetype['type'],
'post_title' => esc_attr( $post_title ),
'post_content' => '',
'post_status' => 'inherit'
);
// Set the post parent id if there is one
if( ! is_null( $parent_post_id ) && absint( $parent_post_id ) )
$attachment['post_parent'] = absint( $parent_post_id );
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path );
//Error check
if( !is_wp_error( $attach_id ) ) {
//Generate wp attachment meta data
if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
} // end if file exists check
} // end if error check
return $attach_id;
} else {
return false;
} // end if $filetype
} // end function jdn_create_image_id
?>
@wpfangirl
Copy link

Don't you need a $ before the initial form_id?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment