0

I am coping with the scenario where I need to filter content of my custom function. My function is used for sending emails but I need to filter it just like wp_mail hook do.

Here is my function:

function koku_crm_send_sendgrid($sendgrid_api_key, $to, $subject, $text, $html) {

  $sendgrid = new \SendGrid($sendgrid_api_key);
  $mail = new KCSendGrid\Mail();
  $from = new KCSendGrid\Email(get_bloginfo( 'name' ), get_bloginfo( 'admin_email' ));
  $mail->setFrom($from);
  $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));

  $content = new KCSendGrid\Content("text/plain", $text);
  $mail->addContent($content);

  $content = new KCSendGrid\Content("text/html", $html);
  $mail->addContent($content);

  $personalization = new KCSendGrid\Personalization();
  $to = new KCSendGrid\Email(null, $to);
  $personalization->addTo($to);
  $mail->addPersonalization($personalization);

  $sendgrid->client->mail()->send()->post($mail);

}

I want to filter that "$to" variable before sending the email. Something similar to the wp_mail filter hook.

I have search a lot, please help me in this regards. Thank you so much in advance.

1

You can use the apply_filters function:

function koku_crm_send_sendgrid($sendgrid_api_key, $to, $subject, $text, $html) {
  $to = apply_filters( 'koku_crm_send_to', $to );

  $sendgrid = new \SendGrid($sendgrid_api_key);
  $mail = new KCSendGrid\Mail();
  $from = new KCSendGrid\Email(get_bloginfo( 'name' ), get_bloginfo( 'admin_email' ));
  $mail->setFrom($from);
  $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));

  $content = new KCSendGrid\Content("text/plain", $text);
  $mail->addContent($content);

  $content = new KCSendGrid\Content("text/html", $html);
  $mail->addContent($content);

  $personalization = new KCSendGrid\Personalization();
  $to = new KCSendGrid\Email(null, $to);
  $personalization->addTo($to);
  $mail->addPersonalization($personalization);

  $sendgrid->client->mail()->send()->post($mail);

}

The first argument of apply_filters() is the name of the filter. It's what you'll use when calling add_filter(). The second argument is the value to be filtered.

Now you can filter $to like so:

function wpse_276933_send_to( $to ) {
  $to = 'new@example.com';

  return $to;
}
add_filter( 'koku_crm_send_to', 'wpse_276933_send_to' );

You can pass more values into the filter callback function by adding arguments to apply_filters(). These won't do anything on their own, but it means that they will be available when adding the filter. So if your filter was:

$to = apply_filters( 'koku_crm_send_to', $to, $subject, $text );

You could access $subject and $text in your filter function by including the arguments in the callback function and setting the fourth argument of add_filter() to 3, so that the function knows to accept 3 arguments:

function wpse_276933_send_to( $to, $subject, $text ) {
  if ( $subject === 'Subject One' ) {
    $to = 'new@example.com';
  }

  return $to;
}
add_filter( 'koku_crm_send_to', 'wpse_276933_send_to', 10, 3 );

Read more in the Plugin Handbook: https://developer.wordpress.org/plugins/hooks/custom-hooks/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.