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

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. • Language Basics • WP Coding Standards • Common Files

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

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

    • Functions • Objects PHP Language Basics
  4. 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’ )
  5. • header.php • footer.php • functions.php • page.php, single.php •

    front-page.php, home.php • category.php, archive.php Common Theme Files
  6. 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
  7. <?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
  8. <?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; ?>
  9. <?php if ( have_posts() ) : while ( have_posts() )

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

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  12. • the_title() • the_content() • the_author() • the_category() • the_time()

    Examples of Template Tags https://codex.wordpress.org/Template_Tags
  13. 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
  14. 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
  15. • Language Basics • WP Coding Standards • Common Files

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

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

    ?> Conditional Statements https://codex.wordpress.org/Conditional_Tags
  18. • 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
  19. 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() ) {
  20. • Language Basics • WP Coding Standards • Common Files

    • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress
  21. 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' );
  22. function register_my_menus() { register_nav_menus( array( 'footer_menu' => __( 'Footer Menu',

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

    add_action( 'save_post', ‘publish_post_to_slack’ );