• Resolved helppawz1

    (@helppawz1)


    I have a multiple image upload field in one of my forms, which creates a custom post when submitted. I would like the images to be stored in my acf gallery field. Please could you advise how I can get this to work. It needs to be stored in this kind of format – a:6:{i:0;s:5:”14007″;i:1;s:5:”14004″;i:2;s:5:”14003″;i:3;s:5:”14001″;i:4;s:5:”14000″;i:5;s:5:”13999″;}

Viewing 2 replies - 16 through 17 (of 17 total)
  • Thread Starter helppawz1

    (@helppawz1)

    I have managed to solve the problem by using pathinfo to retrieve the filename and using this in get_page_by_title instead of attachment_url_to_postid. This can now deal with any suffix being added to the file by wordpress. Thanks for all your help.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @helppawz1

    You are right, I completely missed the “-rotated” part, probably because it’s not as common as -scaled.

    But the “attachmant_url_to_postid” returns 0 if it cannot find attachment so I think we can benefit from that and just search for “-scaled” and “-rotated” versions of images.

    Try replacing this part of the code

    foreach ($fields_data as $key=>$single_field) {
    			
    			if ($single_field['key'] === $acfgal) {
    				
    				$images = explode( ',', $single_field['value'] );
    				
    				foreach ($images as $a=>$b) {
    					$theimages[$a]=attachment_url_to_postid( trim($b) );
    				}
    				
    				
    			
    				update_post_meta( $post_id, $acfgal,  $theimages );
    				
    			}
    			
    		}

    with this:

    foreach ($fields_data as $key=>$single_field) {
    			
    			if ($single_field['key'] === $acfgal) {
    				
    				$images = explode( ',', $single_field['value'] );
    				
    				foreach ($images as $a=>$b) {
    					
    					
    					$single_image = trim($b);
    					$single_image_ext = '.' . preg_match('/\./', $single_image) ? preg_replace('/^.*\./', '', $single_image) : '';
    					
    										
    					$theimages[$a]=attachment_url_to_postid( $single_image );
    					
    					if ( $theimages[$a] == 0 ) {
    						
    						$single_image_scaled = str_ireplace( $single_image_ext, '-scaled'.$single_image_ext, $single_image );			
    						$theimages[$a] = attachment_url_to_postid( $single_image_scaled );	
    						
    					}
    					
    					if ( $theimages[$a] == 0 ) {
    						
    						$single_image_rotated = str_ireplace( $single_image_ext, '-rotated'.$single_image_ext, $single_image );	
    						$theimages[$a] = attachment_url_to_postid( $single_image_rotated );
    						
    					}
    				}
    				
    				
    			
    				update_post_meta( $post_id, $acfgal,  $theimages );
    				
    			}
    			
    		}

    Best regards,
    Adam

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Image upload to acf gallery’ is closed to new replies.