Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fledgling WordPress Developer? Learn PHP! (Speaker Notes)

Fledgling WordPress Developer? Learn PHP! (Speaker Notes)

Many people start doing things on the web at zero. Often the thing that gets them started is WordPress. You can learn a lot from template files and doing things with existing WP functions but there comes a point where actually learning PHP will do wonders for your growth towards becoming a full fledged developer.

Peter Shackelford

August 15, 2014
Tweet

More Decks by Peter Shackelford

Other Decks in Programming

Transcript

  1. 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.
  2. For many WP is the gateway to doing things on

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

    the web. WordPress is the starting place.
  4. CC image credit girlwparasol flickr My Awesome Blog It starts

    with having a blog or perhaps a website.
  5. 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.
  6. 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.
  7. 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.
  8. The Loop <?php if ( have_posts() ) : ?> <?php

    while ( have_posts() ) : the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?> <?php endif; ?> 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.
  9. 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
  10. 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
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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.
  16. 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.
  17. $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!
  18. $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!
  19. 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.
  20. 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…
  21. 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.
  22. 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.
  23. 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.
  24. 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.
  25. Functionality Plugins I would recommend you start out building a

    functionality plugins that use things that your are learning in PHP.