Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer 1

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.

Example:

<?php
// 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))
        return;

    $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'),
        esc_attr(strip_tags($post->post_title))
    );

    // email body
    $msg = sprintf(
        __("Check out our new post: %s\n\n%s", 'wpse52135'),
        esc_attr(strip_tags($post->post_title)),
        get_permalink($post)
    );

    // send the email
    wp_mail(
        $email,
        $subject,
        $msg
    );

    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
add comment

Your Answer

 
discard

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.