2021-09-02/2023-05-23

All in One SEO Packのタイトル変更

All in One SEO Packのタイトル変更

プラグインの「All in One SEO Pack」で出力されるタイトルを変更する方法を紹介します。

  • 「All in One SEO Pack」は便利だし使っていきたいけど、タイトル思い通りにならない
  • 特定のページだけ変更したい

そんな人は今回の紹介する方法で変更してみてください。

All in One SEO Pack タイトル変更方法

All in One SEO Packのタイトルまわりのフックは以下の4つです。

  • aioseo_title
  • aioseo_disable_title_rewrites
  • aioseo_twitter_tags
  • aioseo_facebook_tags

それぞれ見てきましょう。

aioseo_title

タイトルタグ(<title></title>)の変更用フックです。

add_filter( 'aioseo_title', function( $title ) {
if ( is_single() ) {
$title = '投稿ページのタイトル変更 | サンプル';
}
return $title;
} );

引数にはタイトルが入っています。

サイト名など表示したい場合は含めて変更してください。

aioseo_disable_title_rewrites

タイトルタグを変更するか選択できるフックです。

add_filter( 'aioseo_disable_title_rewrites', function( $disabled ) {
if ( is_tax() ) {
return true;
}
return false;
} );

引数は真偽値(boolean)でデフォルトはfalseです。

上の例ではカスタムタクソノミーのアーカイブページでタイトルは変更しないようにする例です。

aioseo_twitter_tags

ツイッタータグの変更用フックです。

add_filter( 'aioseo_twitter_tags', function( $attributes ) {
// $attributes = [
// 'twitter:card' => '',
// 'twitter:site' => '',
// 'twitter:domain' => '',
// 'twitter:title' => '',
// 'twitter:description' => '',
// 'twitter:image' => ''
// ];
if ( is_page() ) {
$attributes['twitter:title'] = '固定ページのツイッタータイトル変更';
}
return $attributes;
} );

引数はツイッタータグの配列です。

タイトルを変更したい場合は配列のキー「twitter:title」の値を変更してください。

aioseo_facebook_tags

facebookタグ(ogp)の変更用フックです。

add_filter( 'aioseo_facebook_tags', function( $attributes ) {
// $attributes = [
// 'og:site_name' => '',
// 'og:type' => '',
// 'og:title' => '',
// 'og:description' => '',
// 'og:url' => '',
// 'fb:app_id' => '',
// 'fb_admins' => '',
// 'og:image' => '',
// ];
if ( is_category() ) {
$attributes['og:title'] = 'カテゴリ―OGPタイトル変更';
}
return $attributes;
} );

引数はogpの配列です。

タイトルを変更したい場合は配列のキー「og:title」の値を変更してください。

まとめ

そんなに難しくないので簡単に変更出来ると思います。

その他のフックも気になる方はAll in One SEO Packの公式ページから確認してください。

2020 KumaTechLab.