Skip Navigation

[Resolved] Send contact form to custom post's email, saved in a custom field

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 15 replies, has 3 voices.

Last updated by Minesh 1 year, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2624517

Hi,
I believe this is a common scenario. I have a custom posts that are Shops. I need a contact form in this custom post's template, that sends a message to the shop. Shop's email is in a custom field. I need also to recieve a copy of message in my address.

I've searched the forum and I found several solutions: add To: address with PHP in webhook, use shop's email as a field in a CRED form (and then in notification), create Contacts as custom posts and a relation with Shops. Which is the right one?

Thanks!

#2624525

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Toolset forms are used to create or edit a post type. As you do not want to create a shop post type item then you will need to create a separate post type to handle the contact form.

You will be able to add the email address to that post type as a custom field and use the information like any other post form you add.

For more information:

https://toolset.com/forums/topic/building-a-basic-contact-form/#post-1960027

Also take this into account:

https://toolset.com/forums/topic/building-a-basic-contact-form/#post-1960033

Thanks.

#2626003

Hi Cristopher,
do I need a relationship between "shop" cpt and "contact" cpt?

Thank you,

#2626411

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I will ask the second tier about this and get back to you. It will be tomorrow as today is a weekend.

Thanks.

#2626535

Christopher Amirian
Supporter

Languages: English (English )

Screenshot 2023-07-24 at 10.44.54.png

Hi there,

Here is how to do it:

- Add a one-to-many relationship between shop and contact post types:

https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/

- Add a post form for the contact post type:

https://toolset.com/course-lesson/front-end-forms-for-adding-content/

- Add a custom email for the recipient which will send the contact form result to your email. (Check screenshot)

https://toolset.com/course-lesson/send-notifications-when-someone-submits-the-form/

- Add the custom code below to Toolset to retrieve the email address from the custom field of the shop post type:

add_filter('cred_notification_recipients', 'func_send_notification_to_parent_post_author', 10, 4);
function func_send_notification_to_parent_post_author($recipients, $notification, $form_id, $post_id){
    if ( $form_id == 99999){
 
        $relationship_slug = 'your-one-to-many-relationship-slug';
        $parent_post_id = toolset_get_related_post( $post_id, $relationship_slug );
 
        $email = get_post_meta($parent_post_id, 'wpcf-email', true);
          
        $recipients[] = array(
            'to'=>'to',
            'address'=>$email,
            'name'=>'',
            'lastname'=>''
        );
    }
    return $recipients;
}

- Replace99999 with the Contact form ID that you have created
- Replace your-one-to-many-relationship-slug with the relationship slug of shop and contact post types
- Replace wpcf-email with the custom field slug you have on shop post. Please consider that you need to keep wpcf- prefix. So if the custom field slug is "hello" you need to add "wpcf-hello" in the code.

Reference: https://toolset.com/forums/topic/contact-listing-owner-form/#post-2625315

Here are the details on how to add the custom code above to Toolset:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Thanks

#2626625

Hi,
I've created relationship, and a post for to add a new contact. I insert this form in Shop's template. But how can I link the new contact created with the shop that is shown?

I have tested the new contact form, with a notification that is sended to my address, and then your code, to add shop's email to recipients. And a new contact is created, but without any relation to the shop. I receive in my address notification, but shop does not recieve anything. If not relation is created between new contact and the shop, your code can't work, because there is no "parent" for the contact, is it right?

Thank you!

#2626677

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You mentioned that you only need the contact that is saved in the custom field of the page receive an email. And the code does that. You said:

I believe this is a common scenario. I have a custom posts that are Shops. I need a contact form in this custom post's template, that sends a message to the shop. Shop's email is in a custom field. I need also to recieve a copy of message in my address.

You did not mention any sort of a need to connect two systems together.

So please tell me in details what you want to achieve besides the initial point that you have mentioned.

Thanks.

#2626683

Hi Cristopher,
yes, I don't need, for now, to save anything. But I can't get shop's email without the relationship.

$relationship_slug = 'your-one-to-many-relationship-slug';
        $parent_post_id = toolset_get_related_post( $post_id, $relationship_slug );
  
        $email = get_post_meta($parent_post_id, 'wpcf-email', true);

If I only insert a post form in shop's template, no relationship is created.

#2626687

Christopher Amirian
Supporter

Languages: English (English )

#2626693

Hi Cristopher,
but this shows a button, not a form?

And when I choose this option, in shop's template, first question is "Page that contains the form"? The form doesn't exist, I need to create it in template. Sorry, I don't understand this option.

Thank you!!

#2626699

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

The idea is that in the contact form you add a select box that lists the shop pages and that will facilitate the actuation relationship between the form and the post:

https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/

Now that select box should be somehow get selected with the current post that the form is showing, for that you need that button which will give you a query string int he URL that will cause the form to be pre-selected that select option to create the relation.

I do not know any other way to pre-select that select box, maybe you can use some sort of Javascript code to select it.

Please add the select box first and test to see if the relationship is created and you get the correct email address or not.

if yes then you can share the link to the form and I might be able to come up with some sort of javascript code to preselect that select box

Thanks.

#2626935

Hi Christopher,
I've created link in shop's template, and then a page with a form to create a contacta. First field is a list of shops, and it gets selected when you click "Contactar" button in shop's page. Then the relation is created and your code to add shop's email to "To:" array, works.

So the basic idea works. But I find it so complicated... In shop's page a button to a page with a generic contact form, that you fill and submit, and a "thank you" message appears. And you cannot go back to shop's page with a button, you have to use Back in browser. It's a bad user experience.

It would be better to have child create form in parent's page. Like having contact's form in shop's page. If I can set a value for field='@ubicacio-contacte.parent', which selects the parent, it'll be perfect, using shop's ID. Is it possible?

Thank you!

#2627251

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

If you output a shortcode in the contact form, the context for the shortcode is wherever the form is displayed, so for you it would be shop currently being shown.

If you want to link the contact page to that shop you can use the wpv-post-id shortcode to provide the default value for the parent select field.

For more information:

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-id

If you still have the issue which you mentioned you can provide the login information and instructions on where to check and I will take a look into this.

Please make sure that you set the next reply as private if you want to provide the login info.

Thanks.

#2627971

Hi Christopher,
it works with shortcode. Great!

Just one last thing, can I add CC and BCC address to a notification? Maybe I can add them to $recipients[], in the function you gave me?

Thank you!

#2629817

Ping