0

I followed the Twilio tutorial on sending an SMS when a post is publsihed. It works, but any time you make a change to the post and update, it sends another SMS. Can anyone point me in the right direction of making it only send on initial publication.

Basically what we do is a post will be published in the Active Event category. After the event is done, we will move it to the closed event category. The way it works now our subscribers get notifications for each one.

function post_published_notification ( $ID, $post ) {
    // Your Account SID and Auth Token from twilio.com/console
    $sid = '###########';
    $token = '###########';
    // A Twilio phone number you purchased at twilio.com/console
    $from = "+1234567890";
    $client = new Client($sid, $token);

    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $body = sprintf('A new event has been posted: %s %s', $title, $permalink);

    $blogusers = get_users( 'blogid=$ID&role=subscriber' );
    foreach ( $blogusers as $user ) {
        $to = get_user_meta($user->ID, 'mobile-number', true);
        if ( intval($to) == 0 ) { continue; }
        // Use the client to do fun stuff like send text messages!
        $client->messages->create(
            // the number you'd like to send the message to
            $to,
            array(
                'from' => $from,
                // the body of the text message you'd like to send
                'body' => $body
            )
        );
    }
}
add_action('publish_post', 'post_published_notification', 10, 2);



?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.