Trigger code when a custom post is published

Question

I want to send a notification when a custom post is published, by using this code I am able to send

function cc_publish_wpse_263985( $postid ) {

    // check if post status is 'publish'
    if ( get_post_status( $postid ) == 'publish')   {

$url = 'https://url';

//The data you want to send via POST
$data = array( 'title' => '' .get_the_title(). '', 
     'message' => '' .get_the_excerpt() . '',
     'token' => '',
    'topic' => 'Active');

$options = array(
  'http' => array(
    'method'  => 'POST',
    'content' => json_encode( $data ),
    'header'=>  "Content-Type: application/jsonrn" .
                "Accept: application/jsonrn"
    )
);

$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );


}

}
add_action( 'save_post', 'cc_publish_wpse_263985' );

I have 4 custom post in my site and want to send a notification from only one post, but right now by using this when I publish a post from any of them it will send a push notification, I want to send only from the post type

0
Hamza 4 months 0 Answers 17 views 0

Leave an answer