Action on Custom Post publish

时间:2020-09-14 作者: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 );

}
}

?>


1 个回复
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
?>

相关推荐

如何使用PHP函数触发WP CLI DB导出?

我想使用WP-CLI导出数据库-使用wp db export - 创建备份。我还需要使用cron来控制备份计划。WP-CLI安装在我的托管主机上,我可以通过SSH, 但我不能用cron 直接在我的服务器上。为了使用我自己的cron作业来安排备份,我需要使用插件WP Crontrol来添加和编辑cron 工作;该插件使用函数作为add_action, i、 e。my_backup_cron 开火my_backup_function:这是插件使用的格式:add_action( \'my_backup_cron