Question

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);
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top