Last active
June 5, 2017 20:14
-
-
Save alkah3st/dd00163adc6dd2a6b4f0369a8f10f065 to your computer and use it in GitHub Desktop.
Send to Buffer on Publish/Scheduled Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Buffer App Library | |
*/ | |
session_start(); | |
require_once(get_template_directory().'/includes/libraries/bufferapp/bufferapp.php'); | |
/** | |
* Post to Buffer on Publish/Scheduled Publish | |
*/ | |
function buffer_schedule($post_id) { | |
// if draft_to_publish calls this | |
if (is_object ($post_id)) { | |
// we get an object | |
$object = $post_id; | |
$id = $object->ID; | |
// if publish_future_post calls this | |
} else { | |
// we get an id | |
$object = get_post($post_id); | |
$id = $post_id; | |
} | |
// check post type | |
$post_type = get_post_type($id); | |
// only do this for posts | |
if ( 'post' == $post_type) { | |
// okay proceed | |
} else { | |
// otherwise do nothing | |
return; | |
} | |
// now check if buffer is permitted; if true, we skip this post | |
// (this is the ACF field "buffer_lock_post" we created earlier) | |
if (get_field('buffer_lock_post', $id)) return | |
session_start(); | |
// now let's construct an excerpt... | |
// if we have a manual buffer_desc in our ACF field for this post, use that | |
if (get_field('buffer_desc', $id)) { | |
$post_excerpt = get_field('buffer_desc', $id); | |
} else { | |
// otherwise, we should use the_excerpt from the post | |
$post_excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $id)); | |
// if there is no excerpt, we use the post title | |
if (!$post_excerpt) $post_excerpt = $post_title; | |
} | |
// get the featured thumbnail | |
$thumbnail_id = get_post_thumbnail_id($id); | |
if ($thumbnail_id) { | |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id, 'post-thumbnail' ); | |
} else { | |
$thumbnail = false; | |
} | |
// construct media array for buffer | |
$media = array( | |
'link'=> get_permalink($id), | |
'description'=>$post_excerpt, | |
'title'=>$post_excerpt, | |
'picture'=>$thumbnail, | |
'photo'=>$thumbnail, | |
'thumbnail'=>$thumbnail | |
); | |
// by default, we'll attach categories as our hash tags | |
// first, we check if there are any manually-entered hash tags | |
// (this is the hash_tags ACF field we created earlier) | |
if (get_field('hash_tags', $id)) { | |
$term_list = get_field('hash_tags', $id); | |
} else { | |
// otherwise, let's get category terms | |
// this will construct a list of hashtags like #term #term #term | |
// using the slug of each term | |
$term_list = ''; | |
$terms = get_the_terms($id, 'category'); | |
$count = count($terms); | |
if ( $count > 0 ) { | |
foreach ( $terms as $i=>$term ) { | |
$hash_name = $term->slug; | |
if ($i < $count-1) { | |
$term_list .= '#'.$hash_name . " "; | |
} else { | |
$term_list .= '#'.$hash_name; | |
} | |
} | |
} | |
} | |
// construct short link to inject into our sharing copy | |
// we'll use this as the link to share with buffer | |
$url = get_site_url().'/?p='.$id; | |
// construct text to be shared | |
$text = $post_excerpt . ' - ' .$url; | |
// start a new buffer instance | |
$buffer = new \BufferApp(BUFFER_CLIENT_ID, BUFFER_CLIENT_SECRET, null); | |
// specify which profiles to queue to | |
// you can find these IDs by looking at the URL when you one of your profiles | |
// https://buffer.com/app/profile/YOUR_PROFILE_ID/buffer/queue/list | |
$profiles = array( | |
'PUT YOUR LINKEDIN BUFFER PROFILE ID HERE', // linkedin | |
'PUT YOUR GOOGLE PLUS PROFILE ID HERE', // gplus | |
'PUT YOUR FACEBOOK PROFILE ID HERE', // fb | |
'PUT YOUR TWITTER PROFILE ID HERE', // twitter | |
); | |
foreach($profiles as $profile){ | |
// only certain profiles support hashtags, so let's only append our $term_list to those profiles | |
if ($profile == 'PUT YOUR TWITTER PROFILE ID HERE' || $profile == 'PUT YOUR FACEBOOK PROFILE ID HERE') { | |
$text = $post_excerpt . ' - '. $url . ' '.$term_list; | |
} else { | |
$text = $text; | |
} | |
$buffer->go('/updates/create', array( | |
'text' => strip_tags($text), | |
'media[title]'=> strip_tags(get_the_title($id)), | |
'media[link]'=> get_permalink($id), | |
'media[description]'=> strip_tags($post_excerpt), | |
'media[picture]'=> $thumbnail, | |
'media[thumbnail]'=> $thumbnail, | |
'profile_ids[]' => $profile | |
)); | |
} | |
// then, we need to lock this post from being posted to buffer in the future | |
// you'll need to get the field ID of your buffer_lock_post field, and enter it here | |
update_field('ENTER FIELD ID FOR BUFFER_LOCK_POST', 1, $id); | |
} | |
add_action('publish_future_post', 'buffer_schedule'); | |
add_action('draft_to_publish', 'buffer_schedule', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment