Skip to content

Instantly share code, notes, and snippets.

@simofacc
Last active February 6, 2016 16:23
Show Gist options
  • Save simofacc/2e587b870ad8eb58b409 to your computer and use it in GitHub Desktop.
Save simofacc/2e587b870ad8eb58b409 to your computer and use it in GitHub Desktop.
Wordpress - Send post data to 3rd party service via curl
<?php
/**
* @package Post-to-Curl
* @version 1.0
*/
/*
Plugin Name: Post-to-Curl
Plugin URI: http://www.simonfacciol.info
Description: Send post data / status to 3rd party service via curl
Author: Simon Facciol
Version: 1.0
Author URI: http://www.simonfacciol.info
*/
function post_published_notification(
$post_id,
$post_data
) {
/*Check if post status changed to publish.
*
* This avoids multiple firing of the publish event
*/
if (($_POST['post_status'] == 'publish') && ($_POST['original_post_status'] != 'publish')) {
//Check if it's a post revision to prevent unnecessary firing
if (!wp_is_post_revision($post_id)) {
//$post_language_information = wpml_get_language_information($post_id); //Enable if WPML plugin is installed to get post language information
$author_name = get_the_author_meta('display_name', 6); //Get the display name of the author. Can be modified to get other author details
$post_categories = wp_get_post_categories($post_id); //Get the post categories as an array
$cats = array();
//Looping through all post categories to get name and slug only. Can be changed to get more category information
foreach ($post_categories as $c) {
$cat = get_category($c);
$cats[] = array('name' => $cat->name, 'slug' => $cat->slug);
}
$post_tags = wp_get_post_tags($post_id); //Get the post tags as an array
$tags = array();
//Looping through all post tags to get tag name and slug only. Can be changed to get more information
foreach ($post_tags as $t) {
$tags[] = array('name' => $t->name, 'slug' => $t->slug);
}
/*Get Yoast Meta information. Needs Yoast SEO wordpress plugin.
*$meta = [];
*$meta['keywords'] = get_post_meta($post_id, '_yoast_wpseo_focuskw', 1);
*$meta['title'] = get_post_meta($post_id, '_yoast_wpseo_title', 1);
*$meta['description'] = get_post_meta($post_id, '_yoast_wpseo_metadesc', 1);
*/
$body = array(
'action' => 'published',
'post_id' => $post_id,
'post' => $post_data,
//'post_language' => $post_language_information,
'post_author' => $author_name,
'post_category' => $cats,
'post_tags' => $tags,
//'post_meta' => $meta
);
$result = curl_my_post($body);
}
}
}
add_action('publish_post', 'post_published_notification', 10, 2);
function post_updated_notification(
$post_id,
$post_after,
$post_before
) {
//$post_language_information = wpml_get_language_information($post_id); //Enable if WPML plugin is installed to get post language information
$author_name = get_the_author_meta('display_name', 6); //Get the display name of the author. Can be modified to get other author details
$post_categories = wp_get_post_categories($post_id); //Get the post categories as an array
$cats = array();
//Looping through all post categories to get name and slug only. Can be changed to get more category information
foreach ($post_categories as $c) {
$cat = get_category($c);
$cats[] = array('name' => $cat->name, 'slug' => $cat->slug);
}
$post_tags = wp_get_post_tags($post_id); //Get the post tags as an array
$tags = array();
//Looping through all post tags to get tag name and slug only. Can be changed to get more information
foreach ($post_tags as $t) {
$tags[] = array('name' => $t->name, 'slug' => $t->slug);
}
/*Get Yoast Meta information. Needs Yoast SEO wordpress plugin.
*$meta = [];
*$meta['keywords'] = get_post_meta($post_id, '_yoast_wpseo_focuskw', 1);
*$meta['title'] = get_post_meta($post_id, '_yoast_wpseo_title', 1);
*$meta['description'] = get_post_meta($post_id, '_yoast_wpseo_metadesc', 1);
*/
$body = array(
'action' => 'updated',
'post_id' => $post_id,
'post' => $post_after,
//'post_language' => $post_language_information,
'post_author' => $author_name,
'post_category' => $cats,
'post_tags' => $tags,
//'post_meta' => $meta
);
$result = curl_my_post($body);
}
add_action('post_updated', 'post_updated_notification', 10, 3);
function trash_post_notification($post_id)
{
if (!did_action('trash_post')) {
$body = array(
'action' => 'delete',
'id' => $post_id
);
$result = curl_my_post($body);
}
}
add_action('trash_post', 'trash_post_notification', 10, 1);
function untrash_post_notification($post_id)
{
if (!did_action('untrash_post')) {
$body = array(
'action' => 'restore',
'id' => $post_id
);
$result = curl_my_post($body);
}
}
add_action('untrash_post', 'untrash_post_notification', 10, 1);
function curl_my_post($body)
{
$url = 'URL_TO_POST_TO';
//$url = 'http://requestb.in'; //Replace with valid requestbin URL for testing
$result = '';
try {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
} catch (Exception $e) {
error_log($e->getMessage());
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment