Pro Yearly is on sale from $80 to $50! »

WordPress Plugin 入門 #wpshinshu / 2019-06-22 Shinshu WordPress Meetup

WordPress Plugin 入門 #wpshinshu / 2019-06-22 Shinshu WordPress Meetup

Transcript

  1. WordPress Plugin ೖ໳ Toro_Unit @Shinshu WP Meetup vol.13 1

  2. $ whoami 2

  3. Toro_Unit ઎෦ ߛ (͏Β΂ ͻΖ͠) • Frontend Engineer • WordPress

    Plugin and Theme Developer Github: @torounit Twitter: @Toro_Unit 3
  4. Contribution • WordPress 4.3 / 4.4 / 4.7 / 5.0

    / 5.1 / 5.2 • WordCamp Osaka 2019 • WordCamp Osaka 2018 Speaker. • etc... 4
  5. Plugins and Themes • Custom Post Type Permalinks • Advanced

    Posts Blocks • Simple Post Type Permalinks • Powerful Posts Per Page (PPPP) • Vanilla • and more... 5
  6. Question. 6

  7. Theme ࡞ͬͨ͜ͱ͋Δਓ 7

  8. Plugin ࡞ͬͨ͜ͱ͋Δਓ 8

  9. ͦ΋ͦ΋ ϓϥάΠϯͱ͸Կͳͷ͔ɻ 9

  10. • WordPress Λ֦ு͢ΔػೳɻσϑΥϧτʹແ͍ػೳΛ௥Ճ͠ ͨΓɺಈ࡞Λվมͨ͠Γ͢Δɻ • wp-settings.php ͰಡΈࠐ·ΕΔɻ • PHPͷεΫϦϓτɻ͔ͦ͜ΒCSSͱ͔JSͱ͔ΛಡΈࠐΜͩΓ ͢Δ͜ͱ΋ɻ

    10
  11. foreach ( wp_get_active_and_valid_plugins() as $plugin ) { wp_register_plugin_realpath( $plugin );

    include_once( $plugin ); /** * Fires once a single activated plugin has loaded. * * @since 5.1.0 * * @param string $plugin Full path to the plugin's main file. */ do_action( 'plugin_loaded', $plugin ); } 11
  12. wp-content/plugins ͷதʹɺ • hogehoge.php • hogehoge/hugahuga.php ͳͲͷϑΝΠϧΛ࡞੒͠ɺઌ಄ʹίϝϯτΛॻ͚͹ϓϥάΠ ϯɻ <?php /**

    * Plugin Name: Your Plugin */ 12
  13. ΞΫγϣϯϑοΫɺϑΟϧλʔϑοΫ • WordPress ͷಛఆͷಈ࡞Λվม͢ΔͨΊͷػೳɻ 13

  14. add_action ಛఆͷՕॴʹಈ࡞Λ௥Ճ͢Δɻ // ϓϥάΠϯͷελΠϧγʔτΛొ࿥ function add_reading_minutes_styles() { wp_enqueue_style( 'plugin-hans-on-sample', plugin_dir_url(

    __FILE__ ) . 'style.css' ); } // ΞΫγϣϯϑοΫΛొ࿥ add_action( 'wp_enqueue_scripts', 'add_reading_minutes_styles' ); 14
  15. add_filter ಛఆͷՕॴʹಈ࡞Λվม͢Δɻ //ຊจͷલʹ͜ͷهࣄ͸n෼ͰಡΊ·͢Λදࣔɻ function add_reading_minutes_to_the_content( $content ) { $minutes =

    count_reading_minutes( $content ); $text = sprintf( '͜ͷهࣄ͸໿%d෼ͰಡΊ·͢ɻ', $minutes ); $before = '<span class="reading-minutes">' . esc_html( $text ) . '</span>'; return $before . $content; } //ϑΟϧλʔϑοΫΛొ࿥ add_filter( 'the_content', 'add_reading_minutes_to_the_content' ); 15
  16. ࡶͳͨͱ͑ ࡶͳΧϨʔͷ࡞Γํ 1. ࡐྉΛ༻ҙ͢Δ: [ܲ೑ɺδϟΨΠϞɺʹΜ͡ΜɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4.

    ࣽΔ 5. ௐຯྉ(ϧ΢)ΛೖΕΔ 16
  17. ΧϨʔ΋ͪΌΜͱΞΫΛऔͬͨํ͕ඒຯ͍ɻ ΞΫΛऔΓ·͘ΔΧϨʔVSҰ੾औΒͳ͍ΧϨʔ :: σΠϦʔϙʔ λϧZ 17

  18. 1. ࡐྉΛ༻ҙ͢Δ: [ܲ೑ɺδϟΨΠϞɺʹΜ͡ΜɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4. ࣽΔ 5.

    ΞΫΛऔΔ!!! 6. ௐຯྉ(ϧ΢)ΛೖΕΔ 18
  19. ࡶͳϨγϐΛ PHP Ͱॻ͍ͯΈΔɻ $zairyo = [ 'ܲ೑', 'δϟΨΠϞ', 'ʹΜ͡Μ', 'λϚωΪ'

    ]; $zairyo = apply_filters( 'zairyo', $zairyo ); $cook->cut( $zairyo ); do_action( 'before_boil', $cook ); $cook->boil(); do_action( 'after_boiled', $cook ); $seasoning = apply_filters( 'seasoning', [ 'ΧϨʔϧ΢' ] ); $cook->add_seasoning( $seasoning ); 19
  20. ΞΫΛऔΔ add_action( 'after_boiled', 'remove_aku' ); function remove_aku( $cook ) {

    $cook->remove_aku(); } 20
  21. 1. ࡐྉΛ༻ҙ͢Δ: [ܲ೑ɺδϟΨΠϞɺʹΜ͡ΜɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4. ࣽΔ 5.

    ΞΫΛऔΔ!!! 6. ௐຯྉ(ϧ΢)ΛೖΕΔ 21
  22. ϏʔϑΧϨʔʹม͑Δ add_filter( 'zairyo', 'beef_curry_zairyo' ); function beef_curry_zairyo( $zairyo ) {

    return array_replace( $zairyo, [ 0 => 'ڇ೑'] ); } 22
  23. 1. ࡐྉΛ༻ҙ͢Δ: [ ڇ೑!! ɺδϟΨΠϞɺʹΜ͡ΜɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4.

    ࣽΔ 5. ௐຯྉ(ϧ΢)ΛೖΕΔ 23
  24. ೑͡Ό͕ʹม͑Δ add_filter( 'zairyo', function() { return ['ಲ೑', 'δϟΨΠϞ', 'ʹΜ͡Μ', '͠Β͖ͨ',

    'λϚωΪ' ]; }); add_filter( 'seasoning', function() { return [ '͠ΐ͏Ώ', 'ΈΓΜ', '࠭౶' ]; }); add_action( 'before_boil', function( $cook ) { $cook->make_dashi('׍અ'); }); add_action( 'after_boiled', function( $cook ) { $cook->remove_aku(); $cook->set_otoshibuta(); }); 24
  25. 1. ࡐྉΛ༻ҙ͢Δ: [ಲ೑ɺδϟΨΠϞɺʹΜ͡Μɺ͠Β͖ͨɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4. μγΛऔΔ 5.

    ࣽΔ 6. փोΛऔΔ 7. མͱ֖͠ 8. ௐຯྉ(ে༉ɺΈΓΜɺ࠭౶)ΛೖΕΔ 25
  26. ϑοΫͷ୳͠ํ Codex ΛݟΔ • ϓϥάΠϯ API/ΞΫγϣϯϑοΫҰཡ - WordPress Codex ೔

    ຊޠ൛ 26
  27. 27

  28. • Code reference https://developer.wordpress.org/reference/ɹ Ͱ͕͢͞ɻ • ιʔεΛಡΉɻ • ϑΟϧλʔϑοΫ͸ϚδͰଟ͗͢ΔͷͰɺͦͷ౎౓୳ͯ͠ Δɻ͜ͷؔ਺ͱ͔ςϯϓϨʔτλάͲ͏ʹ͔ͳΒͳ͍͔

    ͳʔͬͯײ͡ͰιʔεΛͦͷՕॴ͚ͩಡΉͱɺେ఍ɺ do_action, apply_filter ͕ݟ͔ͭΔɻ 28
  29. Α͘࢖͏ action • after_setup_theme: ςʔϚͷઃఆ͕ऴΘͬͨޙ • init : WordPress ͷॳظઃఆͳͲ͕ऴΘͬͨޙ

    • admin_init : ؅ཧը໘ͰͷॳظઃఆͳͲ͕ऴΘͬͨޙ • wp_enqueue_scripts : CSS ͱ͔ JS Λొ࿥͢Δͱ͖ • pre_get_posts: ౤ߘͷσʔλΛऔಘ͢Δલ 29
  30. Α͘࢖͏ filter • body_class: body_class() Ͱग़ྗ͞ΕΔ class Λվมɻ • get_the_archive_title:

    ҰཡϖʔδͰͷλΠτϧɺ the_archive_title() ͷςΩετΛมߋɻ ͋Μ·Γͳ͍ɻͦͷ౎౓ඞཁʹԠͯ͡ɻɻɻ 30
  31. functions.php ͱͳʹ͕͕ͪ͏ͷ͔ • ಡΈࠐ·ΕΔλΠϛϯά͕ҧ͏ɻϓϥάΠϯͷ100ߦԼ͘Β͍Ͱಡ Έࠐ·ΕΔɻ • ͦΕҎ֎ʹҧ͍͸ಛʹͳ͍ɻfunctions.php ͰՄೳͳ͜ͱ͸શͯϓϥ άΠϯͰ࣮ݱͰ͖Δɻ foreach

    ( wp_get_active_and_valid_themes() as $theme ) { if ( file_exists( $theme . '/functions.php' ) ) { include $theme . '/functions.php'; } } 31
  32. • άάΔͱ functions.php ʹɾɾɾΈ͍ͨͳهࣄ͕ࢁ΄Ͳग़ͯ ͘Δ͕ɺ΄΅શͯϓϥάΠϯͰ΋ಈ͘ɻ 32

  33. ԿނϓϥάΠϯʁςʔϚͰ΋ಉ͜͡ͱ͕Ͱ͖Δͳ Βผʹྑ͘ͳ͍ʁ • ͪ·ͨͰ͸ଟػೳΛചΓʹͯ͠ΔςʔϚ΋ଟ͍Ͱ͕͢ɻɻɻ 33

  34. ςʔϚʹػೳΛؚΊΔ͜ͱͷ໰୊఺ • ςʔϚ͸αΠτͷݟͨ໨Λఆٛ͢ΔϞϊɻػೳΛؚΊΔ΋ͷ Ͱ͸ແ͍ɻ • ݟͨ໨Λม͍͑ͨʂͱ͍͏ͱ͖ʹɺػೳ͕ࢮ͵ͷͰม͑ΒΕ ͳ͍ͳͲͷ໰୊ɻ • ଞͷϞϊͰରԠ͍ͨ͠ػೳΛOFFʹͰ͖ͳ͍ɻ 34

  35. ϓϥάΠϯςϦτϦʔ Plugin Territory • ΞΫηεղੳ • SEO ͷͨΊͷػೳ • ͓໰͍߹ΘͤϑΥʔϜ

    • ΧελϜ౤ߘλΠϓ • ΧελϜϒϩοΫɾγϣʔτίʔυ • etc.... 35
  36. ʮϦχϡʔΞϧ΍ςʔϚͷมߋͰͿͬ ͱΜͩΒࠔΔ΋ͷ͸ϓϥάΠϯʯ Plugin Territory – WordPressͷͦͷॲཧ͸ςʔϚͰ΍Δ΂͖͔ϓϥάΠϯͰ΍Δ΂͖͔ʁ | Firegoby 36

  37. ͲΜͳ;͏ʹ࢓ࣄͰ࢖ͬͯΔ͔ • Ҋ݅Ͱ࡞Δͱ͖͸ɺػೳ͝ͱʹผͷϓϥάΠϯʹ෼ׂ͍ͯ͠ Δɻ • 5ߦͷίʔυͱ͔Ͱ΋ɻ • γϯϓϧͳػೳ͸࢖͍ճ͕͠ޮ͖΍͍͢͠ཧղ͠΍͍͢ɻ • functions.php

    ͕ _sʢελʔλʔςʔϚʣ ͦͷ··ͱ͍͏৔ ߹΋ଟ͍ɻ 37
  38. Thanks! Github: @torounit Twitter: @Toro_Unit Facebook: fb.me/torounit Blog: https://torounit.com 38