Home › Forums › Front-end Issues › Can a visitor (anonymous user) upload a file from the front end
I’m building a front end form and we’d like the user to be able to upload a file. Visitors will not have accounts or be logged in. Is this possible?
Currently I’m getting this error in the media uploader:
An error occurred in the upload. Please try again later.
And firebug shows that the process gets hung up with “async-upload.php” – no post or response is logged
Any ideas on how I can give a visitor the necessary capabilities?
Hi @locomo
Currently, the WP uploader requires you to be logged in 100%.
I will soon include a simple but effective basice file uploader which you can download and test from this thread:
http://support.advancedcustomfields.com/forums/topic/acf-add-image-by-user-gravity-form/
Hope that helps.
Thanks
E
worked great on the backend but on a front end form the only thing that gets saved is the filename .. a new attachment post is not actually created
is this not supported in the field yet?
thanks ..
ps. this is really great and IMO a nice user experience for front end users than the WP media uploader
Hi @locomo
Thanks for the feedback.
I’ll do some more work on the field soon. Feel free to do some debugging in the file yourself for now.
Thanks
E
i think i found the issue – for some reason the following action is not being fired on the front end:
add_action('post_edit_form_tag', array($this, 'post_edit_form_tag'));
as a work around i use the form_attributes argument to the acf_form function when i create my front end form and it works perfectly
'form_attributes' => array(
'enctype' => 'multipart/form-data',
),
Found one more issue that other people might find helpful. The Basic File uploader fails validation if it is a required field. To solve this I hook into “acf/validate_field” like this (let me know if there is a better way):
jQuery(document).on('acf/validate_field', function( e, field ){
var $field = jQuery(field);
if( $field.find('input[type="file"]').exists() ) {
$field.data('validation', true);
if( $field.find('input[type="file"]').val() == '' ) {
$field.data('validation', false);
}
}
});
found one more issue .. when using the Basic File field on a front end form and the field not required, a new (empty) attachment is created even if no file is uploaded by the user
btw, i’ll keep logging them here to keep the issues together, but let me know if you’d rather i open a new post to compile the issues i’m finding
Using this basic uploader for a front-end form as well.
Is there a code base for this anywhere yet? Wondering if I should throw this up on Github for collaboration and fixes.
I’d be happy to keep contributing to this .. I’m using it in its current form (along with the changes I’ve posted above) but there are still a couple of wonky things about it that I’d love to get cleaned up
I’m not aware of a codebase for this on Github
Hi,
thanks for your work on the basic uploader! I’m facing the same challenge and would highly appreciate if someone has a working codebase.
Here is the basic file upload field type: https://github.com/tamagokun/acf-basic-file
One problem I ran into was that the form needed to have it’s enctype set, which I did at the time with the class name of the form. To make this more universal I changed it to try to find the parent form element of the field. Didn’t test it though, so you may have to change this line: https://github.com/tamagokun/acf-basic-file/blob/master/basic_file-v4.php#L268
I used that field type in conjunction with a plugin I wrote specifically for creating dynamic front-end forms using ACF: https://github.com/tamagokun/acf-forms
Thanks a lot for sharing, tamagokun!
The only real problem I run into is that an uploaded “file” is shown even if the user didn’t upload anything. Do you have a clue how this could be changed?
Many thanks in advance!
Interesting, i’ll have to take a look-see. I don’t remember running into this problem last time I used this, but that was quite some time ago.
I had two problems with it, but got the solutions.
First – the annoying Strict Standards warning.
Changeline in the plugin file:
$o['name'] = end(explode('/', $o['url']));
into this:
$nval = explode('/', $o['url']);
$o['name'] = end( $nval );
Second – not uploading actual files, just the filenames. I followed what @locomo wrote above, and it worked.
The topic ‘Can a visitor (anonymous user) upload a file from the front end’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.