Beginners guide-to-php-in-wordpress
-
Upload
ninja-black -
Category
Software
-
view
146 -
download
1
Transcript of Beginners guide-to-php-in-wordpress
![Page 1: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/1.jpg)
A Beginners Guide to PHP in WordPressWordCamp Baltimore 2014
![Page 2: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/2.jpg)
Zane M. Konik
Founder, Zane Matthew, inc.http://zanematthew.com/@zanematthew on twitter
@zkolnik on [email protected]
![Page 3: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/3.jpg)
Intro to coding, non-developers,what PHP do I need to knowfor WordPress?
![Page 4: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/4.jpg)
How do I get it& what version?
![Page 5: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/5.jpg)
How do I get it & what version?
Apple Customers
● MAMP
● MAMP Pro
![Page 6: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/6.jpg)
How do I get it & what version?
Window Customers
● WAMP
● WAMP Pro
![Page 7: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/7.jpg)
MAMP Pro/WAMP Pro
● Supports PHP versions
○ 5.2.17
○ 5.3.2
○ 5.4.1
How do I get it & what version?
![Page 8: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/8.jpg)
PHP Versions
Source http://wordpress.org/about/stats/
![Page 9: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/9.jpg)
PHP Versions
If developing to contribute plugins or themes use 5.3.
![Page 10: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/10.jpg)
Specifically what PHP code do I need to know?
![Page 11: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/11.jpg)
PHP
1. Conditional Statements2. Printing3. Arrays (Ordered Maps)4. Loops5. Functions6. File includes
![Page 12: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/12.jpg)
Conditional Statements
If, else, elseif, switch
![Page 13: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/13.jpg)
Conditional Statements
If, else, elseif, switch
Normally used with operators:
&& and|| or== equal! false!= not equal=== exactly equal, i.e., ‘0’, 0!== exactly not equal<, >, <=, >= greater/less than
![Page 14: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/14.jpg)
Conditional Statements
If, else, elseif, switch
...and type comparisons:
![Page 15: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/15.jpg)
If, else, elseif, switch
...and type comparisons:
empty()
is_null()
isset()
Conditional Statements
![Page 16: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/16.jpg)
Conditional Statements
![Page 17: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/17.jpg)
Printing
echo, print, prinft(), sprintf()
![Page 18: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/18.jpg)
Echo vs. Print
echo vs. print?
Simply used to display content, echo is a language construct (its built into the language).
Just use one and stick with it.
![Page 19: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/19.jpg)
Echo vs. Print
printf() – Displays content
sprintf() – Returns content
Most commonly used in translations.
![Page 20: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/20.jpg)
printf, sprintf
printf( “<strong>%s</strong>”, __(‘Name’, ‘foo’)
);
sprintf( “%$2s, <strong>%$1s</strong>”, __(‘Name’, ‘foo’), __(‘First’, ‘foo’) );
![Page 21: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/21.jpg)
Arrays
![Page 22: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/22.jpg)
Arrays
IndexedAssociativeMulti-dimensional
![Page 23: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/23.jpg)
Indexed
$pages_by_slug = array(‘about’, ‘blog’, ‘contact-me’);
How would we traverse this?
![Page 24: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/24.jpg)
$pages_by_slug = array(‘about’, ‘blog’, ‘contact-me’);
How would we traverse this?
foreach( $pages_by_slug as $page ){ echo $page; // What does this display?}
Indexed
![Page 25: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/25.jpg)
$pages_by_slug = array(‘about’, ‘blog’, ‘contact-me’);
How would we traverse this?
foreach( $pages_by_slug as $page ){ echo $page; // Displays, about, blog, contact-me}
Indexed
![Page 26: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/26.jpg)
$pages = array( ‘page_name’ => ‘Contact’, ‘page_id’ => ‘89’, ‘page_slug’ => ‘contact-me’);
How would we traverse this?
Associative
![Page 27: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/27.jpg)
$pages = array( ‘page_name’ => ‘Contact’, ‘page_id’ => ‘89’, ‘page_slug’ => ‘contact-me’);
How would we traverse this?
foreach( $pages as $page ){ echo $page[‘page_name’]; // about, blog, contact-me}
Associative
![Page 28: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/28.jpg)
$items = array( ‘pages’ => array( ‘name’ => ‘Contact’, ‘id’ => 89, ‘slug’ => contact-me ), ‘pages’ => array(... ));
How would we traverse this?
Multi-dimensional
![Page 29: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/29.jpg)
foreach( $items as $k => $v ){ echo $k . ‘ ‘ . $v[‘slug’]; // What does this display?}
Multi-dimensional
![Page 30: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/30.jpg)
How would we traverse this?
foreach( $items as $k => $v ){ echo $k . ‘ ‘ . $v[‘slug’]; // pages about...}
Multi-dimensional
![Page 31: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/31.jpg)
Loops
![Page 32: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/32.jpg)
Loops
$i = 1;while( $i <= 10 ){ echo $i++;}
![Page 33: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/33.jpg)
Loops
$i = 1;while( $i <= 10 ){ echo $i++;}
WordPress has “the_loop”
![Page 34: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/34.jpg)
Loops
<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <h1 class=”entry-title”><?php the_title(); ?></h1> <?php endwhile; ?><?php endif; ?>
![Page 35: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/35.jpg)
Functions
![Page 36: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/36.jpg)
foo( ‘name: ‘, ‘tim’ );
function foo( $a=null, $b=null ){ echo $a . ‘ ‘ . $b;}
Functions
![Page 37: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/37.jpg)
$a = array(‘name’=> ‘tim’);
function foo( $a=array() ){ extract( $a ); echo $name;}
Functions
![Page 38: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/38.jpg)
File Includes
![Page 39: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/39.jpg)
File Includes
1. require_once – fatal error2. require – fatal error3. include_once – warning error4. include – warning error
![Page 40: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/40.jpg)
File Includes
1. require_once – fatal error2. require – fatal error3. include_once – warning error4. include – warning error
WordPress has its own set of file includes
![Page 41: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/41.jpg)
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
![Page 42: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/42.jpg)
Debugging – Why do you care?
![Page 43: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/43.jpg)
Debugging – Why do you care?
“Debugging allows you to be accurate as an owl, that is approaching its
first night meal.”
![Page 44: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/44.jpg)
Types of Debugging
1. Notice – Found something that could indicate an error
2. Warning – Run-time errors, the code still works
3. Fatal – Code will not run, syntax error, memory allocation, etc.
![Page 45: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/45.jpg)
Enabling Debugging
1. Software
2. PHP config
3. wp-config.php
![Page 46: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/46.jpg)
Software
Just enable it
![Page 47: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/47.jpg)
ini_set & error_reporting
Via PHP
error_reporting(-1);
ini_set('display_errors', 'On');
![Page 48: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/48.jpg)
Via .htaccess
php_flag display_startup_errors on
php_flag display_errors on
php_flag log_errors on
php_value error_log /my/path/PHP_errors.log
ini_set & error_reporting
![Page 49: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/49.jpg)
define('WP_DEBUG', true);
Shows errors, notices, warnings, and deprecated functions.
wp-config.php
![Page 50: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/50.jpg)
define('WP_DEBUG_LOG', true);
Uses PHP’s built in error_log function. Saves your errors in /wp-
content/debug.log.
wp-config.php
![Page 51: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/51.jpg)
define('WP_DEBUG_DISPLAY', false);
Errors will not be shown, but can still be logged via define
('WP_DEBUG_LOG', true);
wp-config.php
![Page 52: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/52.jpg)
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
![Page 53: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/53.jpg)
define('SAVEQUERIES', true);
Stores queries in the global $wpdb->queries.
wp-config.php
![Page 54: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/54.jpg)
Debugging – Why do you care?
Note all of these methods should only be done in a development
environment.
![Page 55: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/55.jpg)
Conclusion
1 Obtaining PHP
2 Components – Conditional, printing, arrays (order maps), loops, file includes
3 Debugging
![Page 56: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/56.jpg)
Questions
![Page 57: Beginners guide-to-php-in-wordpress](https://reader034.fdocuments.in/reader034/viewer/2022042600/587994281a28ab95318b62fb/html5/thumbnails/57.jpg)
References
http://wordpress.org/about/stats/http://php.net/manual/en/function.error-reporting.phphttp://php.net/manual/en/function.ini-set.phphttp://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.phphttp://stackoverflow.com/questions/139427/which-coding-convention-to-follow-for-phphttp://pear.php.net/manual/en/standards.php http://stackoverflow.com/a/10057916/714202