Skip Navigation

[Resolved] Passing an Address Field and Category field toa cpt with a Generic Field

This support ticket is created 3 years, 11 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/Karachi (GMT+05:00)

This topic contains 14 replies, has 2 voices.

Last updated by Waqar 3 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1573993

Hi,

I have a User front-end form where I'm passing custom fields to a cpt via generic fields. It seems I cannot do this with the address field though. Is this a known limitation or a bug? If it's a limitation is there any workaround that you know of to do such a thing?

hidden link

Also I'm trying to use a generic select field for category selection, but that also does not seem to work. Am I just doing this wrong?

[cred-generic-field field="new-business-listing" type="select" class=""]
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"Restaurant Delivery","label":"Restaurant Delivery"},
{"value":"Wines, Beers, Spirits","label":"Wines, Beers, Spirits"},
{"value":"Teaching","label":"Teaching"},
{"value":"Mental Health","label":"Mental Health"}
]
}
[/cred-generic-field]

Thanks,

Tim

#1574583

Sorry, the address field is working, I just didn't update the cred_save_data in my functions.php file.

So it's that category field and also the image field that are not working. In the form this is the image generic field I'm trying to save:

[cred_generic_field type='image' field='location-image']
{
"required":0
}
[/cred_generic_field]

And this is the code I am using in functions file:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    if ($form_data['id']==2172)
    {
        // Create post object
        $post_id = wp_insert_post( array(
	      'post_title'    => $_POST['directory-listing-title'],
		  'post_content'  => '',
		  'post_status'   => 'publish',
		  'post_author'   => $post_ID,
		  'post_type' => 'directory-listing'
	                ) );
		
		update_post_meta($post_id, 'wpcf-phone-number', $_POST['directory-listing-phone']);
		update_post_meta($post_id, 'wpcf-email-address', $_POST['directory-listing-email']);
		update_post_meta($post_id, 'wpcf-district', $_POST['select-district']);
		update_post_meta($post_id, 'wpcf-website', $_POST['directory-website']);
		update_post_meta($post_id, 'wpcf-facebook', $_POST['facebook']);
		update_post_meta($post_id, 'wpcf-place-description', $_POST['place-description']);
		update_post_meta($post_id, 'wpcf-coupon-details', $_POST['coupon-details']);
		update_post_meta($post_id, 'wpcf-user-category', $_POST['listing-category']);
		update_post_meta($post_id, 'wpcf-user-added-sub-category', $_POST['listing-sub-category']);
		
		update_post_meta($post_id, 'wpcf-new-business-listing', $_POST['new-business-listing']);
		update_post_meta($post_id, 'wpcf-location-image', $_POST['location-image']);
		update_post_meta($post_id, 'wpcf-location-address', $_POST['location-address']);
		
    }
}
#1575407

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Timothy,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot and test this on my own website, I'll need to see exactly how these fields and the form is set up in the admin area.

Can you please share temporary admin login details, along with the link to a page with this form?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1576815

Any update with this? I've also been trying to use the wp_set_object_terms function to map the category generic field to the new-business-listing category and no luck:

wp_set_object_terms($post_id, $category, 'new-business-listing', true);

The image upload issue seems to be fixed now, maybe due to today's Forms plugin update?

#1578661

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Timothy,

Thank you for waiting and sorry about the delay in getting back on this.

During troubleshooting, I noticed that you've rightly updated the "new-business-listing" generic field, to include the term slugs as values now.

The last line in the code snippet in your theme's "functions.php" file needs to be updated slightly:

From:


wp_set_object_terms($post_id, $category, 'new-business-listing', true);

To:


wp_set_object_terms( $post_id, $_POST['new-business-listing'], 'new-business-listing', true );

Ref: https://developer.wordpress.org/reference/functions/wp_set_object_terms/

This works on my test website and please let me know how it goes on yours.

regards,
Waqar

#1578679

That works. Thanks!

But weirdly now the image filed isn't working again. Same thing is happening that was happening before, the upload url seems to be just grabbing the file name and not the website path.

hidden link

Tim

#1581853

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that it worked.

I've performed some tests with a similar image type generic field, but couldn't reproduce the issue shown in your screenshot.

Even on your website's form, I'm able to see the selected image correctly.
( screenshot: hidden link )

Are you getting this error for any specific image?

Note: I also didn't find any image with name "image2(1).jpeg" in the media library.

If you're still experiencing this issue, please share the exact steps so that it can be reproduced.

#1582159

I mean on the form, the "Location Image" upload image field does not work. It doesn't upload the image. If you click upload image and select an image from your desktop you can see it does not work.

hidden link

Tim

#1585205

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your message.

I tested the image upload field by uploading an image from my computer.
( screenshot: hidden link )

