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

俺のフックが名古屋でもこんなに簡単なわけがない。

 俺のフックが名古屋でもこんなに簡単なわけがない。

WordPressのフィルターフック、アクションフックの動作原理と使い方を実例多めに解説した資料です。

Takuro Hishikawa

August 31, 2013
Tweet

More Decks by Takuro Hishikawa

Other Decks in Technology

Transcript

  1. ϑΟϧλʔϑοΫͷ͓͞Β͍ function ࠞͥΔʹΑ͘Λ଍͢ ($text) { return ‘Α͘’ . $text; }

    add_filter('ࠞͥΔ', 'ࠞͥΔʹ Α͘Λ଍͢'); ͋͘·Ͱطଘͷॲཧͷมߋɻ 31
  2. 8PSE1SFTTίΞϑΝΠϧͷ֘౰ͷίʔυ function wp_trim_excerpt($text = '') { $raw_excerpt = $text; if

    ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } wp-includes/formatting.phpʢൈਮʣ 44
  3. 8PSE1SFTTίΞϑΝΠϧͷ֘౰ͷίʔυ function wp_trim_excerpt($text = '') { $raw_excerpt = $text; if

    ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } BQQMZ@pMUFST ϑΟϧλʔΛ͔͚Δ wp-includes/formatting.phpʢൈਮʣ 45
  4. 8PSE1SFTTίΞϑΝΠϧͷ֘౰ͷίʔυ function wp_trim_excerpt($text = '') { $raw_excerpt = $text; if

    ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } ઌ΄ͲͷNPSFͷத਎͕൑໌ɻ ൒֯εϖʔε <>ͩͬͨɻ wp-includes/formatting.phpʢൈਮʣ 46
  5. 8PSE1SFTTίΞϑΝΠϧͷ֘౰ͷίʔυ function wp_trim_excerpt($text = '') { $raw_excerpt = $text; if

    ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]&gt;', $text); $excerpt_length = apply_filters('excerpt_length', 55); $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); } return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); } ίΞϑΝΠϧதͷFYDFSQU@NPSFʹ ୅ೖ͞ΕΔ஋͕มΘΔ wp-includes/formatting.phpʢൈਮʣ 49
  6. ֘౰෦෼ͷίʔυ /** * Categories widget class * * @since 2.8.0

    */ class WP_Widget_Categories extends WP_Widget { ... ! ! $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); ... ! ! <ul> <?php ! ! $cat_args['title_li'] = ''; ! ! wp_list_categories(apply_filters('widget_categories_args', $cat_args)); ?> ! ! </ul> ... wp-includes/default-widget.phpʢൈਮʣ 55
  7. ֘౰෦෼ͷίʔυ /** * Categories widget class * * @since 2.8.0

    */ class WP_Widget_Categories extends WP_Widget { ... ! ! $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); ... ! ! <ul> <?php ! ! $cat_args['title_li'] = ''; ! ! wp_list_categories(apply_filters('widget_categories_args', $cat_args)); ?> ! ! </ul> ... XQ@MJTU@DBUFHPSJFT ؔ਺Ͱ ΧςΰϦʔͷҰཡΛग़ྗͯ͠ΔΑ͏ͩɻ wp-includes/default-widget.phpʢൈਮʣ 56
  8. ֘౰෦෼ͷίʔυ /** * Categories widget class * * @since 2.8.0

    */ class WP_Widget_Categories extends WP_Widget { ... ! ! $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); ... ! ! <ul> <?php ! ! $cat_args['title_li'] = ''; ! ! wp_list_categories(apply_filters('widget_categories_args', $cat_args)); ?> ! ! </ul> ... Φϓγϣϯ஋DBU@BSHTʹ ϑΟϧλʔΛ௨ͯ͋͠Δʂ ͜ΕͰฒͼॱ΋มߋͰ͖ͦ͏ɻ wp-includes/default-widget.phpʢൈਮʣ 57
  9. ֘౰෦෼ͷίʔυ /** * Categories widget class * * @since 2.8.0

    */ class WP_Widget_Categories extends WP_Widget { ... ! ! $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); ... ! ! <ul> <?php ! ! $cat_args['title_li'] = ''; ! ! wp_list_categories(apply_filters('widget_categories_args', $cat_args)); ?> ! ! </ul> ... Φϓγϣϯ஋DBU@BSHT͸ ഑ྻͳΜͩͳɻ ฒͼॱ͸໊લॱʹͳ͍ͬͯΔͳɻ wp-includes/default-widget.phpʢൈਮʣ 58
  10. ֘౰෦෼ͷίʔυ /** * Categories widget class * * @since 2.8.0

    */ class WP_Widget_Categories extends WP_Widget { ... ! ! $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h); ... ! ! <ul> <?php ! ! $cat_args['title_li'] = ''; ! ! wp_list_categories(apply_filters('widget_categories_args', $cat_args)); ?> ! ! </ul> ... ϑΟϧλʔ໊͸ ʮXJEHFU@DBUFHPSJFT@BSHTʯ͔ɻ wp-includes/default-widget.phpʢൈਮʣ 59
  11. GVODUJPOTQIQʹهࡌ͢Δίʔυ function change_widget_category_orderby( $cat_args ) { $cat_args['orderby'] = 'count'; $cat_args['order']

    = 'DESC'; return $cat_args; } add_filter( 'widget_categories_args', 'change_widget_category_orderby' ); 60
  12. GVODUJPOTQIQʹهࡌ͢Δίʔυ function change_widget_category_orderby( $cat_args ) { $cat_args['orderby'] = 'count'; $cat_args['order']

    = 'DESC'; return $cat_args; } add_filter( 'widget_categories_args', 'change_widget_category_orderby' ); XQ@MJTU@DBUFHPSJFT ͷ Φϓγϣϯ஋Λ഑ྻͰड͚औͬͯʜ ˢ ͜Ε͕ιʔεΛಡ·ͳ͍ͱ෼͔Βͳ͍ 61
  13. GVODUJPOTQIQʹهࡌ͢Δίʔυ function change_widget_category_orderby( $cat_args ) { $cat_args['orderby'] = 'count'; $cat_args['order']

    = 'DESC'; return $cat_args; } add_filter( 'widget_categories_args', 'change_widget_category_orderby' ); มߋ͍ͨ͠Φϓγϣϯ஋Λ ઃఆͯ͠ʜ ˢ ͬͪ͜͸$PEFYͷXQ@MJTU@DBUFHPSJFTͷ ϖʔδΛݟͳ͍ͱ෼͔Βͳ͍ 62
  14. GVODUJPOTQIQʹهࡌ͢Δίʔυ function change_widget_category_orderby( $cat_args ) { $cat_args['orderby'] = 'count'; $cat_args['order']

    = 'DESC'; return $cat_args; } add_filter( 'widget_categories_args', 'change_widget_category_orderby' ); ฦ͢ 63
  15. XQMPHJOQIQͷϑΟϧλʔϑοΫ $login_header_url = __( 'http://wordpress.org/' ); ... $login_header_url = apply_filters(

    'login_headerurl', $login_header_url ); ... <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1> wp-login.phpʢൈਮʣ 72
  16. XQMPHJOQIQͷϑΟϧλʔϑοΫ $login_header_url = __( 'http://wordpress.org/' ); ... $login_header_url = apply_filters(

    'login_headerurl', $login_header_url ); ... <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1> wp-login.phpʢൈਮʣ ม਺MPHJO@IFBEFS@VSMʹ͸ 8PSE1SFTTެࣜαΠτͷ63-͕ ֨ೲ͞Ε͍ͯΔ ʢ຋༁ϑΝΠϧʹΑ࣮ͬͯࡍʹ͸ KBXPSEQSFTTPSHʹͳΔʣ 73
  17. XQMPHJOQIQͷϑΟϧλʔϑοΫ $login_header_url = __( 'http://wordpress.org/' ); ... $login_header_url = apply_filters(

    'login_headerurl', $login_header_url ); ... <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1> wp-login.phpʢൈਮʣ ม਺MPHJO@IFBEFS@VSMʹ ϑΟϧλʔ͕ద༻͞Ε͍ͯΔ 74
  18. XQMPHJOQIQͷϑΟϧλʔϑοΫ $login_header_url = __( 'http://wordpress.org/' ); ... $login_header_url = apply_filters(

    'login_headerurl', $login_header_url ); ... <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1> wp-login.phpʢൈਮʣ ม਺MPHJO@IFBEFS@VSM͕ ࢖ΘΕ͍ͯΔͷ͸ϩάΠϯ ϑΥʔϜͷϩΰͷϦϯΫઌ 75
  19. XQMPHJOQIQͷΞΫγϣϯϑοΫ function login_footer_message() { ! echo '<h1 style="text- align:center">ͭͳ͕Ζ·͍ʂ</h1>'; }

    add_action( 'login_footer', 'login_footer_message' ); ϝοηʔδΛ௥Ճͯ͠Έͨ 78
  20. ࿅श໰୊ɿϩάΠϯΤϥʔϝοηʔδ echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n"; wp-login.phpʢൈਮʣ

    ΤϥʔϝοηʔδͷʮɻʯΛ ʮὑʯʹม͑ͯΈ·͠ΐ͏ʂ ղ౴͸ͪ͜Β IUUQTHJTUHJUIVCDPNIJTTZ DGCB 79