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

I'm trying to pass a custom field value to my functions.php file. I want to replace:

$to = get_bloginfo( 'admin_email' );

to something along the lines of:

$to = get_custom_field('sendto', TRUE);

So the user will have a recipients field on their post (custom post type) that they can add email addresses to, then when the post is published, those users will receive the notification as well.

Thank You for any help.

share|improve this question

I'm going to assume the folks receiving these emails has gone through the whole two step opt in procedure or they have given their consent in some other way.

There is an action called {$status}_{$post_type} that fires whenever a post is transitioned from one status to another. So when a post is published, the hook publish_post fires; when a custom post type is published the hook publish_the_custom_post_type is fired.

You can hook into this, grab whatever post meta you need, and send emails.


// replace 'publish_post' with 'publish_your_post_type'
add_action('publish_post', 'wpse52135_transition', 10, 2);
 * When a post moves from 'draft' to 'publish, send an email
 * @uses get_post_meta
 * @uses update_post_mtea
 * @uses wp_mail
function wpse52135_transition($post_id, $post)
    // store the fact that we sent an email in a custom field if that
    // field is present, don't resend
    if(get_post_meta($post_id, '_wpse52135_sent_mail', true))

    $email = get_post_meta($post_id, 'wpse52135_email', true);

    // No email?  bail.
    if(!$email || !is_email($email)) return;

    // email subject
    $subject = sprintf(
        __('New Post: %s', 'wpse52135'),

    // email body
    $msg = sprintf(
        __("Check out our new post: %s\n\n%s", 'wpse52135'),

    // send the email

    update_post_meta($post_id, '_wpse52135_sent_mail', true);

As a plugin.

share|improve this answer
Thanks Christopher for shedding some light on this. – Scott Meyers May 22 '12 at 23:42
great tips. thanks chris. – Fatih Toprak Dec 23 '12 at 22:20

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.