I am trying to:
To send email after submitting a form to email address in parent post using the code
below but it is not working.
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ( $form_data['id']==1534 ){
$id_contact = get_post_meta($post_id, '_wpcf_belongs_tenant-contact_id', true);
$to = get_post_meta($id_contact, "wpcf-contact-email", true);
$from = "property<noreply@gutech.edu.om>";
$subject = get_the_title($post_id);
$mailbody = get_post_meta($post_id, "wpcf-communication_description", true);
$status= wp_mail($to, $subject, $mailbody, $from);
}
}
When I specify the $to email to be my email it works fine but when I replace it with
$to = get_post_meta($id_contact, "wpcf-contact-email", true);
it can not receive the email.
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
The parent post ID is not stored in post meta, not since Types 3.0.
If you have created a one-to-many relationship then you need to use the Relationships API to retrieve the parent post ID.
Specifically, you would use the function toolset_get_related_post (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post ), like so:
$parent_id = toolset_get_related_post( $post_id, "relationship-slug" );
Can you try that in your code after editing for the correct relationship slug?
I have edited the code but still I do not receive any email.
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ( $form_data['id']==1534 ){
$id_contact = toolset_get_related_post( $post_id, "tenant_contact-communicaton" );
$to = get_post_meta($id_contact, "wpcf-contact-email", true);
$from = "property<noreply@gutech.edu.om>";
$subject = get_the_title($post_id);
$mailbody = get_post_meta($post_id, "wpcf-communication_description", true);
$status= wp_mail($to, $subject, $mailbody, $from);
}
}
Nigel
Supporter
Languages:
English (English )
Spanish (Español )
Timezone:
Europe/London (GMT+00:00)
Dump the variables to your debug.log and that should help determine where it fails.
If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);
That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.
Sprinkle some error_log calls in your code, e.g.
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ( $form_data['id']==1534 ){
$id_contact = toolset_get_related_post( $post_id, "tenant_contact-communicaton" );
error_log(print_r($id_contact, true));
$to = get_post_meta($id_contact, "wpcf-contact-email", true);
error_log(print_r($to, true));
$from = "property<noreply@gutech.edu.om>";
$subject = get_the_title($post_id);
$mailbody = get_post_meta($post_id, "wpcf-communication_description", true);
$status= wp_mail($to, $subject, $mailbody, $from);
}
}