php - WordPress: Add custom add_filter for custom functions -


i coping scenario need filter content of custom function. function used sending emails need filter wp_mail hook do.

here 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 filter "$to" variable before sending email. similar wp_mail filter hook.

i have search lot, please me in regards. thank in advance.

i think need use apply_filters($tag, $value) passes 'value' argument each of functions 'hooked' (using add_filter) specified filter 'tag'. each function performs processing on value , returns modified value passed next function in sequence.

now let’s @ easy example in customizr theme. let’s change url of link in logo:

// change url linked logo add_filter( 'tc_logo_link_url', 'change_site_main_link' ); function change_site_main_link() {   return 'http://example.com'; } 

inside customizr core code, in function displays logo (in class-header-header_main.php), customizr has:

apply_filters( ‘tc_logo_link_url’, esc_url( home_url( ‘/’ ) ) ) 

this our add_filter() hooking itself. esc_url() function eliminates invalid characters etc. in urls , home_url() function retrieves home url site. without filtering, ‘tc_logo_link_url’ filter returns home page’s address.

in example, didn’t take notice of incoming arguments (the home url), because knew going overwrite it.

remember: when use filter, must return something.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -