admin管理员组文章数量:1384366
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);
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);
Share
Improve this question
asked May 7, 2020 at 1:21
Chris ParsonsChris Parsons
36 bronze badges
1 Answer
Reset to default 0You 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/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.
本文标签: pluginsTwillio How To Send SMS for Custom Post Type
版权声明:本文标题:plugins - Twillio How To Send SMS for Custom Post Type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744496921a2609085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论