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

PHP for WordPress

De0a01122ca4a619004332ab04431b7b?s=47 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.

De0a01122ca4a619004332ab04431b7b?s=128

Zac Gordon

September 12, 2015
Tweet

Transcript

  1. PHP for WordPress Zac Gordon @zgordon

  2. What is PHP?

  3. Many Developers Learn PHP via WP

  4. Often Starts with Theme Customization

  5. Some Examples of PHP Theme Customization

  6. What to Know Before Learning PHP

  7. What to Know 
 About PHP for WordPress

  8. • Language Basics • WP Coding Standards • Common Files

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  9. • Syntax • Variables • Operators • Conditionals • Loops

    • Functions • Objects PHP Language Basics
  10. Learn PHP Basics
 In 4 Hours at CodeAcademy For Free

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

  12. Ways of Writing PHP in WordPress Files

  13. <?php if ( is_front_page() ) { echo “<h1>Welcome Home!</h1>”; }

    ?> HTML in PHP example-1.php
  14. <article id=“<?php the_ID; ?>”> <h1><?php the_title(); ?></h1> </article> PHP in

    HTML example-2.php
  15. • Syntax • Variables • Operators • Conditionals • Loops

    • Functions • Objects PHP Language Basics
  16. Learn PHP Basics
 In 4 Hours at CodeAcademy For Free

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

  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’ )
  19. Common PHP Theme Files

  20. • header.php • footer.php • functions.php • page.php, single.php •

    front-page.php, home.php • category.php, archive.php Common Theme Files
  21. Template Hierarchy http://wphierarchy.com/

  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
  23. Why Learn The Loop?

  24. <?php if ( have_posts() ) : while ( have_posts() )

    : the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_content(); ?> <?php else: ?> <h1>Oh No!!!</h1> <p>There is no content for this page</p> <?php endif; endwhile; ?> The Loop
  25. <?php $args = array( 'post_type' => ‘portfolio’, ‘category_name’ => ‘featured’,

    ‘posts_per_page’ => 4 ); $query = new WP_Query( $args ); ?> Custom Loops with WP_Query <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?> <h1><?php the_title(); ?></h1> <?php the_content(); ?> <?php endwhile; endif; ?>
  26. <?php if ( have_posts() ) : while ( have_posts() )

    : the_post(); ?> <?php get_template_part( 'content', 'page' ); ?> <?php endif; endwhile; ?> Including Files
  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
  28. • Language Basics • WP Coding Standards • Common Files

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  29. Template Tags Get You What You Need

  30. • the_title() • the_content() • the_author() • the_category() • the_time()

    Examples of Template Tags https://codex.wordpress.org/Template_Tags
  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
  32. Template Tag Documentation

  33. One More bloginfo()

  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
  35. https://codex.wordpress.org/ Template_Tags

  36. • Language Basics • WP Coding Standards • Common Files

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  37. <?php if ( is_front_page() ): ?> <h1>Welcome Home!</h1> <?php endif;

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

    ?> Conditional Statements https://codex.wordpress.org/Conditional_Tags
  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
  40. Can Also Do Not !is_single()

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

    ) { } else { } Multiple Conditions
  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' ), '<span class="vcard">' . get_the_author() . '</span>' ); } elseif ( is_year() ) { $title = sprintf( __( 'Year: %s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) ); } elseif ( is_month() ) {
  43. • Language Basics • WP Coding Standards • Common Files

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  44. Hooks Let You Do Anything, Anywhere, Anytime In WordPress

  45. Filters vs Actions

  46. “Anything, Anywhere, Anytime” Filters }

  47. “Anything, Anywhere, Anytime” Actions }

  48. Filters modify something

  49. Actions do something

  50. A Few Examples of Actions and Filters

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

    'excerpt_length_example' );
  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' );
  53. function register_my_menus() { register_nav_menus( array( 'footer_menu' => __( 'Footer Menu',

    'mytheme' ) ) ); } add_action( 'init', 'register_my_menus' );
  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' );
  55. function publish_post_to_slack( ) { // Connect to Slack API }

    add_action( 'save_post', ‘publish_post_to_slack’ );
  56. http://codex.wordpress.org/Plugin_API/Filter_Reference http://codex.wordpress.org/Plugin_API/Action_Reference

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

  58. Where to Learn All of This, Again

  59. Language Basics CodeAcademy

  60. WP Coding Standards make.wordpress.org

  61. Common Files wphierarchy.com

  62. The Loop codex.wordpress.org/The_Loop

  63. Template Tags codex.wordpress.org/Template_Tags

  64. Conditional Tags codex.wordpress.org/Conditional_Tags

  65. Hooks Treehouse Hooks Course

  66. Be Practical In Your Learning

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