WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can this work? I want to send a email when a post is published to the recipient, that is called in custom field.

EDIT I changed the code to another example I found.

function wpr_Notification($post_id) {
    $post = get_post($post_id);
    $to = get_post_meta($post_id, 'recipient', true);
    $headers = 'From: Disctopia Metal Webzine <admin@disctopia.de>';
    $title = $post->post_title." has been published";
    $message = " Hello ... and so on.... ";

    wp_mail($to, $title, $message, $headers); 
add_action('publish_post', 'wpr_Notification');

Everything works smooth except for the recipient. If I put an static email adress into this field instead of $to, it works. But if I try to get the value of the custom field, it fails.

I tried to see whats the value of $to by putting it into the $message and changing the $to to my own email adress. Well, there was no value at all for the $to.

What is wrong with this variable? The custom field definitely is called "recipient" and is just a text-string.


share|improve this question

Yes, it can work. But you should filter the email in the custom field. Users might have a error in his context or the mail address is wrong.

You can filter the email address with filter_var()

share|improve this answer
Hi Frank, you have no idea how often your blog posts on your website have helped me. :-) Well, I tried this code of me, but it didn't work. The inserted email adress is correct and the email was definitely not sent (looked even in my spam folder). Something must be wrong... :( Any clue? I would like to filter the adress, but I haven ever used this function. I am not used to PHP at all... – Martin Dec 24 '12 at 10:46
Thanks and welcome. How you include this code? set a var_dump() and output for debugging purpose all var from wp_mail() function to check. – bueltge Dec 24 '12 at 12:37
I have put everything into my function.php within my theme-folder. Shouldn't that work itself? D: filter and all that stuff sounds nice, but is beyond my knowlegde. ;-) – Martin Dec 24 '12 at 13:04
changed the root question :) – Martin Dec 24 '12 at 15:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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