Not only did the image uploaded successfully, but the form also correctly added a user and a "Directory Listings" post.
( you'll find a user named "Waqar TS Support" and the post named "Directory Listing Title TS support test" on the website )

I'll recommend testing the form again and if this is only happening for any specific image from your computer, please share that image with me.

#1585427

I see from the screenshot that you were already logged in when you uploaded the image. In that case the upload field does work. But this form is for new signups, non-logged in visitors, and in that case the image field doesn't work.

Tim

#1588075

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Tim,

Thank you for waiting, while I performed some further tests on my website.

I can confirm that when a visitor/guest submits a user form with a generic image field, the uploaded image's full file URL is not available in the "$_POST" global variable, which is why the respective image custom field value is not saved properly.

Unfortunately, a couple of workarounds that I tested to overcome this also didn't work.

The best way to deal with this situation would be to remove this image field from this first registration form and create a new edit form for "Directory Listings" posts, that contain this image field, which users can use only after logging in.

regards,
Waqar

#1588397

Hi Waqar,

"he uploaded image's full file URL is not available in the "$_POST" global variable"... so this is a bug right? Are you saying no one has implemented this functionality before? But I see many posts with others using the generic image field for similar purposes. This must be a bug a recent version of the Forms plugin.

One of the reasons a generic image field exists is for the purpose of providing the functionality that I am trying to implement. The workaround you suggest is very clunky and a huge friction point for users, and would simply be confusing.

There must be a fix here... all that needs to be done is have the site's url added to the image filename, right?

Tim

#1589445

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Tim,

I'm afraid, I couldn't find any information related to any recent changes related to the handling of image upload for generic fields or any previous support thread where users were able to achieve the same for the non-logged-in visitors.

However, I was able to make this work through a workaround.

You can register a new image type user field with slug "user-image" and instead of using an image type generic field include this field in your registration form.
( ref: https://toolset.com/documentation/user-guides/custom-content/user-fields/ )

When the form will be submitted, the image's URL will be saved in this user field, which can be then fetched through the code and set as a value in the custom field for the post, like other fields.
( and once it has fulfilled its purpose, that value can be removed from the user's field too )

The updated code will look like this:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	if ($form_data['id']==2172)
	{
		// Create post object
		$new_post_id = wp_insert_post( array(
			'post_title'    => $_POST['directory-listing-title'],
			'post_content'  => '',
			'post_status'   => 'publish',
			'post_author'   => $post_ID,
			'post_type' => 'directory-listing'
		) );
		
		update_post_meta($new_post_id, 'wpcf-phone-number', $_POST['directory-listing-phone']);
		update_post_meta($new_post_id, 'wpcf-email-address', $_POST['directory-listing-email']);
		update_post_meta($new_post_id, 'wpcf-district', $_POST['select-district']);
		update_post_meta($new_post_id, 'wpcf-website', $_POST['directory-website']);
		update_post_meta($new_post_id, 'wpcf-facebook', $_POST['facebook']);
		update_post_meta($new_post_id, 'wpcf-place-description', $_POST['place-description']);
		update_post_meta($new_post_id, 'wpcf-coupon-details', $_POST['coupon-details']);
		update_post_meta($new_post_id, 'wpcf-user-category', $_POST['listing-category']);
		update_post_meta($new_post_id, 'wpcf-user-added-sub-category', $_POST['listing-sub-category']);
		
		update_post_meta($new_post_id, 'wpcf-new-business-listing', $_POST['new-business-listing']);
		update_post_meta($new_post_id, 'wpcf-location-address', $_POST['location-address']);

		wp_set_object_terms( $new_post_id, $_POST['new-business-listing'], 'new-business-listing', true );

		// get image URL value from the user
		$user_image = get_user_meta( $post_id, 'wpcf-user-image', true );
		// if image URL exists
		if (!empty($user_image)) {
			// set the image URL as new post's custom field value
			update_post_meta($new_post_id, 'wpcf-location-image', $user_image);
			// delete the image URL from user's custom fiedl
			delete_user_meta($post_id, 'wpcf-user-image');
		}
	}
}

Note: The variable "$post_ID" holds the newly added user's ID while "$new_post_id" holds the newly added post's ID.

This should do the trick.

regards,
Waqar

#1593955

This works! Thanks so much for the extra effort, really appreciated.

I do think though the generic image upload field should work as other generic fields work, so you might want to add this as a feature request.

Thanks again,

Tim

#1594105

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're very welcome Tim and glad that it works.

The file uploading mechanism for a non-logged in visitor is complicated, because of security and privileges concerns, but I'm sure this thread will prove useful for someone trying to achieve the same.

Please feel free to submit any new feature request at:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.