code snippet in developer hooks not working - Store file upload URL in another field

By: Jay Mac | Asked: 03/08/2023
ForumsCategory: General questionscode snippet in developer hooks not working - Store file upload URL in another field
Jay Mac asked 2 years ago
This code snippet from https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-store-file-upload-url-in-another-field Does not appear to work. I changed the line $_POST['item_meta'][$posted_field->id] = wp_get_attachment_url( $_POST['item_meta'][20] );  to $filename= wp_get_attachment_url( $_POST['item_meta'][20] );  to print the filename in the debug.log and it is always blank. The file uploads and there are no errors   add_filter('frm_validate_field_entry', 'filename_to_field', 10, 3); function filename_to_field($errors, $posted_field, $posted_value){ if ( $posted_field->id == 25 ) { //change 25 to the ID of the field to store the file url in   $_POST['item_meta'][$posted_field->id] = wp_get_attachment_url( $_POST['item_meta'][20] );  } return $errors; } Any thoughts as to why it is not working?
2 Answers
Victor Font answered 2 years ago
By changing that line, you placed the value into the $filename variable that you never use to apply to a field. Once you leave the function, $filename no longer exists because you've scoped it to be visible in the function only. You are not writing it to the debug log or storing the value in a field. You shouldn't have changed the example to use the $filename variable and then not use it for anything.
Jay Mac replied 2 years ago

Thanks Victor - I have my own function that writes to the debug log. I did not include this part of the code

jm_write_log("5.0 blockfilename=". $filename);
$_POST['item_meta'][$posted_field->id] = $filename;

The $filename is blank

Victor Font replied 2 years ago

Your if statement will only work for $posted_field->id == 25. Is that the correct field id for the field you want to store the file name? What is the value of $_POST['item_meta'][20]. Did you cross reference $_POST['item_meta'][20] to make sure its value is the same as the attachment id in the media library? What value is wp_get_attachment_url() returning? I would set a break point and examine the values of the $errors, $posted_field, and $posted_value parameters also.

Jay Mac replied 2 years ago

As a follow up - I ran the script as is in the documentation just replacing the field ids as directed with my field ids. Same result nothing no file url

Jay Mac replied 2 years ago

That is the problem the function wp_get_attachment_url() is returning nothing.

Victor Font replied 2 years ago

That's exactly why I asked if you cross referenced the data. It could be a timing issue with the hook you're using. Make sure the attachment id exists in the media library and that its attachment id matches what's in your field. You also have to ensure you're passing the right value to the WordPress function. You need to use breakpoints when you debug. Look up Kint. That's what we use.

I've given you all the right steps to do. Anything more, is beyond what I can offer in my scope as a community volunteer. Finish troubleshooting as I explained and you'll find the problem. The problem is not with the code, but more likely the data. You need to know the value of everything going through that channel.

When you finish your own debugging, if things don't work out, you may need the help of a developer. Please visit https://formidable-masterminds.com/developers-directory/ to search through a list of the best Formidable talent in the world. No boast.

Jay Mac replied 2 years ago

Thanks Victor there seems to be several problems going on here including with my theme

Jay Mac replied 2 years ago

Hi Victor - I think I found the problem, but I don't know the solution. I am using the code below to change the uploads folder directory and it works fine. The problem is that now the image upload is writing the file with different permissions than it does if it just writes to the default formidable uploads directory. I have turned off all media settings that generate multiple image sizes. Any idea why it is now writing different permissions?

The default permissions: -rw-r--r-- 1 apache apache

The permissions it is writing with the new upload folder: --w------- 1 apache apache

The code to change the upload folder

add_filter( 'frm_upload_folder', 'frm_custom_upload', 10, 2 );

function frm_custom_upload( $folder, $atts ) {

$form_key = FrmForm::get_key_by_id($atts['form_id']);

if ( $form_key == 'sponsor_challenge_block_management0001' ) {

global $current_user;

$folder = "formidable" . '/' . $current_user->user_login;

}
return $folder;
}

Victor Font replied 2 years ago

File permissions are inherited from the folder. The Formidable upload field doesn't assign permissions to anything it uploads. Permissions are managed solely by your server's operating system. You have to FTP into your site and change the formidable/folder permissions manually and allow the new permissions to cascade to the sub-folders. If "formidable" is a subfolder of the WordPress uploads directory, I don't think you'd be experiencing these permissions issues.

Jay Mac replied 2 years ago

I figured out the permissions were being set by the Forms File Protection. Once I disabled it the permission problem went away. Thanks

Victor Font replied 2 years ago

Good find! I forgot about Formidable security. It's been a very long time since I've had to use it.

Jay Mac answered 2 years ago
The problem was that I had the multiple file upload box checked in the upload field. As soon as I unchecked that box it works. I wish they would more clearly state the ramifications of checking those boxes. Even though while testing I was only uploading only one file at a time there was no indication that would cause a problem with their code snippet. The first problem of file permissions which they did not document that it changed the rw permissions to the file. I lost a lot of days due to incomplete documentation.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right