Hi @shayno90
I hope you are doing well.
Can you please replace the previous code using this instead?
<?php
/**
* Plugin Name: [Forminator] - Updated version of Map ACF to upload field.
* Plugin URI: https://wpmudev.com/
* Description: Map ACF fields to upload field of Forminator when adding meta user.
* Author: Abdoljabbar Bakhshande
* Author URI: https://wpmudev.com/
* Task: SLS-4029
* License: GPLv2 or later
*
* @package WordPress
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_filter( 'forminator_replace_form_data', 'forminator_mu_map_acf_upload_field', 10, 3 );
function forminator_mu_map_acf_upload_field( $content, $data, $original_content ) {
if ( 'registration' === $data['form_type'] && ! empty( $original_content ) ) { // Check if it is registration form.
// Check if it is upload field that is mapped to ACF.
$upload_fields = array( '{upload-1}', '{upload-2}' ); // upload fileds IDs
if ( ! in_array( $original_content, $upload_fields ) ) {
return $content;
}
$form_id = $data['form_id'];
$email = $data['email-1'];
$original_content = str_replace( array( '{', '}' ), '', $original_content ); // Remove { and }.
$entry_model = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id ); // Get latest entry.
if ( $entry_model->meta_data['email-1']['value'] === $email ) { // Check if email is same.
if ( isset( $entry_model->meta_data[ $original_content ] ) ) { // Check if field exists.
$content = $entry_model->meta_data[ $original_content ]['value']; // Get value.
if ( is_array( $content ) && isset( $content['file'] ) ) { // Check if file exists.
$file_url = $content['file']['file_url']; // Get file url.
$attach_id = attachment_url_to_postid( $file_url );
if( $attach_id ) {
$content = $attach_id; // Get attachment id because ACF file field only accept attachment id.
$mime_type = wp_get_image_mime( $file_url );
if ( $mime_type ) {
$update_data = array(
'ID' => $attach_id,
'post_mime_type' => $mime_type,
);
wp_update_post( $update_data );
}
}
}
}
}
}
return $content;
}
Best Regards
Patrick Freitas