Skip Navigation

[Resolved] Embedded CRED in CPT post allowing user to send message to post's contact person

This support ticket is created 7 years, 7 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
- 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 22 replies, has 2 voices.

Last updated by Minesh 7 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#507907

I am trying to:

I want to embed a CRED contact form into a CPT post template, allowing a viewer to contact the posts' contact person, which can either be the post authors' email or a different email (post user meta email or cred field set email).

The problem is that using conditionals I can access the post authors' email, but not the posts' custom contact email (enabled by a radio field) so data is not available for notifications purposes. Since the field wpcf-courriel-contact-on (which determines if we use post authors' email or another contact email) is not from this cred, I think it may cause an issue.

I also linked this CRED to a CTP called form_contact that stores this internally for the only purpose of sending the notification email. No data is visible to anyone in the admin panel.

My code :
[credform class='or_form']
[cred_field field='form_messages' value='']

<h2>[wpml-string context="cred-form-contact"]Besoin d'informations?[/wpml-string]</h2>
<div class="form-group">
<label for="form-contact-nom">[wpml-string context="cred-fcontact"]Votre Nom[/wpml-string]</label>
[cred_field field='form-contact-nom' post='form-contact' value='' urlparam='']
</div>

<div class="form-group">
<label for="form-contact-courriel">[wpml-string context="cred-form-contact"]Votre Courriel[/wpml-string]</label>
[cred_field field='form-contact-courriel' post='form-contact' value='' urlparam='']
</div>

<div class="form-group">
<label for="form-contact-msg">[wpml-string context="cred-form-contact"]Sujet[/wpml-string]</label>
[cred_field field='post_title' post='form-contact' value='' urlparam='']
</div>

<div class="form-group">
<label for="form-contact-msg">[wpml-string context="cred-form-contact"]Votre Message[/wpml-string]</label>
[cred_field field='form-contact-msg' post='form-contact' value='' urlparam='']
</div>

[wpv-conditional if="($(wpcf-courriel-contact-on) eq 1 )"]
[cred_generic_field field='post_auth_email' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-author format="meta" meta="user_email"]"
}
[/cred_generic_field]
[wpv-conditional]
[wpv-conditional if="($(wpcf-courriel-contact-on) eq 2 )"]
[cred_generic_field field='post_auth_email' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[types field='son-courriel' output='raw'][/types]"
}
[/cred_generic_field]
[wpv-conditional]
[cred_field field='recaptcha' value='' urlparam='']
[cred_field field='form_submit' value='[wpml-string context="cred-form-contact"]Soumettre[/wpml-string]' urlparam='']
[/credform]

#508071

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to rename the field name and try to use following code, also you did not close the [wpv-conditional] shortcode properly. I have done that like [/wpv-conditional].

[wpv-conditional if="($(wpcf-courriel-contact-on) eq 1 )"]
[cred_generic_field field='post_auth_email-1' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-author format="meta" meta="user_email"]"
}
[/cred_generic_field]
[/wpv-conditional]

[wpv-conditional if="($(wpcf-courriel-contact-on) eq 2 )"]
[cred_generic_field field='post_auth_email-2' type='email' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[types field='son-courriel' output='raw'][/types]"
}
[/cred_generic_field]
[/wpv-conditional]

Also, check without changing the field names.

#508238

Hi Minesh

Thank you for catching the closure mistake.

However even though I've replaced the block of code by yours the notification module doesn't see the generic field and doesn't let me select it in the drop down. Aside of this, a new complication arises as the fields ( post_auth_email-1 and post_auth_email-2 ) are now different, and the email needs to be sent to only one address right now and not both.

Thanks! 🙂

#508244

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry - I forget to add this that generic fields will not be available to select with CRED notification.

You need to use the filter cred_notification_recipients here :

Add following code to your theme's functions.php file:

add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients){
    if (!is_array($recipients)) {
        $recipients=array();
    }
    if (isset($_POST['_cred_cred_prefix_form_id']) && 99999== $_POST['_cred_cred_prefix_form_id']){
         
           $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$_POST['post_auth_email'] );
             
        }
    }
    return $recipients;
}

Where - Replace 99999 with your CRED form ID.

#508348

Hi Minesh,

It worked!

But one thing. The notification is sent from "WordPress [wordpress@theme.onroule.org]". Is it possible to have it sent from something else, like sender's Name/Email?

#508359

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - CRED offer hook to setup email headers as well.

For example - add following code to your functions.php file:

add_filter( 'cred_mail_header', 'add_my_header', 10, 3 );
function add_my_header( $headers, $formid, $postid, $notification_counter = 0 ) {
    if ( $formid === 24 ) {
      $email = your-from-email ;
      $name = your from-name;
      $myheaders = array( 'From: ' . $nom . ' <' . $email . '>' );
      return array_merge( $headers, $myheaders );
    }
}

Where - Replace your corresponding variable values such as $formid, $email and $name.

More info with Detailed Doc:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred-mail-header

#508361

Hi Minesh,

I added the code, but now I get an error ("There was an error while submitting the form")

#508362

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What code did you add to your functions.php file?

Did you tried to replace the replace your corresponding variable values such as $formid, $email and $name?

#508367

Yes 🙂
I replaced it with:

if ( $formid === 35371 ) {
$email = $_POST['form-contact-courriel'] ;
$name = $_POST['form-contact-nom'] ;

#508369

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try with following code:


add_filter( 'cred_mail_header', 'add_my_header', 10, 3 );
function add_my_header( $headers, $formid, $postid, $notification_counter = 0 ) {
    if ( $formid === 35371  ) {
      $email = get_post_meta( $postid, 'wpcf-form-contact-courriel' , true );
     $name = get_post_meta( $postid, 'wpcf-form-contact-nom' , true );
      $myheaders = array( 'From: ' . $name . ' <' . $email . '>' );
      return array_merge( $headers, $myheaders );
    }
}
#508371

I still get the error. Do you want to access the site?

#508373

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes please.

Please share problem URL where I can see CRED form.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#508375

Hi Minesh,

I spoke too fast. It works! 🙂

But, it seems that if I am connected, the notification is sent to both the receiver AND my user account email. But if I am not connected, the notification is only sent to the contact person. Is there a way that both (sender's and receiver's) receive the email?

#508376

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you can add as many notifications as you want using following filter - which you are already using:

See below code where I've added one more notification for field: "wpcf-form-contact-courriel"

add_filter('cred_notification_recipients', 'my_cred_notification_recipients_func', 10, 4);
function my_cred_notification_recipients_func($recipients){
    if (!is_array($recipients)) {
        $recipients=array();
    }
    if (isset($_POST['_cred_cred_prefix_form_id']) && 99999== $_POST['_cred_cred_prefix_form_id']){
          
           $recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$_POST['post_auth_email'] );


// this is added for field - "wpcf-form-contact-courriel" - you can change the variable if required
$recipients[] = array('to'=>'to', 'name'=>'', 'lastname'=>'', 'address'=>$_POST['wpcf-form-contact-courriel'] );

              
        }
    }
    return $recipients;
}
#508381

Ok.
I now have 2 receivers, but if I am connected, I also receive it to my user account email.