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.

DIY WordPress: Really Simple Plugin Admin Menus

(Reading time: 3 – 4 minutes)

Following our Technical Tuesday theme, here’s another stupendously popular WordPress Plugin mini-tutorial.

This tutorial shows how to create menu and submenu items in your left sidebar in the WordPress administration interface.

Here’s what it looks like:
Admin Menu WordPress Administration page

The blue arrow points to a spiffy menu icon, the orange highlighting to the menu title, and the green highlighting to the submenus. The name of the plugin is “Demo Admin Menu.”

First, let’s examine the structure of the the plugin.

WordPress Plugin Structure

This following listing is a pseudo-code. It looks like PHP but it’s not. It’s like PHP with all the confusing syntactical parts stripped out, and simplified functions. As you can see, all that’s left is the structure of the code, which is exactly what we want:

class demo_plugin_adminmenu {

    function demo_plugin_adminmenu() {
        add_action(admin_menu, add_demo_menu)
    }

    function add_demo_menu() {
        add_menu_page( call demo_menu_page)
        add_submenu_page( call demo_submenu_page')
    }

    function demo_menu_page() { write a web page }
    function demo_submenu_page() { write a web page }
}

Each function listed in the pseudo-code above may have one number or more arguments. The Codex documents the WordPress functions add_action, add_menu_page and add_submenu_page.

Part of the reason for this article is that the Codex doesn’t document the exact syntax for these calls, which depends on the context in which these calls are made. If the calls are made outside of a class, use the Codex version directly. If made within a class, as shown here, the callbacks will need to be wrapped as shown in the code listing next.

WordPress Admin Menu Plugin

Here’s the final code listing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
 
 
if (!class_exists("demo_plugin_adminmenu")) {
 
    class demo_plugin_adminmenu {
 
        function demo_plugin_adminmenu() {
            add_action('admin_menu', array(&$this, 'add_demo_menu'));
        }
 
 
        function add_demo_menu() {
            if (function_exists('add_menu_page')) {
 
                add_menu_page('Menu Page Title', 'Menu Title', 
				              'administrator', __FILE__, 
							  array(&$this, 'demo_menu_page'), 
							  WP_PLUGIN_URL.'/demo-plugin-adminmenu/award_star_gold_1.ico');
 
                add_submenu_page(__FILE__, 'Page Title', 'Submenu Title', 
				              'administrator', 'submenu_handle', 
							  array(&$this, 'demo_submenu_page'));
            }
        }
 
        function demo_menu_page() {    
			?>
			<div class="wrap">
			    <h2>Demo Menu Page</h2>
			    Does nothing but demonstrate menu.
			</div>
			<?php 
        }
 
        function demo_submenu_page() {
			?>
			<div class="wrap">
			    <h2>Demo Submenu Page</h2>
			    Does nothing but demonstrate submenu. 
			</div>
			<?php 
        }
    }
}
 
$wpdpd = new demo_plugin_adminmenu();
 
?>

You could copy and paste this code listing. But you would learn it better if you typed in by hand, which is what often do. Don’t forget to add the plugin header information.

WordPress Plugin Tutorials

These mini-tutorials are designed to provide a single lesson resulting in runnable code, with no extras.

Each provides a simple, standalone plugin. You can use these plugins like bricks, combining to create your own plugins.

Often, these tutorials go beyond the WordPress Codex to demonstrate new techniques which aren’t documented in the Codex.