Slide 1

Slide 1 text

FLEDGLING WORDPRESS DEVELOPER? LEARN PHP! CC image credit furryscalyman flickr

Slide 2

Slide 2 text

Peter Shackelford @pixelplow I am going to cover a quick overview of a path to WP that starts at 0. No programing experience, hardly any HTML and CSS. Then I will cover a few laughably basic things I figured out relating to PHP some time after my journey started. This is my story.

Slide 3

Slide 3 text

For many WP is the gateway to doing things on the web. WordPress is the starting place.

Slide 4

Slide 4 text

For many WP is the gateway to doing things on the web. WordPress is the starting place.

Slide 5

Slide 5 text

CC image credit girlwparasol flickr My Awesome Blog It starts with having a blog or perhaps a website.

Slide 6

Slide 6 text

Themes CC image credit girlwparasol flickr Many people start by picking out a theme, and that’s that. Their blog is set up and now they will focus on creating content. But there are some that want more than a generic theme offers.

Slide 7

Slide 7 text

Plugins CC image credit megpi flickr Lots also turn to plugins to enhance what their theme can do, and are pretty happy to settle with what they get.

Slide 8

Slide 8 text

Why can’t I make it do… CC image credit justjenn flickr A huge number of people first dip their toe into basic WP development by tweaking their own website because the plugin or theme does not do exactly what they want. They don’t have a background in Computer Science, they have never studied a programing language. Like me they just dive in an hack their way along.

Slide 9

Slide 9 text

The Loop CC image credit girlwparasol flickr Along the way the will discover a few basic WordPress things. Chances are they will discover “the loop” and how to do things with it.

Slide 10

Slide 10 text

http://varyhelp.com/tutorial/list-of-template-hierarchy-with-example/ Template Hierarchy Then they may discover page templates and the Template Hierarchy.

Slide 11

Slide 11 text

