Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bjonesy/cd05daf752f483f2b5ff to your computer and use it in GitHub Desktop.
Save bjonesy/cd05daf752f483f2b5ff to your computer and use it in GitHub Desktop.
Customizing Publicize Sharing Behavior
<?php
/**
* Include WP Codebird
*
* Either in functions.php file or however you prefer to include it
*/
/* If on WordPress VIP */
// Load VIP Plugins/Lib
wpcom_vip_require_lib( 'codebird' );
// Other - include folder within theme
require_once get_template_directory() . '/folder-you-put-it-in/wp-codebird-master/class-wp-codebird.php';
/**
* Change default message that is sent to Twitter - ('publicize_twitter_message')
* http://vip.wordpress.com/documentation/customizing-publicize-sharing-behavior/#altering-the-default-messages
*
* This function will not work without https://github.com/automattic/wp-codebird
*/
add_filter( 'publicize_twitter_message', 'theme_slug_add_twitter_image', 10, 3 );
function theme_slug_add_twitter_image( $message, $post, $url ) {
/*
* Must create a Twitter app and then fill out these fields in order for this function to work
* https://apps.twitter.com/
*/
//Twitter OAuth Settings
$CONSUMER_KEY = '';
$CONSUMER_SECRET = '';
$ACCESS_TOKEN = '';
$ACCESS_TOKEN_SECRET = '';
//Get authenticated
WP_Codebird::setConsumerKey($CONSUMER_KEY, $CONSUMER_SECRET);
$cb = WP_Codebird::getInstance();
$cb->setToken($ACCESS_TOKEN, $ACCESS_TOKEN_SECRET);
/*
* In this example the featured image is media attached to the tweet
* Followed by the title of the post and shortlink
*
* This can be customized any way you want to engage your followers
*/
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); // Get the featured image
$image_path = $image_attributes[0]; // Return featured image url
$title = html_entity_decode( get_the_title( $post->ID ) ); // The title of the post
$seperator = str_repeat(' ', 1); // Create space between the title and url
// This is optional - I suggest using it if you have it
$url = esc_url( wp_get_shortlink( $post->ID ) ); // Returns the Short Link to a post
$status = $title.$seperator.$url; // This is the custom status we want Twitter to Tweet
/** The Twitter REST API
* POST statuses/update_with_media
* https://dev.twitter.com/docs/api/1/post/statuses/update_with_media
*/
$params = array(
'status' => $status,
'media[]' => $image_path,
);
$message = $cb->statuses_updateWithMedia( $params );
// Don't keep retweeting
return $message;
}
@silu44
Copy link

silu44 commented Apr 10, 2021

how to customize publicize to share on facebook like( post title + link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment