50
Entry-Level PHP for WordPress

Entry-level PHP for WordPress

Embed Size (px)

Citation preview

Entry-Level PHP for WordPress

Who is this for? You can read HTML

You know how to access WordPress theme files via FTP

You’ve seen some of these things ( <?php ?>) and you want to know

what they do

Where is this magical PHP? Theme files Plugin files

Tools Not the WordPress file editor!

Atom Sublime Text 2

Coda 2 Any IDE will do

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> ! <h2><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2> <?php echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>'; ?> <div class="post-content"> <?php the_content(__('Read more'));?> </div> <?php get_template_part('partials/post-meta'); ?> <?php endwhile; else: ?> ! <div class="no-results"> <p>We're sorry, no results were found.</p> </div><!—noResults--> !<?php endif; ?>

Coding Standards WordPress PHP Coding Standardshttps://make.wordpress.org/core/handbook/coding-standards/php/

!PHP The Right Way

http://www.phptherightway.com/

The Basics from the perspective of the

world’s smallest theme

style.css index.php

A theme only needs two files

/* Theme Name: Smallest Theme */

style.css

<!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1>Smallest Theme</h1> </body> </html>

index.php

PHP is HTML on Red Bull

<!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1><?php echo 'Smallest Theme in PHP'; ?></h1> </body> </html>

index.php

GOTCHAS PHP statements are like sentences.

Don’t forget the ‘;’ at the end.

Variables Boolean (true or false)

Integer (numbers) String (text)

Array (a group of data) Other types (functions, objects)

<?php // Boolean - a true or false variable $wordcamp_is_awesome = true; !// Integer - number $number_of_wordcamps = 4; !// String - text $warm_greetings = 'Thank you for coming to my session!'; ?> !<!DOCTYPE html> <html> . . .

index.php

Arrays Pairs of data with keys and values Unassigned keys are automatically

given a number value.

<?php !// Array - a group of data in key/value pairs $array_with_keys = array( 'lunch' => 'yummy', 'sessions' => 'awesome', 'after_party' => "Can't wait!" ); !$array_wtihout_keys = array( 'yummy', // 0 'awesome', // 1 "Can't wait!" // 2 ); ?>

index.php

Data interaction !Print to page

‘echo’

<!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1><?php echo 'Smallest Theme in PHP'; ?></h1> <main> <p>Welcome to WordCamp Nashville #<?php echo $number_of_wordcamps; ?></p> <p>Lunch was <?php echo $array_with_keys[ ‘lunch’ ]; ?>.</p> </main> </body> </html>

index.php

GOTCHAS You can’t “print” an array without a loop, but you can print single data

points.

<?php print_r( $array_without_keys ); ?> ! // Array ( [0] => yummy [1] => awesome [2] => Can't wait! ) ! <?php echo $array_without_keys[ 1 ]; ?> ! // awesome

Data interaction !

Return for use in evaluation

<?php if ( $wordcamp_is_awesome ) { $awesomeness = $array_with_keys[ ‘sessions’ ]; } else { $awesomeness = "Got ya! It's still awesome"; } ?> <h3>WordCamp is ... <?php echo $awesomeness; ?>!</h3>

index.php

Conditionals !

test if whatever is in parentheses is true

<?php if ( $wordcamp_is_awesome ) { $awesomeness = $test_array['sessions']; } else { $awesomeness = "Got ya! It's still awesome"; } ?> <h3>WordCamp is ... <?php echo $awesomeness; ?>!</h3> ! // WordCamp is ...

index.php

<?php if ( $wordcamp_is_awesome ) { $awesomeness = $test_array[ ‘sessions' ]; } else { $awesomeness = "Got ya! It's still awesome"; } ?> <h3>WordCamp is ... <?php echo $awesomeness; ?>!</h3> ! // WordCamp is ... awesome!

index.php

<?php if ( $wordcamp_is_awesome ) { $awesomeness = $test_array[ ‘sessions' ]; } else { $awesomeness = "Got ya! It's still awesome"; } ?> <h3>WordCamp is ... <?php echo $awesomeness; ?>!</h3> ! // WordCamp is ... awesome!

index.php

GOTCHAS Always surround your conditional with

parentheses. Don’t forget your curly braces!

Comparison Operators !

== - Equal To >,< - Greater/Less Than >=,<= - Greater/Less Than or Equal To != - Not Equal To

<?php

// Evaluate using a comparison operator if ( $number_of_wordcamps == 4 ) { echo '<p>This fourth WordCamp is the best.</p>'; } ?>

index.php

GOTCHAS = assigns a value to a variable,

but == evaluates as “is equal to”

Logical Operators !

&& - And || - Or! - Not

<?php // Evaluate using a logical operator if ( $number_of_wordcamps == 4 && $wordcamp_is_awesome ) { echo "<p>Now you're getting the hang of it.</p>"; } ?>

index.php

Data interaction !

Return for use in built-in WordPress functions

The Loop !

A conditional statement that collects post information

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> !. . . !<?php endwhile; endif;?>

index.php

<h1><?php the_title(); ?></h1> ! <?php the_content(); ?>

index.php

Get vs. The !

When “the” starts a WordPress function name, it prints directly to the page.

When “get” starts a WordPress function name, it simply returns that value to a

variable

<h1><?php the_title(); ?></h1> ! <h1><?php echo get_the_title( 1 ); ?></h1>

index.php

<footer><?php echo $warm_greetings; ?></footer> ! // Thank you for coming to my session!

Thank You!

https://github.com/sprclldr/entry-level-php !

http://www.slideshare.net/sprclldr/entry-level-php-for-wordpress

@sprclldr [email protected] @stellarcowboy