Slide 1

Slide 1 text

PHP for WordPress Zac Gordon @zgordon

Slide 2

Slide 2 text

What is PHP?

Slide 3

Slide 3 text

Many Developers Learn PHP via WP

Slide 4

Slide 4 text

Often Starts with Theme Customization

Slide 5

Slide 5 text

Some Examples of PHP Theme Customization

Slide 6

Slide 6 text

What to Know Before Learning PHP

Slide 7

Slide 7 text

What to Know 
 About PHP for WordPress

Slide 8

Slide 8 text

• Language Basics • WP Coding Standards • Common Files • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Learn PHP Basics
 In 4 Hours at CodeAcademy For Free codecademy.com

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Ways of Writing PHP in WordPress Files

Slide 13

Slide 13 text

Welcome Home!”; } ?> HTML in PHP example-1.php

Slide 14

Slide 14 text

”>

PHP in HTML example-2.php

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Learn PHP Basics
 In 4 Hours at CodeAcademy For Free codecademy.com

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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’ )

Slide 19

Slide 19 text

Common PHP Theme Files

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Template Hierarchy http://wphierarchy.com/

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Why Learn The Loop?

Slide 24

Slide 24 text

Oh No!!!

There is no content for this page

The Loop

Slide 25

Slide 25 text

‘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(); ?>

Slide 26

Slide 26 text

Including Files

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

• Language Basics • WP Coding Standards • Common Files • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress

Slide 29

Slide 29 text

Template Tags Get You What You Need

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Template Tag Documentation

Slide 33

Slide 33 text

One More bloginfo()

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

https://codex.wordpress.org/ Template_Tags

Slide 36

Slide 36 text

• Language Basics • WP Coding Standards • Common Files • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress

Slide 37

Slide 37 text

Welcome Home!

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

Slide 38

Slide 38 text

Welcome Home!”; } ?> Conditional Statements https://codex.wordpress.org/Conditional_Tags

Slide 39

Slide 39 text

• 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

Slide 40

Slide 40 text

Can Also Do Not !is_single()

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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' ), '' . get_the_author() . '' ); } elseif ( is_year() ) { $title = sprintf( __( 'Year: %s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) ); } elseif ( is_month() ) {

Slide 43

Slide 43 text

• Language Basics • WP Coding Standards • Common Files • The Loop • Template Tags • Conditionals • Hooks What to Know About
 PHP for WordPress

Slide 44

Slide 44 text

Hooks Let You Do Anything, Anywhere, Anytime In WordPress

Slide 45

Slide 45 text

Filters vs Actions

Slide 46

Slide 46 text

“Anything, Anywhere, Anytime” Filters }

Slide 47

Slide 47 text

“Anything, Anywhere, Anytime” Actions }

Slide 48

Slide 48 text

Filters modify something

Slide 49

Slide 49 text

Actions do something

Slide 50

Slide 50 text

A Few Examples of Actions and Filters

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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' );

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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' );

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Where to Learn All of This, Again

Slide 59

Slide 59 text

Language Basics CodeAcademy

Slide 60

Slide 60 text

WP Coding Standards make.wordpress.org

Slide 61

Slide 61 text

Common Files wphierarchy.com

Slide 62

Slide 62 text

The Loop codex.wordpress.org/The_Loop

Slide 63

Slide 63 text

Template Tags codex.wordpress.org/Template_Tags

Slide 64

Slide 64 text

Conditional Tags codex.wordpress.org/Conditional_Tags

Slide 65

Slide 65 text

Hooks Treehouse Hooks Course

Slide 66

Slide 66 text

Be Practical In Your Learning

Slide 67

Slide 67 text

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