我想在发布自定义帖子时发送推送通知(在发布或更新时发送),我尝试使用此代码创建自定义帖子并发送通知。
<?php
/*
Plugin Name: Send Notification CPT
Description: Declares a plugin that will create a custom post type to send notifications.
Version: 1.0
*/
if( ! defined(\'ABSPATH\')) {
exit;
}
// Register Custom Post Type Send Notifiction
function create_sendnotifiction_cpt() {
$labels = array(
\'name\' => _x( \'Send Notifiction\', \'Post Type General Name\', \'textdomain\' ),
\'singular_name\' => _x( \'Send Notifiction\', \'Post Type Singular Name\', \'textdomain\' ),
\'menu_name\' => _x( \'Send Notifiction\', \'Admin Menu text\', \'textdomain\' ),
\'name_admin_bar\' => _x( \'Send Notifiction\', \'Add New on Toolbar\', \'textdomain\' ),
\'archives\' => __( \'Send Notifiction Archives\', \'textdomain\' ),
\'attributes\' => __( \'Send Notifiction Attributes\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent Send Notifiction:\', \'textdomain\' ),
\'all_items\' => __( \'All Send Notifiction\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Send Notifiction\', \'textdomain\' ),
\'add_new\' => __( \'Add New\', \'textdomain\' ),
\'new_item\' => __( \'New Send Notifiction\', \'textdomain\' ),
\'edit_item\' => __( \'Edit Send Notifiction\', \'textdomain\' ),
\'update_item\' => __( \'Update Send Notifiction\', \'textdomain\' ),
\'view_item\' => __( \'View Send Notifiction\', \'textdomain\' ),
\'view_items\' => __( \'View Send Notifiction\', \'textdomain\' ),
\'search_items\' => __( \'Search Send Notifiction\', \'textdomain\' ),
\'not_found\' => __( \'Not found\', \'textdomain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'textdomain\' ),
\'featured_image\' => __( \'Featured Image\', \'textdomain\' ),
\'set_featured_image\' => __( \'Set featured image\', \'textdomain\' ),
\'remove_featured_image\' => __( \'Remove featured image\', \'textdomain\' ),
\'use_featured_image\' => __( \'Use as featured image\', \'textdomain\' ),
\'insert_into_item\' => __( \'Insert into Send Notifiction\', \'textdomain\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this Send Notifiction\', \'textdomain\' ),
\'items_list\' => __( \'Send Notifiction list\', \'textdomain\' ),
\'items_list_navigation\' => __( \'Send Notifiction list navigation\', \'textdomain\' ),
\'filter_items_list\' => __( \'Filter Send Notifiction list\', \'textdomain\' ),
);
$args = array(
\'label\' => __( \'Send Notifiction\', \'textdomain\' ),
\'description\' => __( \'\', \'textdomain\' ),
\'labels\' => $labels,
\'menu_icon\' => \'dashicons-format-chat\',
\'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\', \'author\', \'comments\', \'trackbacks\', \'page-attributes\', \'post-formats\', \'custom-fields\'),
\'taxonomies\' => array(),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'hierarchical\' => false,
\'exclude_from_search\' => false,
\'show_in_rest\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
);
register_post_type( \'sendnotifiction\', $args );
}
add_action( \'init\', \'create_sendnotifiction_cpt\', 0 );
//Notification post
function cc_publish_wpse_263985( $postid ) {
// check if post status is \'publish\'
if ( get_post_status( $postid ) == \'publish\') {
$url = \'http://192.168.100.24:8181/notification/topic\';
//The data you want to send via POST
$data = array( \'title\' => \'Hello World\',
\'message\' => \'Hello\',
\'token\' => \'\',
\'topic\' => \'Active\');
$options = array(
\'http\' => array(
\'method\' => \'POST\',
\'content\' => json_encode( $data ),
\'header\'=> "Content-Type: application/json\\r\\n" .
"Accept: application/json\\r\\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
}
}
?>
SO网友:Hamza
问题已解决
更新的代码
<?php
/*
Plugin Name: Send Notification CPT
Description: Declares a plugin that will create a custom post type to send notifications.
Version: 1.0
*/
if( ! defined(\'ABSPATH\')) {
exit;
}
// Register Custom Post Type Send Notifiction
function create_sendnotifiction_cpt() {
$labels = array(
\'name\' => _x( \'Send Notifiction\', \'Post Type General Name\', \'textdomain\' ),
\'singular_name\' => _x( \'Send Notifiction\', \'Post Type Singular Name\', \'textdomain\' ),
\'menu_name\' => _x( \'Send Notifiction\', \'Admin Menu text\', \'textdomain\' ),
\'name_admin_bar\' => _x( \'Send Notifiction\', \'Add New on Toolbar\', \'textdomain\' ),
\'archives\' => __( \'Send Notifiction Archives\', \'textdomain\' ),
\'attributes\' => __( \'Send Notifiction Attributes\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent Send Notifiction:\', \'textdomain\' ),
\'all_items\' => __( \'All Send Notifiction\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Send Notifiction\', \'textdomain\' ),
\'add_new\' => __( \'Add New\', \'textdomain\' ),
\'new_item\' => __( \'New Send Notifiction\', \'textdomain\' ),
\'edit_item\' => __( \'Edit Send Notifiction\', \'textdomain\' ),
\'update_item\' => __( \'Update Send Notifiction\', \'textdomain\' ),
\'view_item\' => __( \'View Send Notifiction\', \'textdomain\' ),
\'view_items\' => __( \'View Send Notifiction\', \'textdomain\' ),
\'search_items\' => __( \'Search Send Notifiction\', \'textdomain\' ),
\'not_found\' => __( \'Not found\', \'textdomain\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'textdomain\' ),
\'featured_image\' => __( \'Featured Image\', \'textdomain\' ),
\'set_featured_image\' => __( \'Set featured image\', \'textdomain\' ),
\'remove_featured_image\' => __( \'Remove featured image\', \'textdomain\' ),
\'use_featured_image\' => __( \'Use as featured image\', \'textdomain\' ),
\'insert_into_item\' => __( \'Insert into Send Notifiction\', \'textdomain\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this Send Notifiction\', \'textdomain\' ),
\'items_list\' => __( \'Send Notifiction list\', \'textdomain\' ),
\'items_list_navigation\' => __( \'Send Notifiction list navigation\', \'textdomain\' ),
\'filter_items_list\' => __( \'Filter Send Notifiction list\', \'textdomain\' ),
);
$args = array(
\'label\' => __( \'Send Notifiction\', \'textdomain\' ),
\'description\' => __( \'\', \'textdomain\' ),
\'labels\' => $labels,
\'menu_icon\' => \'dashicons-format-chat\',
\'supports\' => array(\'title\', \'editor\', \'excerpt\', \'thumbnail\', \'revisions\', \'author\', \'comments\', \'trackbacks\', \'page-attributes\', \'post-formats\', \'custom-fields\'),
\'taxonomies\' => array(),
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'hierarchical\' => false,
\'exclude_from_search\' => false,
\'show_in_rest\' => true,
\'publicly_queryable\' => true,
\'capability_type\' => \'post\',
);
register_post_type( \'sendnotifiction\', $args );
}
add_action( \'init\', \'create_sendnotifiction_cpt\', 0 );
//Notification post
function cc_publish_wpse_263985( $postid ) {
// check if post status is \'publish\'
if ( get_post_status( $postid ) == \'publish\') {
$url = \'http://192.168.100.24:8181/notification/topic\';
//The data you want to send via POST
$data = array( \'title\' => \'Hello World\',
\'message\' => \'Hello\',
\'token\' => \'\',
\'topic\' => \'Active\');
$options = array(
\'http\' => array(
\'method\' => \'POST\',
\'content\' => json_encode( $data ),
\'header\'=> "Content-Type: application/json\\r\\n" .
"Accept: application/json\\r\\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );
}
}
add_action( \'save_post\', \'cc_publish_wpse_263985\' );a
?>