はじめてのプラグインのつくりかた

 はじめてのプラグインのつくりかた

WordBench 長野 vol.14 WordPress 勉強会 の登壇資料です。

E03953e0c18d776fead147601fdc3899?s=128

Toro_Unit (Hiroshi Urabe)

December 02, 2017
Tweet

Transcript

  1. ͸͡ΊͯͷϓϥάΠϯͷͭ͘Γ͔ͨ Toro_Unit @2017.12.02 / WordBench Nagano vol 14. 1

  2. $ whoami ͜͡͠ΐ͏͔͍ 2

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

    Designer Plugin and Theme Developer Github: @torounit Twitter: @Toro_Unit Facebook: fb.me/torounit Blog: https://torounit.com 3
  4. Plugins. • Custom Post Type Permalinks • Active installations: 90,000+

    • Downloads: 450,000 + • Simple Post Type Permalinks • Powerful Posts Per Page (PPPP) • Responsive Slide • and more... 4
  5. Vanilla — Free WordPress Themes 5

  6. Contribution • WordBench ௕໺ ϞσϨʔλʔ • WordCamp Kyoto 2017 /

    Kansai 2016 / Tokyo 2016 • WordCamp Tokyo 2017 Speaker • WordBench.org • VCCW Team • etc... 6
  7. WordPress Plugins • WordPress Λ֦ு͢Δػೳɻ • ެࣜϨϙδτϦʹ͸ɺ50000 Ҏ্ͷ ϓϥάΠϯ͕ܝࡌ͞Ε͍ͯΔɻ(؅ཧ ը໘͔ΒΠϯετʔϧग़དྷΔ΍ͭ)

    7
  8. ΋ͪΖΜࣗ࡞Ͱ͖Δɻ 8

  9. ࣗ࡞͢ΔϝϦοτ • ϓϥάΠϯʹͨ͠ํ͕ɺ࢖͍ճ͕͠༰қɻ • ςʔϚͷfunctions.php ʹॻ͘ͱͪ͝Όͪ͝Ό͢Δ͠ɺςʔϚ Λม͑Δͱͦͷػೳ͕ࢮ͵ɻࡶͳ࡞ΓํΛͯ͠ΔͱαΠτ͕ յΕΔɻ 9

  10. ϓϥάΠϯͷͭ͘Γ͔ͨ 10

  11. 1. PHP ϑΝΠϧΛ࡞੒ͯ͠ɺwp-content/plugins ʹ഑ஔɻ • plugins ௚Լʹ php ϑΝΠϧΛஔ͍ͯ΋OK͕ͩɺϓϥάΠϯͷϑΝΠϧ͕૿͑ͨΓ͢ΔͷͰɺσΟϨΫτϦΛ࡞ͬͯͦͷதʹ ೖΕͨ΄͏͕Θ͔Γ΍͍͢ɻ

    11
  12. 2.PHPϑΝΠϧͷઌ಄ʹίϝϯτΛهड़ <?php /* Plugin Name: ௒͍͢͝ϓϥάΠϯ Plugin URI: https://example.org Description:

    ͍ͧ͢͝ʔ Author: Toro_Unit Version: 1.0.0 Author URI: https://torounit.com/ License: GPL2 or Later. */ ࠷௿ݶ Plugin Name ͕͋Ε͹OK. 12
  13. 13

  14. Ͱ͖ͨɻ 14

  15. ͓ΘΓ 15

  16. ͱ͍͏Θ͚ʹ΋͍͔ͳ͍ͷͰ 16

  17. ϓϥάΠϯΛ࢖ͬͯɺWordPress ΛΧ ελϚΠζ͢Δɻ 17

  18. Action Hooks Filter Hooks 18

  19. ΞΫγϣϯϑοΫɾϑΟϧλʔϑοΫ • WordPress ͷಈ࡞Λมߋ͢ΔͨΊͷAPI • ΞΫγϣϯϑοΫ͸ɺಈ࡞Λ௥Ճ͢Δɻ(දࣔͨ͠ΓɺDBʹ อଘͨ͠Γ) • ϑΟϧλʔϑοΫ͸ɺ஋Λมߋ͢Δɻ 19

  20. ϑΟϧλʔϑοΫͷྫ function sugoi_titie( $title ) { return '͍ͧ͢͝ʔ:' . $title;

    } add_filter( 'the_title', 'sugoi_titie' ); 20
  21. 21

  22. ΞΫγϣϯϑοΫͷྫ function sptp_admin_notices() { $message = '͍ͧ͢͝ʔɻ'; printf( '<div class="notice"><p>%s</p></div>',

    $message ); } add_action( 'admin_notices', 'sptp_admin_notices'); 22
  23. 23

  24. ϑοΫͷҰཡ 2700 ݸ͘Β͍͋Δɻ • wpdocs.osdn.jp/ϓϥάΠϯ_API/ΞΫγϣϯϑοΫҰཡ • wpdocs.osdn.jp/ϓϥάΠϯ_API/ϑΟϧλʔϑοΫҰཡ • developer.wordpress.org/reference/hooks/ 24

  25. Shortcode API 25

  26. γϣʔτίʔυͱ͸ɻ [gallery]ɹ౳ɺͷ[]ͰғΘΕͨϞϊɻίϨΛهࣄ಺ʹهड़͢Δͱ ͦͷγϣʔτίʔυʹม׵͞ΕΔɻ Contact Form 7 ͷϑΥʔϜ΋ίϨΛ࢖ͬͯઃஔɻ [contact-form-7 id="1" title="͓໰͍߹ΘͤϑΥʔϜ"]

    26
  27. function sugoi_shortcode( $atts ) { $param = shortcode_atts( array( 'count'

    => 1, ), $atts ); return str_repeat( '͍͢͝', $param['count'] ); } add_shortcode( 'sugoi', 'sugoi_shortcode' ); 27
  28. 28

  29. 29

  30. WordCamp Kansai ϋϯζΦϯ΍Γ·ͨ͠ɻ ʮ͜ͷهࣄ͸˓෼ͰಡΊ·͢ʯϓϥάΠ ϯΛγϣʔτίʔυͰ࡞Δɻ wckansai2016/plugin-hands-on 30

  31. ଞʹ΋༷ʑͳAPI WordPress API - WordPress Codex ೔ຊޠ൛ μογϡϘʔυ΢ΟδΣοτ API/σʔλϕʔε API/HTTP

    API/ ϑΝΠϧϔομʔ API/ϑΝΠϧγεςϜAPI/ϝλσʔλ API/ Options API/ϓϥάΠϯ API/ΫΠοΫλάAPI/Rewrite API/ Settings API/γϣʔτίʔυ API/Theme Modification API/ Theme Customization API/Transients API/WordPress ΢Ο δΣοτ API 31
  32. WP-CLI Λ࢖͏ͱϓϥάΠϯͷͻͳܗΛαΫοͱ ࡞ΕΔ $ wp scaffold plugin my-plugin-name 32

  33. Plugin Handbook ެࣜͷϓϥάΠϯͷυΩϡϝϯτɻӳޠ ͚ͩͲɺίʔυͷαϯϓϧͳͲ΋ଟ͍ͷ ͰͳΜͱ͔ͳΔɻ https://developer.wordpress.org/ plugins/ 33

  34. ͱΓ͋͑ͣ • ීஈ࢖͍ճ͍ͯ͠ΔίʔυΛϓϥάΠϯʹ͢Δͱ͜Ζ͔Βε λʔτͯ͠ΈΑ͏ɻ • ϑοΫͱɺγϣʔτίʔυ͕࢖͑Δ͚ͩ΋ීஈͷαΠτ੍࡞ ʹ͍ͩͿ໾ཱͭɻ 34

  35. ͭ·͍ͮͨΒ • αϙʔτϑΥʔϥϜͰฉ͍ͯΈΔɻ • ਓͷίʔυΛಡΜͰΈΔɻ • WordBench ͱ͔Ͱฉ͍ͯΈΔɻ 35

  36. ࢀߟॻ੶ αΠτͷ֦ுੑΛඈ༂తʹߴΊΔ WordPressϓϥάΠϯ։ൃͷόΠϒϧ 36

  37. Thanks! Github: @torounit Twitter: @Toro_Unit Facebook: fb.me/torounit Blog: https://torounit.com 37