WP PluginのTweetableでツイートする内容に付加情報をつけるように細工

店のサイトで記事を投稿すると、WordpressのPluginであるTweetableを利用してTwitterにも更新情報を流すようにしている。

通常、Tweetableは投稿記事のタイトルとそのリンクURLをツイートするのだが、これではツイッターをフォローしていただいている人のみ目にすることになる。

これでは商業ベースのツイッターとしては物足りない。そこで、ハッシュタグを自動で付加しようと思い検索したところ、「Leegal memo」の【PHP】WordPressのTweetableのカスタムが目にとまった。

そのブログを参考にtweetable.phpの250行からの関数を以下のように改造した。

  • コメントを付加したところがちょこっと改造したところ
  • 接頭にカテゴリーを付加
  • 接尾にハッシュタグ #hiroshima #bowlingを付加(固定ハッシュタグ)

 

/*** Send tweet on post publish ***/
function tweetable_publish_tweet($post_id) {

	$installed = get_option('tweetable_account_activated');

	if (get_option('tweetable_auto_tweet_posts') == '1' && $installed) {

		$post = get_post($post_id);

		if ($post->post_status == 'private' || $post->post_type == 'page') {
			return;
		}

		if ($post->post_date < $post->post_modified) {
			return;
		}

		$user_key = get_option('tweetable_access_token');
		$user_key_secret = get_option('tweetable_access_token_secret');
		$twitter = new Twitter_API(get_option('tweetable_app_key'), get_option('tweetable_app_key_secret'));
		$shortener = get_option('tweetable_url_shortener');
		$shortener_login = get_option('tweetable_shortener_login');
		$shortener_apikey = get_option('tweetable_shortener_apikey');
		$tweet_prefix = get_option('tweetable_auto_tweet_prefix');
		$googletags = get_option('tweetable_google_campaign_tags');
		foreach((get_the_category()) as $cat);								// 追加分

		$permalink = get_permalink($post_id);
		if ($googletags == '1') {
			$tags = '?utm_source=twitter&utm_medium=social&utm_campaign='.urlencode($post->post_title);
			$permalink = urlencode($permalink . $tags);
		}

		$permalink = apply_filters('tweetable_autotweet_permalink', $permalink);
		$permalink = $twitter->shorten_url($permalink, $shortener, $shortener_apikey, $shortener_login);
		$title = '('.$cat->cat_name.')'.$tweet_prefix.' '.$post->post_title;				// $cat->cat_nameを追加
		$title = apply_filters('tweetable_autotweet_title', $title);

		$tweet = $twitter->fit_tweet($title, $permalink).' #hiroshima #bowling';			// 連結文字以後は改造分;
		$tweet = apply_filters('tweetable_autotweet_tweet', $tweet);

		$update = $twitter->update_status($tweet, $user_key, $user_key_secret);

	}

}
うまい具合に投稿ができました。
カテゴリーweb