Skip to content

Instantly share code, notes, and snippets.

@yoosuf
Last active March 27, 2016 11:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoosuf/991e4165c51ba0eb6a6e to your computer and use it in GitHub Desktop.
Save yoosuf/991e4165c51ba0eb6a6e to your computer and use it in GitHub Desktop.
Managing everything is WordPress is messy, so I wanted to send a POST request when a post is published
<?php
/**
* @package tands
* @version 0.1
*/
/*
Plugin Name: Trigger and Shoot by Yoosuf
Plugin URI: https://github.com/yoosuf
Description: This plugin simply pass the post details to where ever you want for what ever the porpose.
Author: Yoosuf Muhammad
Version: 0.1
Author URI: https://github.com/yoosuf
*/
function on_all_status_transitions( $new_status, $old_status, $post ) {
if ('publish' == $new_status && 'publish' != $old_status && 'trash' != $old_status) {
trigger_and_shoot($post);
}
}
add_action( 'transition_post_status', 'on_all_status_transitions', 10, 3 );
/**
* @param $post
*/
function trigger_and_shoot($post) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$category = get_the_category( $post );
$set_up = [
'X-API-Key: YOUR API KEY',
'Content-Type: application/x-www-form-urlencoded'
];
$post_data="id=".$post_id."&title=".$post_title."&content=".$category[0]->cat_name .": ".$post_title."&category_id=".$category[0]->term_id."&category=".$category[0]->cat_name;
$url="URL FOR THE SERVICE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $set_up);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment