Need a WordPress website this weekend? Start here...

DIY WordPress: What You Need to Know About PHP if You Don’t Yet Know a Damn Thing

(Reading time: 6 – 9 minutes)

WordPress can be considered a dynamic web application. What that means depends a little bit on who you talk to, and what you are using WordPress to achieve. (Look for related reading at the end of this article.)

What “dynamic web application” means for this article is a web page resulting from an unholy fusion of HTML, CSS, PHP and MySQL. And Javascript. Can’t forget Javascript.

5 different langguages. That’s crazy. Seriously, it’s crazy. Back in the dinosaur days (1990s), it was plenty to master one programming language per application.* You could even spend most of a career just in one or a few languages.

Anyway, if you’re still with me, you probably know a little bit about HTML and CSS by now. It turns out that for WordPress, you don’t need a lot of Javascript or MySQL for tweaking themes and plugins. Helpful, they are, but not necessary.

What you need now is a little PHP, so that’s where we’re going next.

“Why should I care about PHP?”

You continue, despite your better judgement, to read Website In A Weekend on a regular basis. Which means by hook or by crook, you’re at least partly in the do-it-yourself camp. You may not want to camp in DIY, but there you are. So you should care because if you don’t, you’re gonna mess something up sooner or later.

Sidenote: If you whitescreen WordPress, that is, all of sudden, the only thing displayed is a blank white page, you almost 100% surely have a syntax error in your PHP code, and that syntax error is almost surely a missing semi-colon “;”.

And it won’t be pleasant.

So that’s why you should care.

WTH is PHP, anyway?

PHP is a recursive acronym, meaning PHP Hypertext Preprocessor. For more on recursive acronyms, I suggest Wikipedia.

PHP is a programming language that slides in and around HTML to do a lot of tedious dirty work so you don’t have to. PHP code intermingled with HTML often appears ugly. In programming, something appearing ugly often is ugly. One of the weaknesses of PHP is that some of this ugliness can’t be helped. It was designed in.

PHP is a module that plugs into your web server that translates (preprocesses) PHP files into HTML (hypertext). The PHP interpreter is different than the web server. You can run a web server without PHP, but you can’t serve PHP over the web without a web server.

PHP is all that and more!

PHP Hypertext Preprocessor

From the above, PHP the programming language is what’s of most concern to you, right here and right now. The language is what you’re going to see when you’re poking around in WordPress. Here’s the basic concepts.

  • PHP language syntax. PHP syntax derives from the family of C like languages. If you know already know C, you will pick up PHP pretty quick (but you probably already know that). If you don’t know C, no worries, it’s not that important. The point is, there’s history and meaning behind PHP syntax. It didn’t sprout from someone’s forehead, ex nihilo.

    Current versions of PHP are “object-oriented.” But really, PHP is procedural, object-oriented capability was bolted on, not designed in.

  • Understand variable scope. Understanding variable scope is critical. If you have programmed before, you may need to unlearn some assumptions, like global variables not acting how you think they should. Variable scope in PHP still trips me up. If you have little programming experience, you won’t have to unlearn anything. That’s a good thing.
  • PHP paths are critical. When you write an application or even tweak a plugin for WordPress, you need to know how WordPress and PHP find things like files and images you want to deliver to your readers. WordPress does a pretty good job of standardizing paths. Spend the time to figure that out.
  • Understand single and double quoting. Single and double quoting in PHP behave slightly differently, but these small differences are important. Sometimes single and double quotes can be used the same way, other times, they can’t be used the same way. Most people develop their own scheme for when and how to use each type of quoting.
  • WordPress and PHP APIs are different. A collection (library) of functions is called an Application Programming Interface (API). When you’re just starting out, it’s critical to figure out the difference between functions that are part of PHP and functions that are part of WordPress.

Here’s four functions you absolutely must know for effectively programming in WordPress. I suggest either memorizing these functions (smart) or have their definitions on speed dial (not as good, but it’s how I do it).

  • dirname: returns the directory path to the file given as argument. This is the directory on the server, not the URL path. PHP function.
  • basename: returns just the file name for a given path. Dual to dirname. PHP function.
  • add_action: adds a specified function to a WordPress action. Example: if you want to print your own meta tags into your web page head element, create a function and use add_action to tell WordPress where to print the output of your function. See Line 20 in Anchor Text Plugin.WordPress function.
  • add_filter: modifies text before printing that text to a WordPress page. For example, WordPress uses a filter to automatically add paragraph breaks to your blog posts. (Which some people really hate, but what can you do?). WordPress function.

Low Mercury from Groovera Chilled Web RadioBy the way, I just got a nice email from Tim Lumen, who operates Groovera.com. Curiously, I just happen to be listening to Low Mercury right at this very moment. Low Mercury is super-chill, great for those days when Trance, Techno and Breakbeats is just a little too much. If you’re in a kick back writing mode, check ‘em out. Groovera is 100% listener supported, and worth it. If you like what you hear, donate or subscribe. If you’re in budget mode, give Tim a little link love. Or both!

Related reading

Here’s some articles you might like from 2009:


*Make and cpp don’t count. La la la I can’t hear you. Never let facts interfere with a good story.

Comments

  1. Sean Neprud says:

    Nice little overview.

    PHP was the most difficult to get familiar with (of the languages that WordPress uses), but it has also been the most useful, especially as I got more and more into creating and hacking up my own themes.
    Sean Neprud´s last post ..Druid Arch Woodblock Print in Process

    • Dave Doolin says:

      I suspect I would vastly prefer Ruby on Rails or Django (Python based) for web application programming. Based on the little I’ve worked with both, they are far cleaner. PHP is just so… messy. One semi-colon off and the app crashes? That’s not very good engineering.

      Then again, PHP started as “Personal Home Page,” not as a way to rewrite the interwebs. So I’m cool with PHP and all it’s wartiness.
      Dave Doolin´s last post ..Exchange

  2. Ralph says:

    Ok, so I know something about PHP but where do I use it and why do I care.
    Ralph´s last post ..How being certain you are right can cause a big problem

  3. Shirls says:

    Dave – help. The internet is eating all my comments on every site I try. It used to work on your site, if this gets vanished I’ll email you.

  4. Dave Doolin says:

Trackbacks

  1. [...] with drag-and-drop design is also an option.As you get more familiar with CSS, HTML, JavaScript and PHP (and maybe a litte bit of SQL), you can achieve more control over your site. Many web designers can [...]