Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
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 

...
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_...
Coding Standards
WordPress PHP Coding Standards

https://make.wordpress.org/core/
handbook/coding-standards/php/
!
PHP The...
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.p...
PHP is HTML on Red Bull
<!DOCTYPE html>
<html>
<head>
<title>Smallest Theme</title>
</head>
<body>
<h1><?php echo 'Smallest Theme in PHP'; ?></h1>...
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;
!...
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...
Data interaction
!
Print to page
‘echo’
<!DOCTYPE html>
<html>
<head>
<title>Smallest Theme</title>
</head>
<body>
<h1><?php echo 'Smallest Theme in PHP'; ?></h1>...
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...
Data interaction
!
Return
for use in evaluation
<?php
if ( $wordcamp_is_awesome ) {
$awesomeness = $array_with_keys[ ‘sessions’ ];
} else {
$awesomeness = "Got ya! It's s...
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 aw...
<?php
if ( $wordcamp_is_awesome ) {
$awesomeness = $test_array[ ‘sessions' ];
} else {
$awesomeness = "Got ya! It's still ...
<?php
if ( $wordcamp_is_awesome ) {
$awesomeness = $test_array[ ‘sessions' ];
} else {
$awesomeness = "Got ya! It's still ...
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...
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 ...
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 ...
<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
!
kenneth@sprclldr.com
@stellarcowboy
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Entry-level PHP for WordPress
Prochain SlideShare
Chargement dans…5
×

Entry-level PHP for WordPress

528 vues

Publié le

An introduction to PHP code for use in WordPress theming.

Publié dans : Internet
  • Soyez le premier à commenter

Entry-level PHP for WordPress

  1. 1. Entry-Level PHP for WordPress
  2. 2. 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
  3. 3. Where is this magical PHP? Theme files Plugin files
  4. 4. Tools Not the WordPress file editor! Atom
 Sublime Text 2 Coda 2 Any IDE will do
  5. 5. <?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; ?>
  6. 6. Coding Standards WordPress PHP Coding Standards
 https://make.wordpress.org/core/ handbook/coding-standards/php/ ! PHP The Right Way http://www.phptherightway.com/
  7. 7. The Basics from the perspective of the 
 world’s smallest theme
  8. 8. style.css index.php A theme only needs two files
  9. 9. /* Theme Name: Smallest Theme */ style.css
  10. 10. <!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1>Smallest Theme</h1> </body> </html> index.php
  11. 11. PHP is HTML on Red Bull
  12. 12. <!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1><?php echo 'Smallest Theme in PHP'; ?></h1> </body> </html> index.php
  13. 13. GOTCHAS PHP statements are like sentences. Don’t forget the ‘;’ at the end.
  14. 14. Variables Boolean (true or false) Integer (numbers) String (text) Array (a group of data) Other types (functions, objects)
  15. 15. <?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
  16. 16. Arrays Pairs of data with keys and values Unassigned keys are automatically given a number value.
  17. 17. <?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
  18. 18. Data interaction ! Print to page ‘echo’
  19. 19. <!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
  20. 20. GOTCHAS You can’t “print” an array without a loop, but you can print single data points.
  21. 21. <?php print_r( $array_without_keys ); ?> ! // Array ( [0] => yummy [1] => awesome [2] => Can't wait! ) ! <?php echo $array_without_keys[ 1 ]; ?> ! // awesome
  22. 22. Data interaction ! Return for use in evaluation
  23. 23. <?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
  24. 24. Conditionals ! test if whatever is in parentheses
 is true
  25. 25. <?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
  26. 26. <?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
  27. 27. <?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
  28. 28. GOTCHAS Always surround your conditional with parentheses. Don’t forget your curly braces!
  29. 29. Comparison Operators ! == - Equal To >,< - Greater/Less Than >=,<= - Greater/Less Than or Equal To != - Not Equal To
  30. 30. <?php // Evaluate using a comparison operator if ( $number_of_wordcamps == 4 ) { echo '<p>This fourth WordCamp is the best.</p>'; } ?> index.php
  31. 31. GOTCHAS = assigns a value to a variable, 
 but == evaluates as “is equal to”
  32. 32. Logical Operators ! && - And || - Or
 ! - Not
  33. 33. <?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
  34. 34. Data interaction ! Return for use in built-in WordPress functions
  35. 35. The Loop ! A conditional statement that 
 collects post information
  36. 36. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> ! . . . ! <?php endwhile; endif;?> index.php
  37. 37. <h1><?php the_title(); ?></h1> ! <?php the_content(); ?> index.php
  38. 38. 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
  39. 39. <h1><?php the_title(); ?></h1> ! <h1><?php echo get_the_title( 1 ); ?></h1> index.php
  40. 40. <footer><?php echo $warm_greetings; ?></footer> ! // Thank you for coming to my session! Thank You!
  41. 41. https://github.com/sprclldr/entry-level-php ! http://www.slideshare.net/sprclldr/entry- level-php-for-wordpress
  42. 42. @sprclldr ! kenneth@sprclldr.com @stellarcowboy

×