Skip Navigation

[Resolved] How to add a dynamic contact form in a custom post type template

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)

Author
Posts
#2628877

I have a housing listing website (houses and apartements for rent / sell)

I would like to add a dynamic contact form on each location's file that would allow the user to directrly contact the owner of the location.

I've created a CPT for the contact form and added custom fields. Then, I've added the Contact Form template to the Housing CPT template. Now I'm trying to connect the Form to the "owners-email" field of the Housing post, so the form is sent to this email once submitted.

Is there any tutorial that exist for this? I haven't found one yet.

My website link :
hidden link

#2628989

Hi,

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

In your contact form's 'Votre courriel' field, you can use the Toolset's Types Fields API shortcode, to set a default value:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#email )


[types field='owner-email' output='raw'][/types]

In the form field's settings, you'll see a 'Field Default Value' field where this shortcode can be used and you'll replace 'owner-email' with the actual owner email field's slug.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2629193

Hi Waqar,

The "Votre courriel" field is the user email, not the house owner's.

I have my House CPT template in wich I've added the Contact Form CPT template.

The Contact Form template needs to have access to the Housing owner-email field in a dynamic way, because it changes with each House files.

Here is my Contact Form CRED code. I've tried to add an hidden field that could retreive the Housing CPT owner-email's field, in order to use email notification to send an email to this specific owner-email field once the form is submitted, but it doesn't work.

Catherine 🙂

[credform]
<div class="form-group">
<label for="%%FORM_ID%%_form-contact-nom">[cred_i18n name='form-contact-nom-label']Votre nom[/cred_i18n]</label>
[cred_field field='form-contact-nom' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_form-contact-courriel">[cred_i18n name='form-contact-courriel-label']Votre courriel[/cred_i18n]</label>
[cred_field field='form-contact-courriel' force_type='field' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_form-contact-message">[cred_i18n name='form-contact-message-label']Votre message[/cred_i18n]</label>
[cred_field field='form-contact-message' force_type='field' class='form-control' output='bootstrap']
</div>
[cred_field field='form_submit' output='bootstrap' value='Soumettre' class='btn btn-primary btn-lg']
[cred_generic_field type='email']
{
"required":0,
"default":""
}
[/cred_generic_field]
[/credform]

<div style="display:none;">[cred_generic_field field='owner-email' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[wpv-post-field name='wpcf-mail' id='$current_page']"
}
[/cred_generic_field]</div>

#2629941

Hi Catherine,

To troubleshoot this, I'll need to see exactly how these fields and the form are set up in the admin area. Can you please share temporary admin login details, in reply to this message?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2631405

Thank you for sharing these details.

During troubleshooting, I made the following changes and the form notification is working now:

1. In the form 'Contact - Fiche', I've updated the shortcode that populates the generic field value to:
[wpv-post-field name='wpcf-courriel' id='$current_page']

The complete code for the form now looks like this:


[credform]
	<div class="form-group">
		<label for="%%FORM_ID%%_form-contact-nom">[cred_i18n name='form-contact-nom-label']Votre nom[/cred_i18n]</label>
			[cred_field field='form-contact-nom' force_type='field' class='form-control' output='bootstrap']
		</div>
		<div class="form-group">
			<label for="%%FORM_ID%%_form-contact-courriel">[cred_i18n name='form-contact-courriel-label']Votre courriel[/cred_i18n]</label>
			[cred_field field='form-contact-courriel' force_type='field' class='form-control' output='bootstrap']
		</div>
		<div class="form-group">
			<label for="%%FORM_ID%%_form-contact-message">[cred_i18n name='form-contact-message-label']Votre message[/cred_i18n]</label>
			[cred_field field='form-contact-message' force_type='field' class='form-control' output='bootstrap']
		</div>
		[cred_field field='form_submit' output='bootstrap' value='Soumettre' class='btn btn-primary btn-lg']
		[cred_generic_field type='email']
		{
			"required":0,
			"default":""
		}
		[/cred_generic_field]
		<div style="display:none;">
		[cred_generic_field field='owner-email' type='email' class='' urlparam='']
		{
			"required":0,
			"validate_format":0,
			"persist":1,
			"default":"[wpv-post-field name='wpcf-courriel' id='$current_page']"
		}
		[/cred_generic_field]
	</div>
[/credform]

2. In the form's notification settings, I've changed the trigger option to 'When submitting the form'.

Note: the trigger to send the notification on post status change is useful for the case when the post created on the form submission has a different status than the target status.

#2631499

It works perfectly, thanks! 😀