I'm following the documentation to add more recipients to a post form, however the filter does not seem to fire.
This is the code:
<?php
/**
* Send email notifications to all connected lawyers when a document is submitted
* @filter cred_notification_recipients
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
/**** testing ok *****/
function cred_submit_complete_timestamp( $arg ){
error_log("cred_submit_complete: " . microtime() );
return $arg;
}
add_filter( 'cred_submit_complete', 'cred_submit_complete_timestamp' );
/**** testing no output *****/
function cred_notification_recipients_timestamp( $arg ){
error_log("cred_notification_recipients: " . microtime() );
return $arg;
}
add_filter( 'cred_notification_recipients', 'cred_notification_recipients_timestamp' );
/**
* Customise CRED notification recipients by adding a BCC
* to the the notification "Content submitted"
*/
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
// no output
error_log('modify_recipients');
// Check notification name matches target notification
if ( isset($notification['name']) && 'Document submitted' == $notification['name'] ) {
// get case from cookie
$origin_id = $_COOKIE['db_case'];
// error check validate case
// if case
$relationship_slug = 'case-lawyer';
$child_posts = toolset_get_related_posts(
$origin_id, // get posts connected to this one
$relationship_slug, // in this relationship
array(
'query_by_role' => 'parent', // origin post role
'role_to_return' => 'child', // role of posts to return
'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
'limit' => 999, // max number of results
'offset' => 0, // starting from
'orderby' => 'title',
'order' => 'ASC',
'need_found_rows' => false, // also return count of results
'args' => null // for adding meta queries etc.
)
);
// Start with an empty list of email addresses and empty list of post author IDs
$emails = "";
// Loop over each child post and get the post authors
foreach ($child_posts as $child_post) {
$author = $child_post->post_author;
$emails .= get_the_author_meta( "user_email", $author ) . ',';
}
// Add recipients by BCC
if ( $emails != '' ) {
$recipients[] = array(
'to' => 'cc',
'address' => $emails,
'name' => '',
'lastname' => ''
);
}
}
print_r($recipients);
error_log($recipients);
return $recipients;
}
Hi,
Thank you for contacting us and I'd be happy to assist.
I've checked the first two functions ( i.e. "cred_submit_complete_timestamp" and "cred_notification_recipients_timestamp" ) on my website and the filters "cred_submit_complete" and "cred_notification_recipients" both seem to be firing correctly.
My recommendation would be to use the step-by-step debugging approach in the actual function "modify_recipients", by adding the "error_log" with the processed values at each step. This will help in knowing exactly which part is working and from where it fails.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
My issue is resolved now. Thank you!
I needed to return the post object instead of just the id
'return' => 'post_object', // return array of IDs (post_id) or post objects (post_object)