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

2014 WordCamp Grand Rapids - Core Functions You (Maybe) Don't Know Exist

Nicole
August 15, 2014

2014 WordCamp Grand Rapids - Core Functions You (Maybe) Don't Know Exist

With thousands of functions in the WordPress codebase, it’s virtually impossible to remember all of them. We’ll cover some overlooked WordPress core functions that you may not know exist. We’ll walk through some practical examples for their use, and give you a variety of new gems you can use every day. Presented at WordCamp Grand Rapids 2014.

Nicole

August 15, 2014
Tweet

More Decks by Nicole

Other Decks in Programming

Transcript

  1. wp_list_pluck ! $list (array) A list of objects or arrays

    ! $field (int|string) A field from the object to place instead of the entire object Parameters (array) A numerically indexed array of values from the specified field Returns
  2. wp_list_pluck $vehicles = array( array( 'make' => 'Chevrolet', 'model' =>

    'Traverse', ), array( 'make' => 'Ford', 'model' => 'Focus', ), array( 'make' => 'Dodge', 'model' => 'Charger', ), array( 'make' => 'Chevrolet', 'model' => 'Impala', ), ); $makes = wp_list_pluck( $vehicles, 'make' ); ! echo '<pre>'; print_r( $makes ); echo '</pre>'; Array ( [0] => Chevrolet [1] => Ford [2] => Dodge [3] => Chevrolet )
  3. Array ( [0] => Chevrolet [1] => Ford [2] =>

    Dodge [3] => Chevrolet ) wp_list_pluck $counts = array_count_values( $makes ); ! echo '<pre>'; print_r( $counts ); echo '</pre>'; Array ( [Chevrolet] => 2 [Ford] => 1 [Dodge] => 1 )
  4. human_time_diff ! $from (integer) Unix timestamp from which the difference

    begins. ! $to (integer) Unix timestamp to end the time difference. Default becomes time() if not set. Parameters (string) Human readable time difference Returns
  5. human_time_diff 2 days ago $days = time() - 2*24*60*60; //

    2 days $minutes = time() - 10*60; // 10 minutes $hours = time() - 3*60*60; // 3 hours echo human_time_diff( $days, time() ) . ' ago';
  6. human_time_diff 1 week from now $event = strtotime( ‘8/25/2014’ );

    echo human_time_diff( time(), $event ) . ' from now';
  7. make_clickable Hate manually adding links in your content? With make_clickable(),

    you don't have to. This function will turn email addresses, like [email protected], and urls, such as www.alleyinteractive.com into clickable links automatically. Pretty cool, huh? $content = "Hate manually adding links in your content? With make_clickable(), you don't have to. This function will turn email addresses, like [email protected], and urls, such as www.alleyinteractive.com into clickable links automatically. Pretty cool, huh?"; echo make_clickable( $content );
  8. get_extended Array ( [main] => A simple way to get

    your content, both before and after the 'more' tag with one function. [extended] => Show each section of content separately! [more_text] => ) global $post; ! $content = get_extended( $post->post_content ); ! echo '<pre>'; print_r( $content ); echo '</pre>';
  9. get_extended Array ( [main] => A simple way to get

    your content, both before and after the 'more' tag with one function. [extended] => Show each section of content separately! [more_text] => Read More ) global $post; ! $content = get_extended( $post->post_content ); ! echo '<pre>'; print_r( $content ); echo '</pre>';
  10. get_extended A simple way to get your content, both before

    and after the 'more' tag with one function. Show More <?php echo $content['main']; ?> <span class="read-more">Show More</span> <div class="extended-content" style="display:none;"><?php echo $content['extended']; ?></div> ! <script> jQuery( document ).ready(function() { jQuery( '.read-more' ).on( 'click', function() { jQuery( '.extended-content' ).toggle(); jQuery( '.read-more' ).text( jQuery( '.read-more' ).text() == 'Show More' ? 'Hide' : 'Show More'); }); }); </script>
  11. get_extended <?php echo $content['main']; ?> <span class="read-more">Show More</span> <div class="extended-content"

    style="display:none;"><?php echo $content['extended']; ?></div> ! <script> jQuery( document ).ready(function() { jQuery( '.read-more' ).on( 'click', function() { jQuery( '.extended-content' ).toggle(); jQuery( '.read-more' ).text( jQuery( '.read-more' ).text() == 'Show More' ? 'Hide' : 'Show More'); }); }); </script> A simple way to get your content, both before and after the 'more' tag with one function. Hide Show each section of content separately!
  12. _split_str_by_whitespace ! $string (string) The string to split. ! $goal

    (integer) The desired chunk length. Parameters (array) Numerically indexed array of chunks. Returns
  13. _split_str_by_whitespace Array ( [0] => Lorem ipsum dolor [1] =>

    sit amet, [2] => consectetur [3] => adipiscing elit. ) $content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; ! $split_content = _split_str_by_whitespace( $content, 20 ); ! echo '<pre>'; print_r( $split_content ); echo '</pre>';
  14. is_email $email (string) Email address to check. Parameters (string |

    bool) Either returns false or the valid email address. Returns
  15. is_email Your email is valid $email = ‘[email protected]’; ! if

    ( is_email( $email ) ) { echo ‘Your email is valid’; }
  16. antispambot $email (string) Email address to check. ! $hex_encoding (integer)

    Optional 0 to allow only decimal encoding or 1 to also allow hex encoding. Default is 0. Parameters (string) Converted email address. Returns