Category Archives: PHP

In addition to PHP, this topic covers a fair amount of MySQL, as well as some *nix shell scripting.

Patrons at Mathew's Pub in Portland, ME on April 4, 2014. © Ben McCanna via Flickr. All rights reserved, used by permission.

How To: Use Transients To Speed Up WordPress Templates

Background on how caching works in WordPress, and when it makes sense to use transients, is in the introduction to this post, Using WordPress Transients To Speed Up Your PHP-Heavy Templates: Introduction.

WordPress transients are a simple way to store markup in the WordPress database for short periods of time.

In effect, transients are a kind of mini-cache. They allow us to store finished markup, generated by a template, that’s computationally expensive to create — such as a custom WP Query.

“Transients” are called that because they eventually expire. When some period of time is up (or we intentionally replace or expire the transient), WordPress can rebuild that transient and store it again for some period of time.

This has the net effect of speeding up page load times. Rather than the template having to repeatedly crunch the same code, over and over, to produce the same chunk of markup, the webserver does it once — and serves up that saved markup to all subsequent users.

That’s a lot easier on the server, so it makes pages load faster and reduces errors that can be created when a webserver is under stress.

Continue reading

Dusk over the highway

Using WordPress Transients To Speed Up Your PHP-Heavy Templates: Introduction

This post serves as background to the post, “How To: Use Transients To Speed Up WordPress Templates.”

WordPress is a powerful content management system, and over the years its ability to render very complex data in pleasing, human- and machine-readable formats has been greatly improved.

Thanks to changes in the taxonomy model, as well as improvements in the core functions that surround templates / themes, a designer doesn’t run into nearly as many cases these days of having to run lots of PHP code in a template file.

Even so, sometimes you just can’t avoid having to use a template file to run a complex WP Query, or otherwise put together a bunch of PHP to get something onto your WordPress-based site.

As you’ve no doubt discovered, depending on how code-heavy your template proves, and what resources your Web server has, this can lead to very long processing times, meaning very slow page loads.

Worse, on-template PHP code often can’t be cached, especially if it isn’t centered around a single post / page or a particular taxonomy term.

Continue reading

Beach To Beacon 10K finish line by Ben McCanna. All rights reserved, used by permission.

Make Your First WordPress Plugin: A WordCamp Maine 2014 Presentation

This post is a companion to my WordCamp Maine 2014 talk, “Make Your First WordPress Plugin.”

It’s effectively a recap of an earlier post, Making A Simple WordPress Plugin, with elements from Hooking WordPress Taxonomy Changes With The Plugins API.

If you know how to make, or hack, a WordPress theme, you have all the skills you need to write a plugin.

All it takes is a little knowledge of how to write PHP, a basic understanding of how WordPress goes about making Web pages, and a little trial and error. Oh, and the same, basic tools you use to make any website:

A decent text editor is the only “must have.”

For Macintosh, TextWrangler will work fine; I prefer its full-featured sibling, BBEdit. Protip: Download these directly from BareBones; the Apple Store versions don’t support all features and can make upgrading difficult.

For PC, I use Notepad++; others swear by Sublime Text.

A Web development IDE, such as Dreamweaver, WebMatrix or even Visual Studio will work, too; although these tools, in their zeal to make things easier, can significantly complicate writing plugin code.

Regardless of whether you use a Mac or PC, don’t use the built-in text editors (TextEdit and Notepad, respectively); they tend to introduce file encoding that can prove problematic when trying to write plugins.

Optionally, it’s good to have an FTP program. While this isn’t strictly necessary, it makes salvaging a plugin installation that has gone horribly wrong a lot easier. If you’re an OS guru you can, of course, just use your Web browser for FTP, or maybe install an FTP plugin; but I prefer to use FileZilla on PC and Transmit on Mac.
Continue reading

These are the properties of a WordPress post. Source: WordPress Codex, XML-RPC WordPress API/Posts

Using The WordPress XML-RPC API With PHP: The Post Model’s Properties

Part 4 in a series on working with the WordPress XML-RPC API with PHP.

Previously I discussed the reasons for creating a data model (aka a class), that looks like a WordPress post, in order to streamline and simplify working with the WordPress XML-RPC API.

In this post, we’ll create the properties, or data, that goes into that model. This will help us make heads and tails out of what we need to send to the XML-RPC API, and what we’ll get back, when working with posts.
Continue reading

Hooking WordPress Taxonomy Changes With The Plugins API

Captain Hook twirls his moustache at Breakfast in the Park with Minnie & Friends at the Plaza Inn by Loren Javier, on Flickr

So you’ve developed a plugin, or a theme function, in WordPress that needs to know when a taxonomy term — in a category, tag or custom taxonomy — is added, edited or deleted.

Unfortunately, the WordPress Codex barely mentions how to do this, and the examples you see out there on the Web basically regurgitate the same odd instructions, which boil down to passing in some integer parameters that can’t possibly be correct for every example.

Like this:

add_action('create_stores', 'my_create_stores', 10, 1);

That’s unfortunate. So let’s clear up the mystery of how WordPress notifies plugins and theme functions of a taxonomy change, and how we can go about writing a sensible treatment for those events.
Continue reading