Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adamwalter/ddbab3520c6337b52f32 to your computer and use it in GitHub Desktop.
Save adamwalter/ddbab3520c6337b52f32 to your computer and use it in GitHub Desktop.
GF-ACF image attachment maker
<?php
/**
* Change the default GF upload path
* https://www.gravityhelp.com/documentation/article/gform_upload_path/
*/
function agw_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;
}
add_filter("gform_upload_path", "change_upload_path", 20, 2);
/**
* Create the image attachment and return the new media upload id
*/
function agw_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
/**
* If upload field has value, do the magic
*/
$post_id = 'YOUR POST ID';
$gf_field_id = '1';
if( isset( $entry[$gf_field_id] ) ) {
$images = stripslashes( $entry[$gf_field_id] );
$images = json_decode( $images, true );
if( !empty( $images ) && is_array( $images ) ) {
$gallery = array();
foreach( $images as $key => $value ) {
if( function_exists( 'agw_create_image_id' ) )
$image_id = agw_create_image_id( $value, $post_id );
if( $image_id ) {
$gallery[] = $image_id;
}
}
}
}
// If we have images, update the ACF field
if( ! empty( $gallery ) ) {
update_field( 'FIELD_NAME_HERE', $gallery, $post_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment