Skip to content

Instantly share code, notes, and snippets.

@adamplabarge
Last active October 27, 2016 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamplabarge/e8ee6289980f23880e58 to your computer and use it in GitHub Desktop.
Save adamplabarge/e8ee6289980f23880e58 to your computer and use it in GitHub Desktop.
GF-ACF image attachment maker
<?php
//* the the default upload path to work in normal WP structure
add_filter("gform_upload_path", "change_upload_path", 20, 2);
function change_upload_path($path_info, $form_id){
$wp_upload_path = wp_upload_dir();
$path_info["path"] = $wp_upload_path['path'] . '/';
$path_info["url"] = $wp_upload_path['url'] . '/';
return $path_info;
}
/**
* Create the image attachment and return the new media upload id.
* @based on: http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
* @since 2.0.0
* now includes thumbnail processing and better path logic
*/
function jdn_create_image_id( $image_url, $parent_post_id = null ) {
if( !isset( $image_url ) )
return false;
global $_wp_additional_image_sizes;
// 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
$file_base_name = basename( $image_url );
$uploaded_file_path = ABSPATH . $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
$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 ) )
$attachment['post_parent'] = $parent_post_id;
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
if( in_array( $s, array( 'thumbnail', 'medium', 'large' ) ) ) {
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
if( isset( $_wp_additional_image_sizes[ $s ] ) ) {
$sizes[ $s ] = array(
'width' => $_wp_additional_image_sizes[ $s ]['width'],
'height' => $_wp_additional_image_sizes[ $s ]['height'],
'crop' => $_wp_additional_image_sizes[ $s ]['crop'],
);
}
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $uploaded_file_path, $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$attachment['sizes'][$size] = $resized;
}
// 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 get_image_id
/**
* Repeater field update for Add Team Member
* @author Adam LaBarge
* @param $entry array required
* @param $form array required input by user
*
* @return none
*/
add_action("gform_after_submission_11", "add_team_member_11", 10, 2);
function add_team_member_11($entry, $form)
{
$team_member_name = $entry['1'];
$team_member_title = $entry['4'];
$team_member_bio = $entry['2'];
$team_member_image = $entry['3'];
$team_member_email = $entry['5'];
$field_key = "field_54f61acd460bb"; // Form - Project Add Team Member Repeater field key
$post_id = $_SESSION['project_id'];
$value = get_field( $field_key, $post_id ); // must return image as ID
$value = ( $value == '0' ? array() : $value ); // if value empty, set it as array
if( function_exists( 'jdn_create_image_id' ) ) :
$att_id = jdn_create_image_id( $entry[3], $post_id );
endif;
$value[] = array(
'name' => $team_member_name,
'bio' => $team_member_bio,
'image' => $att_id, // must be an ID
'email' => $team_member_email,
'title' => $team_member_title,
);
update_field( $field_key, $value, $post_id );
}
@rynoceris
Copy link

Thank you so much for this awesome code! The improved path settings are working great for me, but for some reason I am still not getting the additional sizes created. I'm not sure what I'm doing wrong.

@adamplabarge
Copy link
Author

Only guess would be to make sure to add the custom image sizes: https://developer.wordpress.org/reference/functions/add_image_size/

@dougkeeling
Copy link

Thank you for this improvement on Josh's code. Someone really should turn this into a plugin!

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