$30 off During Our Annual Pro Sale. View Details »

PHP for WordPress

Zac Gordon
September 12, 2015

PHP for WordPress

Slides for a talk at WordCamp Baltimore 2015 about what to know about PHP when starting off with WordPress Development as well as where you can learn all of this.

Zac Gordon

September 12, 2015
Tweet

More Decks by Zac Gordon

Other Decks in Technology

Transcript

  1. PHP for WordPress
    Zac Gordon
    @zgordon

    View Slide

  2. What is PHP?

    View Slide

  3. Many Developers
    Learn PHP via WP

    View Slide

  4. Often Starts with
    Theme Customization

    View Slide

  5. Some Examples of
    PHP Theme Customization

    View Slide

  6. What to Know
    Before Learning PHP

    View Slide

  7. What to Know 

    About PHP for WordPress

    View Slide

  8. • Language Basics
    • WP Coding Standards
    • Common Files
    • The Loop
    • Template Tags
    • Conditionals
    • Hooks
    What to Know About

    PHP for WordPress

    View Slide

  9. • Syntax
    • Variables
    • Operators
    • Conditionals
    • Loops
    • Functions
    • Objects
    PHP Language Basics

    View Slide

  10. Learn PHP Basics

    In 4 Hours at CodeAcademy
    For Free codecademy.com

    View Slide

  11. Learn PHP
    In 4 Hours on CodeAcademy
    For Free!!!

    View Slide

  12. Ways of Writing PHP
    in WordPress Files

    View Slide

  13. if ( is_front_page() ) {
    echo “Welcome Home!”;
    }
    ?>
    HTML in PHP
    example-1.php

    View Slide

  14. ”>


    PHP in HTML
    example-2.php

    View Slide

  15. • Syntax
    • Variables
    • Operators
    • Conditionals
    • Loops
    • Functions
    • Objects
    PHP Language Basics

    View Slide

  16. Learn PHP Basics

    In 4 Hours at CodeAcademy
    For Free codecademy.com

    View Slide

  17. WordPress has PHP
    Coding Standards
    https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/

    View Slide

  18. function myCustomFunction() {}
    function my_custom_function() {}
    WordPress has PHP
    Coding Standards
    if(is_single()):
    if ( is_single() ) :
    the_title(‘h2’,’h3’)
    the_title( ‘h2’, ‘h2’ )

    View Slide

  19. Common PHP
    Theme Files

    View Slide

  20. • header.php
    • footer.php
    • functions.php
    • page.php, single.php
    • front-page.php, home.php
    • category.php, archive.php
    Common Theme Files

    View Slide

  21. Template Hierarchy
    http://wphierarchy.com/

    View Slide

  22. Archives
    Custom Posts

    Miscellaneous
    Hierarchy Review
    How They Work
    Core Files

    Homepages
    Pages and Posts
    Template Hierarchy Course
    at Treehouse
    http://teamtreehouse.com/library/the-wordpress-template-hierarchy

    View Slide

  23. Why Learn
    The Loop?

    View Slide





  24. Oh No!!!
    There is no content for this page

    The Loop

    View Slide

  25. $args = array(
    'post_type' => ‘portfolio’,
    ‘category_name’ => ‘featured’,
    ‘posts_per_page’ => 4
    );
    $query = new WP_Query( $args );
    ?>
    Custom Loops with WP_Query
    have_posts() ) : while( $query->have_posts() ) :
    $query->the_post(); ?>



    View Slide




  26. Including Files

    View Slide

  27. Including Files
    get_template_part( 'content', 'page' );
    Loads: content-page.php
    get_template_part( 'custom' );
    Loads: custom.php
    get_template_part( ‘lib/my’ ‘file’ );
    Loads: /lib/my-file.php

    View Slide

  28. • Language Basics
    • WP Coding Standards
    • Common Files
    • The Loop
    • Template Tags
    • Conditionals
    • Hooks
    What to Know About

    PHP for WordPress

    View Slide

  29. Template Tags
    Get You What You Need

    View Slide

  30. • the_title()
    • the_content()
    • the_author()
    • the_category()
    • the_time()
    Examples of
    Template Tags
    https://codex.wordpress.org/Template_Tags

    View Slide

  31. the_time( 'F j, Y' );
    September 12, 2015
    Some Template Tags
    Take Parameters
    https://codex.wordpress.org/Function_Reference/the_time
    the_time( ‘n/j/y’ );
    09/12/15
    the_time( ‘l g:i a’ );
    Saturday 9:00 am

    View Slide

  32. Template Tag
    Documentation

    View Slide

  33. One More
    bloginfo()

    View Slide

  34. bloginfo( 'name' );
    Site Name
    Get Information
    About WordPress Install
    https://codex.wordpress.org/Function_Reference/bloginfo
    bloginfo( ‘description’ );
    Site Description
    bloginfo( ‘url’ );
    http://siteurl.com

    View Slide

  35. https://codex.wordpress.org/
    Template_Tags

    View Slide

  36. • Language Basics
    • WP Coding Standards
    • Common Files
    • The Loop
    • Template Tags
    • Conditionals
    • Hooks
    What to Know About

    PHP for WordPress

    View Slide


  37. Welcome Home!

    Conditional Statements
    https://codex.wordpress.org/Conditional_Tags

    View Slide

  38. if ( is_front_page() ) {
    echo “Welcome Home!”;
    }
    ?>
    Conditional Statements
    https://codex.wordpress.org/Conditional_Tags

    View Slide

  39. • is_page(), is_page( ‘about‘ )
    • is_category()
    • is_author()
    • wp_is_mobile()
    • is_main_query()
    • is_woocommerce()
    • is_shop(), is_cart()
    Conditional Statements
    https://codex.wordpress.org/Conditional_Tags

    View Slide

  40. Can Also Do Not
    !is_single()

    View Slide

  41. if ( is_front_page() ) {
    } elseif ( is_page( ‘about’ ) {
    } else {
    }
    Multiple Conditions

    View Slide

  42. function get_the_archive_title() {
    if ( is_category() ) {
    $title = sprintf( __( 'Category: %s' ),
    single_cat_title( '', false ) );
    } elseif ( is_tag() ) {
    $title = sprintf( __( 'Tag: %s' ),
    single_tag_title( '', false ) );
    } elseif ( is_author() ) {
    $title = sprintf( __( 'Author: %s' ), 'class="vcard">' . get_the_author() . '' );
    } elseif ( is_year() ) {
    $title = sprintf( __( 'Year: %s' ),
    get_the_date( _x( 'Y', 'yearly archives date
    format' ) ) );
    } elseif ( is_month() ) {

    View Slide

  43. • Language Basics
    • WP Coding Standards
    • Common Files
    • The Loop
    • Template Tags
    • Conditionals
    • Hooks
    What to Know About

    PHP for WordPress

    View Slide

  44. Hooks Let You Do
    Anything, Anywhere, Anytime
    In WordPress

    View Slide

  45. Filters vs Actions

    View Slide

  46. “Anything, Anywhere, Anytime”
    Filters
    }

    View Slide

  47. “Anything, Anywhere, Anytime”
    Actions
    }

    View Slide

  48. Filters modify something

    View Slide

  49. Actions do something

    View Slide

  50. A Few Examples of
    Actions and Filters

    View Slide

  51. function excerpt_length_example( $length ) {
    return 15;
    }
    add_filter( 'excerpt_length', 'excerpt_length_example' );

    View Slide

  52. function body_class_example( $classes ) {
    if( is_single() ) {
    foreach( get_the_category( get_the_ID() ) as $cat )
    $classes[] = 'cat-' . $cat->category_nicename;
    }
    return $classes;
    }
    add_filter( 'body_class', 'body_class_example' );

    View Slide

  53. function register_my_menus() {
    register_nav_menus(
    array(
    'footer_menu' => __( 'Footer Menu', 'mytheme' )
    )
    );
    }
    add_action( 'init', 'register_my_menus' );

    View Slide

  54. function my_plugin_page() {
    add_options_page(
    'Page Title',
    ‘Menu Title',
    'manage_options',
    ‘plugin-slug',
    ‘my_plugin_page_init’ );
    }
    add_action( 'admin_menu' , 'my_plugin_page' );

    View Slide

  55. function publish_post_to_slack( ) {
    // Connect to Slack API
    }
    add_action( 'save_post', ‘publish_post_to_slack’ );

    View Slide

  56. http://codex.wordpress.org/Plugin_API/Filter_Reference
    http://codex.wordpress.org/Plugin_API/Action_Reference

    View Slide

  57. http://codex.wordpress.org/Plugin_API/Action_Reference
    Runtime

    View Slide

  58. Where to Learn
    All of This, Again

    View Slide

  59. Language Basics
    CodeAcademy

    View Slide

  60. WP Coding Standards
    make.wordpress.org

    View Slide

  61. Common Files
    wphierarchy.com

    View Slide

  62. The Loop
    codex.wordpress.org/The_Loop

    View Slide

  63. Template Tags
    codex.wordpress.org/Template_Tags

    View Slide

  64. Conditional Tags
    codex.wordpress.org/Conditional_Tags

    View Slide

  65. Hooks
    Treehouse Hooks Course

    View Slide

  66. Be Practical
    In Your Learning

    View Slide

  67. Zac Gordon
    wp.zacgordon.com
    @zgordon
    https://teamtreehouse.com/learn/wordpress

    View Slide