JetPackのFacebookへの自動共有で全文が掲載されてしまうのを防いでハッシュタグも付ける方法

作成日:

JetPackプラグインのパブリサイズ共有機能で、ブログ投稿時に自動的にFacebookのページに共有するように設定しているのですが、標準ですと以下のように本文がずらーーーっと掲載されてしまいます。

favicon
Publicize https://jetpack.com/support/publicize/

Publicize makes it easy to share your site’s posts on several social media networks automatically when you publish a new post.

そこで、対策法を探ってみましたらこちらの記事がヒット。

favicon
WordPressのFacebook自動投稿で全文表示されるのを防ぐ方法【Jetpack共有】 https://www.ec-create.jp/wp-jetpack-share-facebook-all-text/

このブログでは、WordPressで作っています。そして、WordPressで記事を投稿すると以下のSNSに自動で投稿されるように設定をしています。 WordPressプラグインの「Jetpack」で《共有》設定をしてい […]

要は毎回毎回カスタムメッセージフォームにタイトルなどを入力しろとのこと。

これはこれで面倒でさらに探ってみますと、英語の記事でこちらがヒット。

favicon
Jetpack: add hashtags to tweets sent by Publicize | Jeremy Herve https://jeremy.hu/jetpack-hashtags-tweets-publicize/

Here is how to add hashtags to the message posted by Publicize to Twitter and Facebook

こちらの方法では、記事の保存・公開時、カスタムメッセージに「タイトル」と記事に設定しているタグを「ハッシュタグ」に変更してくれるという方法です。

以下のコードをテーマのfunctions.phpにコピペすれば利用出来ます。[php]function jeherve_publicize_hashtags() {
$post = get_post();
if ( ! empty( $post ) ) {

// Grab the tags of the post
$post_tags = get_the_tags( $post->ID );

// Append tags to custom message
if ( ! empty( $post_tags ) ) {

// Create list of tags with hashtags in front of them
$hash_tags = '';
foreach( $post_tags as $tag ) {
$hash_tags .= ' #' . $tag->name;
}

// Create our custom message
$custom_message = get_the_title() . ' ' . $hash_tags;
update_post_meta( $post->ID, '_wpas_mess', $custom_message );
}
}
}

// Save that message
function jeherve_cust_pub_message_save() {
add_action( 'save_post', 'jeherve_publicize_hashtags' );
}
add_action( 'publish_post', 'jeherve_cust_pub_message_save' );[/php]

ただ、理由は定かではありませんが、子テーマのfunctions.phpでは機能しなかったので、仕方がなく親テーマで設定しました。

物草 灸太郎
物草 灸太郎

WordPressでホームページを制作しつつ、休日は畑を耕したりDIYを楽しんでいます。

Loading...

コメントをどうぞ

  • メールアドレスが公開されることはありません。
  • コメント欄にURLは入力できません。
  • このサイトはreCAPTCHAによって保護されており、Googleのプライバシーポリシー利用規約が適用されます。