LoginSignup
9
7

More than 5 years have passed since last update.

WordPressのREST APIにAll in One SEOの項目を追加する方法

Last updated at Posted at 2019-01-23

WordPressのREST APIは便利ですが、色々カスタマイズしないと使いにくいのも事実。
みなさんがよく使うAll In One SEOの項目が入ってないのでこれを項目に追加して取得可能にする方法を以下に示します。
いつものWordPressのfunction.phpに以下を足すだけです。ちなみに追加するフックはこちらを参照ください。
http://hookr.io/plugins/all-in-one-seo-pack/2.3.9.2/hooks/#index=a

add_action( 'rest_api_init', 'adding_post_meta_rest' );
function adding_post_meta_rest() {
    register_rest_field( 'post',
                        '_aioseop_title',
                        array(
                            'get_callback'      => 'post_meta_callback',
                            'update_callback'   => null,
                            'schema'            => null,
                        )
                       );
    register_rest_field( 'post',
                        '_aioseop_description',
                        array(
                            'get_callback'      => 'post_meta_callback',
                            'update_callback'   => null,
                            'schema'            => null,
                        )
                       );
    register_rest_field( 'post',
                        '_aioseop_keywords',
                        array(
                            'get_callback'      => 'post_meta_callback',
                            'update_callback'   => null,
                            'schema'            => null,
                        )
                       );
}
function post_meta_callback( $post, $field_name, $request) {
    return get_post_meta( $post[ 'id' ], $field_name, true );
}

WordPressAPIは以下のようになっています。
(your website)/wp-json/wp/v2/posts?_embed
こうして呼ぶことで情報を取得できます。

rest.png

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7