如何将多合一SEO项目添加到WordPress REST API


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 );
}

WordPress API如下。
(您的网站)/ wp-json / wp / v2 /帖子?_嵌入
可以通过这种方式调用来获取信息。

rest.png