Upgrade to Pro — share decks privately, control downloads, hide ads and more …

REST API な WordPress テーマについて考える。

REST API な WordPress テーマについて考える。

Toro_Unit (Hiroshi Urabe)

September 03, 2017
Tweet

More Decks by Toro_Unit (Hiroshi Urabe)

Other Decks in Technology

Transcript

  1. Toro_Unit ઎෦ ߛ (͏Β΂ ͻΖ͠) ϑϦʔϥϯε Frontend Engineer / Web

    Designer Github: @torounit Twitter: @Toro_Unit Facebook: fb.me/torounit Blog: https://torounit.com 2
  2. 9

  3. 10

  4. 11

  5. 13

  6. • WordPressͷςʔϚͰ΋ϦονͳϢʔβʔମݧ • ϦονͳϖʔδભҠɻ • ϦΞϧλΠϜͳσʔλߋ৽ɻ • REST API Λ׆༻͢Δ͜ͱͰɺPWA౳΁ͷରԠ΋εϜʔζʹɻ

    • ΦϑϥΠϯϑΝʔετͷ࣮ݱɻ • طଘͷWordPress͔ΒͷҠߦɻ • ϓϨϏϡʔͰαΠτͷݟͨ໨Λ֬ೝɻ 16
  7. 17

  8. 23

  9. WordPress͔ΒJSʹ஋Λ౉͢ wp_localize_script Λ࢖͑͹ग़དྷΔɻʢຊདྷͷ࢖༻๏ͱ͸ͣ ΕΔؾ΋͢Δ͚Ͳʣ wp_localize_script( 'vuepress-script', 'WPSettings', [ 'root' =>

    esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'date_permastruct' => $wp_rewrite->get_date_permastruct(), 'page_on_front' => get_option('page_on_front'), 'page_for_posts' => get_option('page_for_posts'), 'category_base' => get_option('category_base') ? get_option('category_base') : 'category', 'tag_base' => get_option('tag_base') ? get_option('tag_base') : 'tag' ] ); 30
  10. add_action( 'rest_api_init', function () { register_rest_route( 'vuepress/v1', '/post/(?P<url>.*)', array( 'methods'

    => 'GET', 'callback' => function ( $request ) { $post_id = url_to_postid( $request['url'] ); if ( ! $post_id ) { return []; } $post = get_post( $post_id ); $post_type = get_post_type_object( $post->post_type ); $request['id'] = $post_id; $response = new WP_REST_Posts_Controller( $post->post_type ); return $response->get_item( $request ); } ) ); } ); 34
  11. "Clean URL"Ͱͳ͍URL΁ͷରԠ • /?s=hoge /search/hoge ͱ౳ՁͰ͋Δ͕ϦμΠϨΫτ͞Εͳ͍!!! • /?cat=1 ͸ϦμΠϨΫτ͞ΕΔ͕ɺ/?cat=1&cat=2 •

    /?p=123౳ͷɺ"Clean URL"Λ࢖༻͍ͯ͠ͳ͍έʔεɻ • ৽ن౤ߘͷϓϨϏϡʔ͸ɺ/?p=123&preview=true ܗࣜʹͳΔɻ • URL ͔Β /caregory/ Λফ͍ͨ͠ͱ͔͍͏ෆಧ͖ऀ΋ଟ͍ 38