Notes

Notes on programming-related problems that I’ve encountered while working on various projects.

Why You Should Write Meaningful WordPress Meta Keys

If you’re someone who’s building solutions with WordPress regardless of if they are themes, plugins, or applications, then odds are you’ve had to deal with saving some type of information to the database.

Granted, anytime you create a post, a page, or anything that stores data, you’re entering information into the database.

But that’s not what I’m talking about.

Instead, I’m talking about times when you’re having to work with the Options API, the Theme Customizer API, or one of the meta APIs (like the Post Meta API). In this case, you’re working with the core application to save and retrieve information from the database.

When doing this – especially in development and possibly in staging – it’s likely that you’re having to write the code, run a test (automated or not), and then take a look at the way the record is stored in the database to make sure it saved as expected.

In my experience, there are a handful of things that can be done to make this process a little bit easier and make your code a little bit cleaner and one of these has to do with writing meaningful WordPress Meta Keys

Adding More Fields to the WordPress Media Uploader

Last week, I shared a short post on how to include your own instance of the WordPress Media Uploader in your theme or your plugin.

Though it walks you through everything that you need, on aspect of using the instance that I described is that it’s limited. This is fine for certain situations, but there are other times where you may want to grab more information from the user.

Case in point, the post received the following comment:

Now, how do you activate (turn on) the Insert Image via URL feature of the Media Uploader?

Which I thought was a good question especially given that most of us are used to seeing more options in the media uploader than what the previous post described.

Tagging Untagged Posts in WordPress

This weekend, I shared how to setup a query to find all untagged posts in WordPress that aren’t tagged with a specific tag. In my example, I opted to use IDs (for no particular reason), but Ross also showed how you can use slugs, as well.

Anyway, there are a number of things that you can do with untagged posts – perhaps you want to remove them, perhaps you want to categorize them, or perhaps you want to apply a tag to them so that they’re stamped with a tag (like one that was not

Find Untagged Posts in WordPress

It’s common for people to categorize or tag their posts when using WordPress. This is true for the average blogger and for those who are using WordPress for its content management features.

For example, let’s say that you’re responsible for working on a site that has an RSVP custom post type and the post types can be tagged for certain types of events.

Tags may include:

  • Formal
  • Informal
  • Wedding
  • Birthday
  • Family
  • Friends
  • …and so on

Overtime, the database is going to increase in size and if RSVPs aren’t manually entered (that is, they are imported or maybe the database is even inherited and mismanaged), there’s a chance that some of the posts will not be tagged.

If you’re a developer, the odds are strong that you’re going to need to at least locate the untagged posts (and perhaps update them, as well).

Your Own Instance of the WordPress Media Uploader

One of the nicest features of WordPress 3.5 was the introduction of a refresh of the WordPress Media Uploader.

For developers who aren’t familiar with the change, the short of it is a new version of the media uploader was built using Backbone.js and Underscore.js both of which are newer JavaScript libraries that introduce a different type of structure to creating JavaScript-intense web applications.

Sometimes, one of the challenges that comes with working with any new feature is the lack of documentation around how to take advantage of it. When that happens, you’re more or less left to dig into the core source code and/or the documentation for each of the specific libraries to learn how to use them.

Writing JavaScript Helpers for the WordPress Theme Customizer

I’ve talked before about how I think the addition of the WordPress Theme Customizer (well, soon to be called the Customizer) is one of the nicest additions to the core application in a long time. I’ve also talked about how I fear the direction that some developers will take it.

Regardless, if you’ve worked with the Customizer long enough – especially when it comes to the JavaScript aspect of it – then you’ve likely noticed that you can end up writing relatively repetitive code especially when you’re working with something such as a list of items or something similar.

When you end up reaching this particular point, having too much of the same code with the only variations being a few strings ends up being a bit of a code smell.

In order to prevent this, it’s often better to write helper functions that abstracts the repetitive functionality into a single, ahem, function that you can call with the strings that are unique to your use case.

Here’s what I mean.

Easily Install phpDocumentor Alongside MAMP Pro

In previous posts, I’ve talked about the WordPress Coding Standards, and the importance of documenting your code, but I’ve not actually spent a lot of time discussing how easy it is to actually generate documentation for your themes.

And by documentation, I mean an actual site that provides your DocBlocks such in a clean and organized fashion – you know, sites that are generated by tools like phpDocumentor.

phpDocumentor Example

An example of a site generated by phpDocumentor.

In other posts, I’ve mentioned that I use MAMP Pro for part of my development stack, so if you’re looking for steps to install phpDocumentor, it’s actually really easy to do.

Including a Template in a WordPress Plugin (Well, a Template Part)

Late last year, I wrote a post that provided a way on how to include a page template in a WordPress plugin. There’s an accompanying project on GitHub that’s been maintained and relatively-well updated since.

Although this post is similar in nature, it doesn’t exactly deal with templates, but parts of code that may be considered partials (or template parts, in WordPress).

Let’s say that you’ve got a single post and you want to append a template to the end of the content. The content can be a little more complicated that markup because that’s easy enough to do inline, isn’t it?

So, for all intents and purposes, let’s say that we have a partial that includes a form that can be used to submit some type of information.

Simplifying Code in WordPress: Option Arrays

Let’s say that you’re working on a plugin that has its own plugin settings page, and on that page there are options to determine what type of posts will support part of the functionality of the plugin.

For example, let’s say this plugin will be introducing a meta box for each post type that extends the type of information that the user can add to a post. The settings page allows you to control which post types will offer this information.

To give a concrete example, take a look at the following screenshot:

Simplifying Code: Options Arrays

Granted, it’s a small example but it makes the point: We have a plugin settings page that displays all of the post types that are in the current theme installation. If selected, we’ll save the values to an options array.

When it comes time to read the values, there are a couple of ways to go about doing it, but one that’s arguably simpler than all of the rest.

Adding Tabbed Navigation in WordPress for Custom Menus

One of the nicest features of the latter versions of WordPress includes the custom menu system. Although people can always introduce too many areas in which custom menus can be introduced, the core feature and customization options make it possible to do some really cool stuff with custom menus.

Case in point: With many of the popular front end frameworks that are now available, such as Foundation and Bootstrap, it’s really easy to add tabbed navigation in WordPress in templates, widgets, and so on.

Though there are a number ways of to do this, one flexible way that I’ve used multiple times requires two things:

  1. A function for retrieving the post IDs for the post types contained in a custom menu
  2. An instance of WP_Query

At that point, all you need is the name of the menu for which you want to retrieve the post IDs.