Support

Account

Home Forums ACF PRO Glitch when generate a gallery field via PHP

Solved

Glitch when generate a gallery field via PHP

  • Hi! I have a problem generating a gallery field via php.
    I use Dropzonejs to give the user the opportunity to upload a number of images and then upload each image via php with the following function, using an ajax call. The image is then attached to a post-type “place”.

    
    // get place ID from post/get
    				$place_ID = $_REQUEST['place_ID'];
    
    				// set target destination directory
    				$upload_overrides = array( 'test_form' => false );
    				$uploaded_file = wp_handle_upload( $_FILES['file'], $upload_overrides );
    // generate the attachment
    				$attachment = array(
    					'post_type'		 => 'attachment',
    					'guid'           => $uploaded_file['url'],
    					'post_mime_type' => $uploaded_file['type'],
    					'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),
    					'post_content'   => '',
    					'post_caption'   => '',
    					'post_status'    => 'inherit',
    					'post_author'    => $current_user->ID
    				);		
    				$attach_id = wp_insert_attachment( $attachment, $uploaded_file['file'], $place_ID );
    				$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file['file'] );
    				$result = wp_update_attachment_metadata( $attach_id,  $attach_data );

    So now i have generated new attachments and retrieve an array of attachments IDs. The image are correctly loaded in wordpress media gallery and also all the sizes are generated.

    Then i make a second ajax call that append the attachaents to a gallery ACF associated to the post. This is the code:

    $attachments_ids = array_map('intval', $data['ids_array']);
    	$attachments_ids = array_unique( $attachments_ids );
    	update_field( 'gallery', $attachments_ids , $data['place_ID'] );

    It seems to works because when i load, in the wordpress backend, the post edit page it is all correctly compiled and all the images are in the gallery ACF field.

    This is a intro to the question, now i will try to explain my issue.

    When i try to load the frontend post page it retgive me an error. If i try to have a var dump of the ‘gallery’ ACF field i can see that the structure of the array is not correct because it don’t contains all the sizes of each image, but only the IDS of the attachments.
    It is not like that: http://www.advancedcustomfields.com/resources/gallery/#template-usage

    but like:
    array(2) { [0]=> int(787) [1]=> int(786) }

    BUT! if i simply save the post from backend clicking on Publish/Update, the gallery field start to works correclty and the vardump is now correct.

    Any suggestion?

  • No one reads this forum?

  • When you use update field the way you are using it you need to use the field key and not the field name. The reason that you need to save it in the back end to get it to work on the front end is because the field key is not being updated properly.

    See “field_key vs field_name” here http://www.advancedcustomfields.com/resources/update_field/#field_key-vs%20field_name

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Glitch when generate a gallery field via PHP’ is closed to new replies.