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

WordPress Plugin 入門 #wpshinshu / 2019-06-22 Sh...

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

More Decks by Toro_Unit (Hiroshi Urabe)

Other Decks in Technology

Transcript

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

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

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

    Posts Blocks • Simple Post Type Permalinks • Powerful Posts Per Page (PPPP) • Vanilla • and more... 5
  4. 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
  5. 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
  6. 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
  7. ࡶͳϨγϐΛ 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
  8. ೑͡Ό͕ʹม͑Δ 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
  9. 1. ࡐྉΛ༻ҙ͢Δ: [ಲ೑ɺδϟΨΠϞɺʹΜ͡Μɺ͠Β͖ͨɺλϚωΪ] 2. ࡐྉΛ੾Δ 3. ᖱΊΔ 4. μγΛऔΔ 5.

    ࣽΔ 6. փोΛऔΔ 7. མͱ֖͠ 8. ௐຯྉ(ে༉ɺΈΓΜɺ࠭౶)ΛೖΕΔ 25
  10. 27

  11. Α͘࢖͏ action • after_setup_theme: ςʔϚͷઃఆ͕ऴΘͬͨޙ • init : WordPress ͷॳظઃఆͳͲ͕ऴΘͬͨޙ

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

    ҰཡϖʔδͰͷλΠτϧɺ the_archive_title() ͷςΩετΛมߋɻ ͋Μ·Γͳ͍ɻͦͷ౎౓ඞཁʹԠͯ͡ɻɻɻ 30
  13. 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
  14. ϓϥάΠϯςϦτϦʔ Plugin Territory • ΞΫηεղੳ • SEO ͷͨΊͷػೳ • ͓໰͍߹ΘͤϑΥʔϜ

    • ΧελϜ౤ߘλΠϓ • ΧελϜϒϩοΫɾγϣʔτίʔυ • etc.... 35