0

Twillio has a tutorial where one assembles a plugin that sends SMS notices when 'post' is published. The issue I face is many 'post' types are published a day and that would generate an annoying number of test messages. So, I'm attempting to modify the code so a custom post type is used to generate SMS. I am so far unable to successfully generate a successful SMS event when publishing posts in the CPT called "SMS". Below is just one of my attempts. I try a conditional to run the code for my CPT, but it's not working (works fine for 'post' type or the default post)


function post_published_notification( $ID, $post ) {

  //$post_type = get_post_type($post);

  if ( get_post_type($post, get_the_ID() ) == 'SMS' ) {

    $sid = '##########';
    $token = '#############';
    $from = '+1530#######';
    $client = new Client($sid, $token);

    $title = $post->post_title;
    $body = sprintf('New Post: %s', $title);

    $blogusers = get_users('blogid=$ID&role=subscriber');
    foreach ($blogusers as $user) {
      $to = get_user_meta($user->ID, 'mobile', true);
      if (intval($to) == 0) {
        continue;
      }
      $client->messages->create(
        $to,
        array(
          'from' => $from,
          'body' => $body
        )
      );
    }
  }
}

add_action('publish_post', 'post_published_notification', 10, 2);
0

You should be able to change publish_post to publish_sms then remove any conditional logic. This is due to the following hook: https://developer.wordpress.org/reference/hooks/new_status_post-post_type/

This is a bit of an aside, but I'm making the assumption you've defined your post type key as sms (all lowercase) if not then you may need to use publish_SMS but it's generally best practice to use lowercase keys.

| improve this answer | |
  • 1
    Thanks Brooke. Removing conditional as you advised and changing the key from SMS -> sms .. and then changing publish_post to publish_sms allowed for a successful event. I appreciate your consideration. Thanks! – Chris Parsons May 7 at 6:04

Your Answer

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

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