• Resolved jejonesjnr

    (@jejonesjnr)


    I have a custom post type which has several image fields. When I create a form which allows attachments and link those fields to the ACF image fields, the images don’t embed. Instead, when I view the post, the image fields say “no image selected”. Is there something else I need to do?

Viewing 15 replies - 1 through 15 (of 43 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @jejonesjnr

    Hope you are doing fine!

    Based on previous notes from our developers it seems ?ACF only accepts the media ID, and the image fields need to be updated after the form is submitted.

    They have worked on a previous request for this issue and provided a workaround. To apply the workaround you’ll need to include a must-use plugin to your site’s wp-content/mu-plugins folder like this https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins, then add the following code to the plugin’s php file (You’ll need to change the image-field-1 field name to the one you are using in your form):

    <?php 
    
    add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_upload', 10, 4 );
    function wpmudev_update_post_acf_upload( $post_id, $field, $data, $cls ){
    	if( isset( $data['post-custom'] ) ){
    		foreach( $data['post-custom'] as $pkey => $pval ){
    			if( $pval['key']  == 'image-field-1' ){
    				if( $pval['value'] ) {
    					$attach_id = attachment_url_to_postid( $pval['value'] );
    					if( $attach_id ) {
    						update_field('image-field-1', $attach_id, $post_id);
    						$mime_type = wp_get_image_mime( $pval['value'] );
    						if( $mime_type ) {
    							$update_data = array(
    								'ID' => $attach_id,
    								'post_mime_type' => $mime_type,
    							);
    							wp_update_post( $update_data );
    						}
    					}
    				}
    			}
    		}
    	}
    }

    Also, please enable the ‘Show files in media library’ setting in the upload field like the image below:

    https://snipboard.io/djNXDG.jpg

    We recommend to test this on the dev/staging version first before putting it on the live?site.

    Hope this information helps

    Kind regards

    Luis

    Thread Starter jejonesjnr

    (@jejonesjnr)

    Again. Thank you for the quick response.
    What code should I use if I have multiple image fields?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jejonesjnr

    Please try this, slightly modified, version of the code:

    <?php 
    
    add_action( 'forminator_post_data_field_post_saved', 'wpmudev_update_post_acf_upload', 10, 4 );
    function wpmudev_update_post_acf_upload( $post_id, $field, $data, $cls ){\
    
    	// comma separated list of image fields
    	$image_fields = array( 'image-field-1', 'image-field-2' ); 
    
    	if( isset( $data['post-custom'] ) ){
    		foreach( $data['post-custom'] as $pkey => $pval ){
    			
    			if ( in_array( $pval['key'], $image_fields ) ) {
    			
    				if( $pval['value'] ) {
    					$attach_id = attachment_url_to_postid( $pval['value'] );
    					if( $attach_id ) {
    						update_field($pval['key'], $attach_id, $post_id);
    						$mime_type = wp_get_image_mime( $pval['value'] );
    						if( $mime_type ) {
    							$update_data = array(
    								'ID' => $attach_id,
    								'post_mime_type' => $mime_type,
    							);
    							wp_update_post( $update_data );
    						}
    					}
    				}
    			}
    		}
    	}

    You will need to list your image fields in this line (note: they must also be consistent with labels used in “custom fields” configured in your post-data field) :

    $image_fields = array( 'image-field-1', 'image-field-2' );

    Best regards,
    Adam

    Thread Starter jejonesjnr

    (@jejonesjnr)

    Thank you. I’ll give it a try and let you know.

    Thread Starter jejonesjnr

    (@jejonesjnr)

    I am getting the following error:

    Parse error: syntax error, unexpected ‘$image_fields’ (T_VARIABLE), expecting identifier (T_STRING)

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jejonesjnr

    Oh, that’s my fault. I didn’t notice a small type when copy-pasting code to post. I’m sorry about it!

    Please edit the code and in this line

    function wpmudev_update_post_acf_upload( $post_id, $field, $data, $cls ){\

    remove the \ character at the end. Correct line should be

    function wpmudev_update_post_acf_upload( $post_id, $field, $data, $cls ){

    Best regards,
    Adam

    Thread Starter jejonesjnr

    (@jejonesjnr)

    That sorted it however, you have a ‘}’ missing at the end of the file.

    ??

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jejonesjnr

    Right. I’m not sure how this happened but I’m deeply ashamed and my best best is that I really didn’t have enough coffee yet for today ??

    Again, I’m sorry for that! I’m glad you sorted it out!

    Best regards,
    Adam (running fast to a coffee machine…)

    Thread Starter jejonesjnr

    (@jejonesjnr)

    Ok, I am still getting the same scenario when I upload images. within the post, it says “No image Selected”.

    I am using the fieldname from the acf post type. In this case “image_two”, “image_three” etc…

    Within the form, the image fields are known as upload-2, upload-3 but in the custom data section, I am mapping upload-2 to image_two.

    Is this all correct?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jejonesjnr

    It does sound correct for as long as the “label” in mapping is the same as ACF field name.

    Would you mind exporting and sharing with us both form and ACF field group so we could test it on our end? It would make troubleshooting way easier.

    To export the form (note: this will only export form itself and not submissions – we don’t need submissions):

    – got to “Forminator -> Forms” page and click on a little “gear” icon next to the form in question
    – from the drop-down menu select “Export” and copy given code
    – put that code at https://pastebin.com service (as public paste)
    – and include link to it in your response here

    To export ACF field group:

    – go to “ACF -> Tools” page
    – check the checkbox for relevant field group in “Export” section and click on “Export as JSON”
    – put exported code on pastebin the same way so for the form or the file on your Googel Drive, Dropbox or similar
    – and share link to it in your response

    This way we’ll be able to test that exact form and ACF setting along with our code on our end and, I believe, get back to you with solution that would finally work for you.

    Best regards,
    Adam

    Thread Starter jejonesjnr

    (@jejonesjnr)

    You can access both files by clicking the link below. Let me know of there are any issues. Thanks.

    Forminator files

    Thread Starter jejonesjnr

    (@jejonesjnr)

    Were you able to access the files okay?

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @jejonesjnr

    Thanks for response and sharing those files.

    It seems to be related to current ACF fields settings. As mentioned earlier (when code was shared), ACF would accept IDs but it seems we didn’t made it clear.

    In ACF you can have “image” type field set to “Array”, “URL” or “ID” type. A few of your image fields are currently set to “Array” type which will not work – the data will be passed over from form to ACF but the data format will be incorrect.

    Those fields in ACF should be changed to “ID” instead of “Arary” and they should start working. The same applies to other image fields in your ACF – which are set to actually be just “Text” fields instead of image fields. That’s fine but as a “Text” field they will only contain number passed from form (that number is image ID) and ACF “won’t know what to do about those”.

    All in all: the “image” fields in ACF should be reconfigured to be all set to “ID” option and if necessary, whatever displays those images (e.g. some custom code) may also need to be adjusted but that depends on the site and how you display the images.

    Best regards,
    Adam

    Thread Starter jejonesjnr

    (@jejonesjnr)

    I set the first 2 image fields in ACF as “Image”. The others are text as I was just testing the code. I’m not sure why you see them all as text fields. I will digest everything else you’ve written here.

    Thread Starter jejonesjnr

    (@jejonesjnr)

    I set all the image fields to type of “ID” but unfortunately, this did not work.

Viewing 15 replies - 1 through 15 (of 43 total)
  • The topic ‘Issue with images’ is closed to new replies.