Skip Navigation

[Resolved] cred_generic_field file cannot save file with full path

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 4 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1819107
Screenshot 2020-10-21 at 10.46.39 PM.png
Screenshot 2020-10-21 at 10.28.50 PM.png
Screenshot 2020-10-21 at 10.28.37 PM.png

Dear Sir/Madam,

I have a post form with

<div class="form-group">
	<label>[cred_i18n name='upload-receipt']Upload Receipt[/cred_i18n]</label>
	[cred_field field='upload-receipt' force_type='field' output='bootstrap' previewsize='thumbnail']
</div>

As I want to allow the user to register and upload the image immediately, so I add a cred_generic_field in user form with

<div class="form-group">
	<label>[cred_i18n name='gf-upload-receipt-label']Upload Receipt[/cred_i18n]</label>
	[cred_generic_field type='file' field='gf-upload-receipt']
      {
      "required":0
      }
      [/cred_generic_field]
</div>

I add code to save the cred_generic_field content to a custom post with

function auto_apply_competition($user_id){
    
    $new_post = array(
        'ID' => '',
        'post_status' => 'publish',
        'post_type' => 'vwcm-application',
        'meta_input' => array(
            'wpcf-upload-receipt' => $_POST['gf-upload-receipt']
        )
    );
    $post_id = wp_insert_post($new_post, 1);
}        
add_action( 'user_register', 'auto_apply_competition', 10, 1 );

Please advise how I can save the full path if I use the cred_generic_field as input, please note I didn't enable the Use the WordPress Media Library manager for image, video, audio, or file fields to both post form and user form but I don't think it is the issue why the code cannot save the full path.

#1822141

Hello,

The Toolset Forms shortcode cred_generic_field does not save value by default, see our document:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_generic_field
It can only render a field in the forms.

I suggest you create a custom file field with Toolset Types plugin, and put this file field into your post/user forms, after user submit and save the post/user, use action hook cred_save_data, to update your other custom fields values with the file field value, more help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/get_post_meta/
https://developer.wordpress.org/reference/functions/update_post_meta/
https://developer.wordpress.org/reference/functions/get_user_meta/
https://developer.wordpress.org/reference/functions/update_user_meta/

#1822681
screencapture-vwcm-hk-registration-application-2020-10-23-23_39_19.png

Dear Luo Yang,

No idea how I can put both post form and user form on the same big form (see attachment). I have the post form for user to apply the application if user has already registered, of course, I also want to allow user to register and apply the application together. Now I can let user to register and apply the application but can handle the file upload.

#1824277

One Toolset post/user form can handle only one post/user, so there isn't such kind of built-in feature within Toolset Forms plugin.
I suggest you try these:
1) User submit the registration form
2) User login your website, redirect him to a page
3) In this page, display post form for creating new post.

#1824281

Dear Luo Yang,

I know the Toolset post/user form can handle only one post/user, that's why I use the [cred_generic_field] to allow the user to submit the post data and I handle from backend using the function auto_apply_competition($user_id)

function auto_apply_competition($user_id){
     
    $new_post = array(
        'ID' => '',
        'post_status' => 'publish',
        'post_type' => 'vwcm-application',
        'meta_input' => array(
            'wpcf-upload-receipt' => $_POST['gf-upload-receipt']
        )
    );
    $post_id = wp_insert_post($new_post, 1);
}        
add_action( 'user_register', 'auto_apply_competition', 10, 1 );

It seems the [cred_generic_field type='image' field='gf-upload-receipt'] doesn't handle the file upload for Guest role.

If the image can't be handled, how can I let the user auto-login once he/she successfully register, so I can get the user_id and redirect him/her to another page with another post form to upload the image using [cred_field field='upload-receipt' force_type='field' class='form-control' output='bootstrap' previewsize='thumbnail']

#1824287

For the [cred_generic_field] issue, please have a look at my first answer:
https://toolset.com/forums/topic/cred_generic_field-file-cannot-save-file-with-full-path/#post-1822141
The Toolset Forms shortcode cred_generic_field does not save value, it can only render a field in the forms.

So in your case, you will need to try the workaround as I mentioned above: use a custom file field instead of generic field.

#1830877

Dear Luo Yang,

Do you mean I should create a custom post like a media and temporary store the image, then save it to post when cred_save_data?

How can I get back the uploaded image using the custom post, it looks the post form will be embedded into a user form, how to handle the cred_save_data when I submit the registration?
Best regards,

Kelvin

#1831643

There is a misunderstanding, the custom image field stores only an image URL, after user submit the form, the image was uploaded by WordPress, you just need to update your custom field value as the image field value, if you still need assistance for it, please provide a test site with the same problem, also provide detail steps to duplicate the problem. thanks