Beginners Guide To PHP in Wordpress
Beginners Guide To PHP in Wordpress
to PHP in WordPress
WordCamp Baltimore 2014
Zane M. Konik
@zkolnik on twitter
[email protected]
Intro to coding,
non-developers,
what PHP do I need to know
for WordPress?
How do I get it
& what version?
Apple Customers
MAMP
MAMP Pro
WAMP
WAMP Pro
PHP Versions
If developing to contribute plugins or themes use 5.3.
PHP Versions
Specifically what PHP code do I need to know?
1. Conditional Statements
2. Printing
3. Arrays (Ordered Maps)
4. Loops
5. Functions
6. File includes
PHP
If, else, elseif, switch
Conditional Statements
If, else, elseif, switch
&& and
|| or
== equal
! false
!= not equal
=== exactly equal, i.e., 0, 0
!== exactly not equal
<, >, <=, >= greater/less than
Conditional Statements
If, else, elseif, switch
Conditional Statements
If, else, elseif, switch
empty()
is_null()
isset()
Conditional Statements
Conditional Statements
echo, print, prinft(), sprintf()
Printing
echo vs. print?
Simply used to display content, echo is a language construct (its built into
the language).
);
printf, sprintf
Arrays
Indexed
Associative
Multi-dimensional
Arrays
$pages_by_slug = array(about, blog, contact-me);
Indexed
$pages_by_slug = array(about, blog, contact-me);
Indexed
$pages_by_slug = array(about, blog, contact-me);
Indexed
$pages = array(
page_name => Contact,
page_id => 89,
page_slug => contact-me
);
Associative
$pages = array(
page_name => Contact,
page_id => 89,
page_slug => contact-me
);
Associative
$items = array(
pages => array(
name => Contact,
id => 89,
slug => contact-me
),
pages => array(
...
)
);
Multi-dimensional
foreach( $items as $k => $v ){
echo $k . . $v[slug]; // What does this display?
}
Multi-dimensional
How would we traverse this?
Multi-dimensional
Loops
$i = 1;
while( $i <= 10 ){
echo $i++;
}
Loops
$i = 1;
while( $i <= 10 ){
echo $i++;
}
Loops
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1 class=entry-title><?php the_title(); ?></h1>
<?php endwhile; ?>
<?php endif; ?>
Loops
Functions
foo( name: , tim );
Functions
$a = array(name=> tim);
Functions
File Includes
1. require_once fatal error
2. require fatal error
3. include_once warning error
4. include warning error
File Includes
1. require_once fatal error
2. require fatal error
3. include_once warning error
4. include warning error
File Includes
get_header(); // includes header.php
get_sidebar(); // includes sidebar.php
get_footer(); // includes footer.php
get_template_part( $path, $slug ); // includes partials/content-
image.php
load_template(); // includes $file
locate_template(); // returns a path to a template
File Includes
Debugging Why do you care?
Debugging allows you to be accurate as an owl, that is approaching its
first night meal.
Types of Debugging
1. Software
2. PHP config
3. wp-config.php
Enabling Debugging
Just enable it
Software
Via PHP
error_reporting(-1);
ini_set('display_errors', 'On');
wp-config.php
define('WP_DEBUG_LOG', true);
wp-config.php
define('WP_DEBUG_DISPLAY', false);
Errors will not be shown, but can still be logged via define
('WP_DEBUG_LOG', true);
wp-config.php
define('SCRIPT_DEBUG', true);
Forces WordPress to use the "dev" versions of core CSS and Javascript
files rather than the minified versions that are normally loaded.
wp-config.php
define('SAVEQUERIES', true);
wp-config.php
Note all of these methods should only be done in a development
environment.
Conclusion
Questions
http://wordpress.org/about/stats/
http://php.net/manual/en/function.error-reporting.php
http://php.net/manual/en/function.ini-set.php
http://php.net/manual/en/ref.array.php https://make.wordpress.org/core/handbook/coding-
standards/php/
http://blog.codinghorror.com/new-programming-jargon/
http://www.php-fig.org/psr/psr-1/
http://www.php-fig.org/psr/psr-2/
http://pear.php.net/manual/en/standards.php
http://stackoverflow.com/questions/139427/which-coding-convention-to-follow-for-php
http://pear.php.net/manual/en/standards.php
http://stackoverflow.com/a/10057916/714202
References