• Resolved SH10151

    (@sh10151)


    I’m using a Forminator file upload form and have code that will add metadata to the DB when someone uploads a file (via the hook ‘forminator_custom_form_submit_before_set_fields’).

    But I’m trying to get the ‘post id’ of the newly uploaded file as it is in the Media Library. I’ve enabled the Forminator option to “Show files in Media Library”.

    I want to be able to take the Forminator ‘entry_id’ and look up the ‘post id’ of the file upload.

    My code is able to see the details about the file that is uploaded, but it only shows the Forminator info, such as:

    [entry_id] => 39
    [entry_type] => custom-forms
    [form_id] => 138

    I can’t find a way to get the WordPress ‘post id’ of the uploaded file from the Forminator ‘entry_id’. Is this possible? I need it to be able to set metadata for the newly uploaded file.

    Or, if Forminator can provide the URL of the file, I can look it up that way, but neither ‘post id’ nor ‘image url’ get passed in via the hook, just the Forminator details such as ‘entry_id’.

    • This topic was modified 4 years, 10 months ago by SH10151.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter SH10151

    (@sh10151)

    Btw, I tried using Forminator_API::get_entry($form_id, $entry_id) in the hopes it would return the ‘post id’ or even the image URL, but all it returns is the same info I already get passed in via the hook:

    Forminator_Form_Entry_Model Object
    (
        [entry_id] => 49
        [entry_type] => custom-forms
        [form_id] => 138
        [is_spam] => 0
        [date_created_sql] => 2020-05-12 11:50:09
        [date_created] => 12 May 2020
        [time_created] => 12 May 2020 @ 11:50 AM
        [meta_data] => Array
            (
            )
    )
    
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @sh10151

    I hope you’re well today!

    Forminator won’t give you a post_id of the image that way but you should be able to get both File URL and file path and that can then be used to get any information about the file from the database using core WP functions.

    THe “get_entry()” method would be a way to do it and if you are already using it then you should be able to access the file URL and file path directly like this:

    Forminator_API::initialize();
    
    $form_id = 123; // ID of a form
    $entry_id = 56; // ID of an entry 
    
    $entry = Forminator_API::get_entry( $form_id, $entry_id );
    
    $file_url = $entry->meta_data['upload-1']['value']['file']['file_url'];
    $file_path = $entry->meta_data['upload-1']['value']['file']['file_path'];

    $file_url and $file_path variables would contain image URL and image path, accordingly.

    Note: make sure to replace “upload-1” with an actual ID of your upload field, as used on form.

    Best regards,
    Adam

    Thread Starter SH10151

    (@sh10151)

    Hi Adam,

    Thanks for the reply.

    I tried what you suggested, but the entire $entry->meta_data array is coming up empty for me. I tried it a few times and print_r()’ed out the whole array, but there’s nothing there. I am passing in the form_id and the entry_id okay, and I am using the correct field name from the form (it is just the same default ‘upload-1’).

    Would this have anything to do with the hook I’m using, “forminator_custom_form_submit_before_set_fields”? Because I tried this before with get_entry() and get_entries(), but it was as though the data hadn’t yet been saved to the DB. get_entries() will get all the previously stored uploads just fine, but not the current one being uploaded.

    Is there a different hook to use after the upload has been stored in the DB and available in the API? Or maybe I’m doing something else wrong?

    This is the exact code I’m using:

    Forminator_API::initialize();
    $uploaded_file_info_from_Forminator = Forminator_API::get_entry( $attachment_ID->form_id, $attachment_ID->entry_id );
    
    write_log('uploaded_file_info_from_Forminator->meta_data:');
    write_log($uploaded_file_info_from_Forminator->meta_data);
    
    $file_url = $uploaded_file_info_from_Forminator->meta_data['upload-1']['value']['file']['file_url'];
    $file_path = $uploaded_file_info_from_Forminator->meta_data['upload-1']['value']['file']['file_path'];
    write_log('file_url = ' . $file_url);
    write_log('file_path = ' . $file_path);
    Thread Starter SH10151

    (@sh10151)

    This is what the empty meta_data output looks like in my debug log:

    [14-May-2020 23:30:54 UTC] Forminator_Form_Entry_Model Object
    (
        [entry_id] => 73
        [entry_type] => custom-forms
        [form_id] => 138
        [is_spam] => 0
        [date_created_sql] => 2020-05-14 16:30:54
        [date_created] => 14 May 2020
        [time_created] => 14 May 2020 @ 16:30 PM
        [meta_data] => Array
            (
            )
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @sh10151

    Thanks for response!

    Actually, please try “var_dump” it rather than “print_r()”. It’s an object containing arrays rather than a plain array. “var_dump()” will show the content as long as the content is there.

    If there’s not then also please make sure that “upload” field in settings had the “Show files in media library” option enabled as it might have a meaning here (I admit I didn’t test it).

    An finally, while I tested my code “as is” I admit I didn’t check it with the hook that you were using, the “forminator_custom_form_submit_before_set_fields”. Thinking about it now, I think it might actually not have that data – as the would be created upon submission saved to DB, so it might be necessary to try to read it “later” – but please check “var_dump” and the field setting that I mentioned, first.

    Best regards,
    Adam

    Thread Starter SH10151

    (@sh10151)

    Hi Adam,

    Thanks. I’m pretty sure print_r() works on objects, but I tried var_dump(), and got similar results, just slightly different syntax:

    object(Forminator_Form_Entry_Model)#263 (10) {
      ["entry_id"]=>
      int(80)
      ["entry_type"]=>
      string(12) "custom-forms"
      ["form_id"]=>
      string(3) "138"
      ["is_spam"]=>
      string(1) "0"
      ["date_created_sql"]=>
      string(19) "2020-05-15 11:00:45"
      ["date_created"]=>
      string(11) "15 May 2020"
      ["time_created"]=>
      string(22) "15 May 2020 @ 11:00 AM"
      ["meta_data"]=>
      array(0) {
      }
    }

    Unfortunately, the meta_data property array is still empty.

    And when trying to access that object property (here: [‘upload-1’][‘value’][‘file’][‘file_url’]) of the meta_data, my logs say PHP Notice: Trying to access array offset on value of type null, which is consistent with the var_dump() output saying meta_data is null. So I think it really actually isn’t there.

    “Show files in media library” is enabled for this form, too, yes.

    I’m leaning toward the idea that the meta_data is just not available yet in the upload when using that hook.

    Is there a Forminator hook that fires after the data is saved to the DB?

    Not sure what to try next. Any ideas?

    Thanks again.

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @sh10151 ,

    I’ve asked our developers for a suggestion of what else to check. We will keep you posted.

    kind regards,
    Kasia

    Thread Starter SH10151

    (@sh10151)

    Hi Kasia,

    Thank you very much. I appreciate it!

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello @sh10151

    Can you try the following snippet?

    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_retrive_uploaded_file', 10, 3 );
    function wpmudev_retrive_uploaded_file( $entry, $form_id, $field_data_array ){
    	foreach( $field_data_array as $field ){
    		if( $field['name'] === 'upload-1' ){
    			$attachment_url = $field['value']['file']['file_url'];
    			$attachment_id = attachment_url_to_postid( $attachment_url );
    			// continue your code here
    			if( $attachment_id ){
    			}
    		}
    	}
    }

    Warm regards,
    Dimitris

    Thread Starter SH10151

    (@sh10151)

    That did it!
    Thank you Dimitris.

    I appreciate all the help, Adam, and Kasia, too. You guys really do have amazing, responsive support and developers. Very impressed with how quickly you guys replied each time and kept trying to help me.

    Thanks again, and all the best!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to get Media Library attachment id (post id) of uploaded file?’ is closed to new replies.