[当我发布自定义帖子类型时,WordPress发送通知

问题描述 投票:2回答:1

[我想在WordPress中发布名为'Notification'的CPT时发送通知Web推送。

$data = array(
'app_id' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'data' => array(
    'foo' => 'bar'
),
'headings' => array(
    'en' => 'Test Notifications API'
),
'contents' => array(
    'en' => 'Test Notifications API'
),
'url' => 'PAGE URL',
'include_player_ids' => [
  'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
]);

$data_string = json_encode($data);

$ch = curl_init('https://onesignal.com/api/v1/notifications/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
));
$result = curl_exec($ch);

发布CPT通知时,我想发送$ result。

感谢您的帮助。

wordpress hook
1个回答
0
投票

[您需要将其挂接到某些动作,例如save_posttransition_post_status

function my_published_notification( $new, $old, $post ) {
     if ( $new === 'publish' && $new !== $old ) {
           // put whatever you want to do here
     }
}

add_action( 'transition_post_status', 'my_published_notification', 10, 3 );

对于CPT,您还具有一些自定义的挂接选项,例如save_*publish_*,其中*myCPTname

add_action( 'publish_myCPTname', 'my_published_notification', 10, 2 );

查看更多信息,确定钩子和操作以获取更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.