Entry-level PHP for WordPress
Transcript of 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
<?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/
<!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1>Smallest Theme</h1> </body> </html>
index.php
<!DOCTYPE html> <html> <head> <title>Smallest Theme</title> </head> <body> <h1><?php echo 'Smallest Theme in PHP'; ?></h1> </body> </html>
index.php
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
<?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
<!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
<?php print_r( $array_without_keys ); ?> ! // Array ( [0] => yummy [1] => awesome [2] => Can't wait! ) ! <?php echo $array_without_keys[ 1 ]; ?> ! // awesome
<?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
<?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
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
<?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
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> !. . . !<?php endwhile; endif;?>
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
https://github.com/sprclldr/entry-level-php !
http://www.slideshare.net/sprclldr/entry-level-php-for-wordpress