Support

Account

Home Forums General Issues Get Image Src (URL) from Text Field and Set Featured Image Reply To: Get Image Src (URL) from Text Field and Set Featured Image

  • I’ve done something similar with Gravity forms, whereby someone would submit content and I needed to use the added image field to set the featured image on the new post.

    The code used this:

    
    $upload_path = rgar($entry, '11');
    			if (!empty($upload_path)){
    				$attachmentid	= wp_create_image_id($upload_path, $member_post_id);
    				$setfeatimg		= set_post_thumbnail($member_post_id, $attachmentid);
    			}	
    

    Obviously, you would need to alter from a gravity form field to your.
    $member_post_id was the post ID I needed to add the image to

    
    function wp_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 prg_create_image_id
    

    If you need to get the image path, please read this forum post