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 = '[email protected]';
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 = '[email protected]';
}
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/