Help us understand the problem. What is going on with this article?

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away