現在業務毎に運用しているサイトの数があり、その中でも事務所情報など同一内容の固定ページが結構あるのですが、今まではそれを1か所修正しただけで、すべてのサイトにコピペしまくっていました。
その単純作業をなんとか1度の編集ですませられないかと、色々模索していましたが・・・
思い切って、WordPressフォーラムで質問してみたところ、親切で詳しいKUCKLUさんにJSON REST API プラグインを使う方法を
教えていただきました。もう本当に感謝しかありません!!
phpについては、私も実はチンプンカンプンなので、最初は教えて下さっている事を一体どうすればいいのかで
頭を悩ませてしまいましたが、普通の説明ではラチがあかないと思われたのか、コードまで作っていただけて、本当にありがたい!!
理解しようにも、よくわからなかったので、教えていただいた事をサクッとコピペしてしまったのですが、
思っていた通りの事が完璧に実現できて本当にHappyです。(笑)
そんな自分の為の覚書と、また私と似たような事を探している方のご参考になればと、以下を記録しておきます。
まずはJSON REST API プラグインをインストールして下さい。
固定ページ一覧を取得する場合は下記のような URL で取得できる。
1
|
http://example.com/wp-json/posts?type=page
|
特定のページを取得する場合は下記のような URL で取得できる。
1
2
|
// Post ID 1234 の場合
http://example.com/wp-json/posts/1234
|
その他、Filter を追加する事で並べ変えも可能。
特定のページの情報を取得して抜粋表示するショートコード。
使用中のテーマの functions.php に記述。
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
31
32
|
function my_get_json_remote_content_shortcode( $atts ) {
extract( shortcode_atts( array(
‘url’ => ”,
), $atts ) );
if ( !isset( $url ) || empty( $url ) || !wp_http_validate_url( $url ) )
return;
$parse_url = parse_url( $url );
$key = ‘json_rest_api_’ . str_replace( array( ‘.’, ‘-‘ ), ‘_’, $parse_url[‘host’] ) . str_replace( array( ‘.’, ‘-‘, ‘/’ ), ‘_’, $parse_url[‘path’] );
$cache = get_transient( $key );
if ( !$cache ) {
$response = wp_remote_get( $url );
if ( !is_wp_error( $response ) && $response[‘response’][‘code’] === 200 ) {
$response = json_decode( $response[‘body’] );
$cache[‘title’] = apply_filters( ‘the_title’, $response–>title );
$cache[‘excerpt’] = apply_filters( ‘the_excerpt’, $response–>excerpt );
$cache[‘link’] = $response–>link;
set_transient( $key, $cache, 12 * HOUR_IN_SECONDS );
} else {
return ‘<p>Error</p>’;
}
}
$result = sprintf( ‘<h2><a href=”%s”>%s</a></h2><p>%s</p>’, esc_url( $cache[‘link’] ), $cache[‘title’], $cache[‘excerpt’] );
return $result;
}
add_shortcode( ‘get_remote_content’, ‘my_get_json_remote_content_shortcode’ );
|
このショートコードを固定ページに貼りつけると、http://example.comのPost ID 1234が抜粋表示される。
1
|
[get_remote_content url=“http://example.com/wp-json/posts/1234” /]
|
以下は、抜粋ではなく、全表示させるもの。
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
31
32
33
34
35
36
37
38
|
function my_get_json_remote_content_shortcode( $atts ) {
extract( shortcode_atts( array(
‘url’ => ”,
), $atts ) );
if ( !isset( $url ) || empty( $url ) || !wp_http_validate_url( $url ) )
return;
$parse_url = parse_url( $url );
$key = ‘json_rest_api_’ . str_replace( array( ‘.’, ‘-‘ ), ‘_’, $parse_url[‘host’] ) . str_replace( array( ‘.’, ‘-‘, ‘/’ ), ‘_’, $parse_url[‘path’] );
$cache = get_transient( $key );
if ( defined( ‘WP_DEBUG’ ) && WP_DEBUG ) {
delete_transient( $key );
}
if ( !$cache ) {
$response = wp_remote_get( $url );
if ( !is_wp_error( $response ) && $response[‘response’][‘code’] === 200 ) {
$response = json_decode( $response[‘body’] );
$cache[‘title’] = apply_filters( ‘the_title’, $response–>title );
$cache[‘excerpt’] = apply_filters( ‘the_excerpt’, $response–>excerpt );
$cache[‘content’] = apply_filters( ‘the_content’, $response–>content );
$cache[‘link’] = $response–>link;
set_transient( $key, $cache, 12 * HOUR_IN_SECONDS );
} else {
return ‘<p>Error</p>’;
}
}
$result = sprintf( ‘<h2><a href=”%s”>%s</a></h2><p>%s</p>’, esc_url( $cache[‘link’] ), $cache[‘title’], $cache[‘content’] );
return $result;
}
add_shortcode( ‘get_remote_content’, ‘my_get_json_remote_content_shortcode’ );
|