Magic Black Box When you learn how to make things happen with WordPress this way, it is something of a black box. I did this and it worked. I copied and pasted this in and… it worked! Yay! or I tried a heck of a lot of random things and nothing works. :( BOO

Slide 12

Slide 12 text

Magic Black Box When you learn how to make things happen with WordPress this way, it is something of a black box. I did this and it worked. I copied and pasted this in and… it worked! Yay! or I tried a heck of a lot of random things and nothing works. :( BOO

Slide 13

Slide 13 text

Elementary CC image credit dynamosquito flickr For someone who approached WordPress as a magical black box there were some elementary PHP concepts that continued to elude me. My lack of exposure to PHP outside of generic WordPress templates was also having an impact how I approached problems.

Slide 14

Slide 14 text

foreach ($colors as $color){ echo $color; } CC image credit vox flickr One example is that it took a while for it to dawn on me that what followed “as” in a foreach was a functional variable that was arbitrary. This could have been $hair or $alot. Thinking back there were a few instances where I left the functional variable as it was when I copied and pasted it because I did not know how it worked.

Slide 15

Slide 15 text

foreach ($colors as $hair){ echo $hair; } CC image credit vox flickr One example is that it took a while for it to dawn on me that what followed “as” in a foreach was a functional variable that was arbitrary. This could have been $hair or $alot. Thinking back there were a few instances where I left the functional variable as it was when I copied and pasted it because I did not know how it worked.

Slide 16

Slide 16 text

foreach ($colors as $alot){ echo $alot; } CC image credit vox flickr One example is that it took a while for it to dawn on me that what followed “as” in a foreach was a functional variable that was arbitrary. This could have been $hair or $alot. Thinking back there were a few instances where I left the functional variable as it was when I copied and pasted it because I did not know how it worked.

Slide 17

Slide 17 text

CC image credit vox flickr One example is that it took a while for it to dawn on me that what followed “as” in a foreach was a functional variable that was arbitrary. This could have been $hair or $alot. Thinking back there were a few instances where I left the functional variable as it was when I copied and pasted it because I did not know how it worked.

Slide 18

Slide 18 text

What does this mean‽ array(62) { ["page"]=> int(0) ["pagename"]=> string(13) "meet- your-rep" ["error"]=> string(0) "" ["m"]=> string(0) "" ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(13) "meet- your-rep" ["static"]=> string(0) "" ["page_id"]=> int(0)… var_dump($wp_query->query_vars); I know people use “var_dump” and “print_r” for debugging, but what does it all mean?! This won’t help you if you have not learned what an associative array is.

Slide 19

Slide 19 text

$args =array( key => value, key2 => value2, ); $args = array( 'post_type' => 'portfolio', 'post_status' => 'publish', ); Familiar? Or that when you are working with custom fields, or meta data you are dealing with key value pairs. If you dig into the WordPress DB you will find wp_posts or whatever your _posts table is called. There are columns in that table for post_type and post_status. These two column names are the “Keys” and the meta info is the “value”. Spending a little time in the DB will give you an idea of how and where data is stored and consequently what you can do with it. Having user role issues? Check user and user meta. What do you find/put in _options? Before changing anything in the DB make sure you have a backup and know how to use it. ;) Or work from a backup locally where you can make changes with impunity!

Slide 20

Slide 20 text

$args =array( key => value, key2 => value2, ); $args = array( 'post_type' => 'portfolio', 'post_status' => 'publish', ); Familiar? Or that when you are working with custom fields, or meta data you are dealing with key value pairs. If you dig into the WordPress DB you will find wp_posts or whatever your _posts table is called. There are columns in that table for post_type and post_status. These two column names are the “Keys” and the meta info is the “value”. Spending a little time in the DB will give you an idea of how and where data is stored and consequently what you can do with it. Having user role issues? Check user and user meta. What do you find/put in _options? Before changing anything in the DB make sure you have a backup and know how to use it. ;) Or work from a backup locally where you can make changes with impunity!

Slide 21

Slide 21 text

Messy elseif? Switch Case! switch ($zipcode) { case ( $zipcode >= $michigan->getZipmin() && $zipcode <= $michigan->getZipmax() ): echo "I am in Michigan!”; break; case ( $zipcode >= $ohio->getZipmin() && $zipcode <= $ohio->getZipmax() ): echo “I am in Ohio!”; break; } Check if $zipcode is in a state’s zip code range. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. I discovered Switch Case. Here is an example of using Switch Case. First I get grab the zip code. Then I check it against a “case”. In this instance I am checking if the value of the zip code is equal or greater than Michigan’s smallest zip code and equal to or less than Michigan’s largest zip code. If the case results in true the “echo “I’m in Michigan!”” code will run. If not we will move on to the next case.

Slide 22

Slide 22 text

PostMethod CC image credit jason_coleman flickr if ( $_SERVER['REQUEST_METHOD'] == ‘POST'){ //do something } I discovered POST_METHOD. If you are passing form values you can check if those have been set and have PHP serve different content or do different thing based on what is set, what the value is etc…

Slide 23

Slide 23 text

Let’s hypothesize that you are building a site for a company that has sales reps assigned to specific zip code regions. Now suppose you need to build a web interface that will show a prospective customer who their rep is? You would need a text field and a submit button. You would need to assign zip ranges to the reps… perhaps as a custom taxonomy.

Slide 24

Slide 24 text

Sales Rep Zip Code Range Data Model Let’s hypothesize that you are building a site for a company that has sales reps assigned to specific zip code regions. Now suppose you need to build a web interface that will show a prospective customer who their rep is? You would need a text field and a submit button. You would need to assign zip ranges to the reps… perhaps as a custom taxonomy.

Slide 25

Slide 25 text

zip code GO! Find your vendor Web UI Sales Rep Zip Code Range Data Model Let’s hypothesize that you are building a site for a company that has sales reps assigned to specific zip code regions. Now suppose you need to build a web interface that will show a prospective customer who their rep is? You would need a text field and a submit button. You would need to assign zip ranges to the reps… perhaps as a custom taxonomy.

Slide 26

Slide 26 text

1. Create form field. 2. Show field if $_SERVER[‘REQUEST_METHOD' != ‘POST' 3. Validate and sanitize $zipcode 4. Pass $zipcode into function 5. Get the template part if $_SERVER[‘REQUEST_METHOD' == ‘POST' General Steps General steps to handle the form side of this feature would be… Make a form field that a user can use to submit a zip code. Show the input field if there is not a Request_Method of ’POST’. to validate and sanitize your $zipcode variable. A zipcode must be an integer/whole numbers and it must have five characters. Build a function that has the arguments to get your custom post type, that are published and has the zip code in it’s taxonomy range. Get and display the post if there is a match.

Slide 27

Slide 27 text

Good old fashioned book learning

Slide 28

Slide 28 text

The Interwebs W3 Schools The Google

Slide 29

Slide 29 text

Functionality Plugins I would recommend you start out building a functionality plugins that use things that your are learning in PHP.

Slide 30

Slide 30 text

Page Templates