ShifterでのURL追加

Shifterがサイト上のURLの追加をサポートするようになりました。以下のフィルターをテーマのfunctions.phpに適用することで実現できます。

add_filter( 'ShifterURLS::AppendURLtoAll', 'my_append_urls' );

制限事項

  • URLはWordPress内に含まれている必要があります(Shifterは外部URLを取得できません)。
  • URLはhome_url("")に埋め込まれている必要があります。
  • URLはスラッシュ(末尾のスラッシュ)で終わるか、許可されたサフィックスのリストに含まれている必要があります。
  • URLは/index.htmlで生成されます。
  • Content-Typeは、ShifterがWordPressから取得したものと同じになります(例:/wp-json/wp/v2/posts//wp-json/wp/v2/posts/index.htmlとなり、content-type: application/jsonとして配信されます)。

許可されるファイルサフィックス:
.html .xml .rss .rdf .atom .css .js .json

サンプル

サンプルコードA:
ジェネレーターのターゲットURLに/wp-json/wp/v2/posts/を追加する

function my_append_urls( $urls ) {
  $urls[] = home_url('/wp-json/wp/v2/posts/');
  return $urls;
}

add_action( 'init', function(){
  add_filter( 'ShifterURLS::AppendURLtoAll', 'my_append_urls' );
} );

結果A:
/wp-json/wp/v2/posts/が、"link_type": "from_filter_hook"および"path": "/wp-json/wp/v2/posts/"としてターゲットURLに追加されます。

$ curl -s https://127.0.0.1:8443/?urls | jq .
{
  "datetime": "2020-07-07 02:41:39 UTC",
  "page": null,
  "start": 0,
  "end": 100,
  "limit": 100,
  "items": [
    {
      "link_type": "home",
      "post_type": "",
      "link": "https://127.0.0.1:8443/",
      "path": "/"
    },
    [... cropped ...]
    {
      "link_type": "from_filter_hook",
      "post_type": "",
      "link": "https://127.0.0.1:8443/wp-json/wp/v2/posts/",
      "path": "/wp-json/wp/v2/posts/"
    }
  ],
  "request_type": "TOP",
  "request_path": "/",
  "count": 20,
  "finished": true
}

サンプルコードB:
米国の州の統計リストを/states/($slag)/としてターゲットURLに追加する。

function my_append_urls( $urls ) {
  $states = [
    'Alabama',
    'Arizona',
    [...cropped....]
    'Wisconsin',
    'Wyoming',
  ];
  foreach ( $states as $slag ) {
    $urls[] = home_url("/states/{$slag}/");
  }
  return $urls;
}

add_action( 'init', function(){
  add_filter( 'ShifterURLS::AppendURLtoAll', 'my_append_urls' );
} );

サンプル結果B:

$ curl -s https://127.0.0.1:8443/wp-json/shifter/v1/urls?page=1 | jq .
{
  "datetime": "2020-09-04 06:01:58 UTC",
  "page": 1,
  "start": 100,
  "end": 200,
  "limit": 100,
  "items": [
    {
      "link_type": "term_feed",
      "post_type": "column",
      "link": "https://127.0.0.1:8443/column/feed/",
      "path": "/column/feed/"
    },
    :
    {
      "link_type": "from_filter_hook",
      "post_type": "",
      "link": "https://127.0.0.1:8443/states/Alabama/",
      "path": "/states/Alabama/"
    },
    {
      "link_type": "from_filter_hook",
      "post_type": "",
      "link": "https://127.0.0.1:8443/states/Arizona/",
      "path": "/states/Arizona/"
    },
    [..... cropped .......]
    {
      "link_type": "from_filter_hook",
      "post_type": "",
      "link": "https://127.0.0.1:8443/states/Wisconsin/",
      "path": "/states/Wisconsin/"
    },
    {
      "link_type": "from_filter_hook",
      "post_type": "",
      "link": "https://127.0.0.1:8443/states/Wyoming/",
      "path": "/states/Wyoming/"
    }
  ],
  "request_type": "TOP",
  "request_path": "/",
  "count": 91,
  "finished": true
}