• Resolved tomaspe96

    (@tomaspe96)


    Hi! I’ve been using your plugin for about a year now and it’s been awesome!

    Now i’m trying to do something a bit more tricky: to populate some custom fields and publish a post, the simple fields like text were no issue, but the only image i’m able to succefully populate is the Featured Image, is there a way to do this?

    One of the Custom Fields is an image_advanced field (https://docs.metabox.io/fields/image-advanced/) which lets the user upload up to 6 images.

    I created a File Upload field in forminator and linked it to the Custom field ID, but it doesn’t populate the fields with images, only the plain URL.

    I’ve seen a lot of post where you fix this with shortcodes in the mu-plugin folder, but i believe that it was for a single image field, is there a way to do it for a multiple images field?

    I’ve uploaded the json generated in forminator and the snippet that i tried to modifie from a previous post:
    https://drive.google.com/drive/folders/1aoDUn2Ad7rkYxOsX-IuNSJZGteDNdeD1?usp=sharing
    (I couldn’t export a “prettier” json, if there is a way, please let me know!)

    The gallery custom field id is “blc_slider”

    Thanks in advance,
    Tomás.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @tomaspe96

    I hope you are doing well.

    It should be possible with some custom coding as you are trying to do, I am not familiar to metabox plugin since I see you are using it but we forwarded the code and form to our developers to take a closer look.

    We will keep you posted.
    Best Regards
    Patrick Freitas

    Thread Starter tomaspe96

    (@tomaspe96)

    Thanks for the reply @wpmudevsupport12 ! If the team is or was able to do such a thing with ACF gallery field before, I could try to make it work with some tweaks in metabox, i don’t want to bother the team too much!

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    HI @tomaspe96

    Thanks for response!

    Our team will look into it since Patrick already ask them to do so ??

    I can’t promise at this moment if/what will they be able to come up with but surely they’ll check it and once we got feedback we’ll update you here agian.

    Have a great day!
    Adam

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @tomaspe96

    I just got code from our developers, it should help.

    <?php 
    
    add_action( 'forminator_post_data_field_post_saved', 'wpmudev_change_upload_field_image', 10, 4 );
    function wpmudev_change_upload_field_image( $post_id, $field, $data, $Forminator_Postdata ){
    	$post_meta   = isset( $data['post-custom'] ) ? $data['post-custom'] : '';
    	$img_id = array();
    	if ( !empty( $post_meta ) ) {
    		foreach ( $post_meta as $pmeta ) {
    			if( $pmeta['key'] == 'blc_slider' ){
    				delete_post_meta( $post_id, $pmeta['key'] );
    				if( strpos( $pmeta['value'], ',' ) !== false ){
    					$images = explode(', ', $pmeta['value']);
    					foreach( $images as $img_key => $img_url ){
    						add_post_meta( $post_id, $pmeta['key'], attachment_url_to_postid( $img_url ) );
    					}
    				}else{
    					add_post_meta( $post_id, $pmeta['key'], attachment_url_to_postid( $pmeta['value'] ) );
    				}
    			}
    		}
    	}
    }

    To use it on site as MU plugin:

    – create an empty file with a .php extension
    – put the code into it
    – save the file and upload it to the “/wp-content/mu-plugins/” folder of your site’s WordPress installation.

    Note also:

    – this code assumes that you didn’t change “mapping” of your upload field to custom field; if it’s changed you may need to adjust form field and custom field names in the code

    – to allow multiple fields, you will need to change setting of upload field in the form from “single” to “multiple”

    I’d also strongly recommend testing this first on some dev/staging site before adding to live one.

    Best regards,
    Adam

    Thread Starter tomaspe96

    (@tomaspe96)

    Wow it works flawlessly! Thank you so much ?? hope it gets added eventually to the plugin natively!

    Again, thanks, you guys have awesome support, i Will be buying it soon ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Connect a File Upload field to a Gallery Custom Field’ is closed to new replies.