Notes

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

Adding Plugin Config Files to WordPress

Almost everyone who has worked with WordPress has dealt with wp-config.php. I know – there are a lot of managed hosts out there that take care of a lot of this for you – but even though a person may not have directly edited the file, they have definitely interacted with it.

For those who are unfamiliar:

This file is located in the root of your WordPress file directory and contains your website’s base configuration details, such as database connection information.

Anyway, for sometime now, I’ve been working on a relatively large plugin for a client and have recently taken to introducing a similar type of configuration files. So this raised that question, do what you think of plugin config files?

Tips for Saving and Retrieving Post Meta Data

One of the common things that I’ve seen – and personally done – is mismanaging $_POST data as it comes into the server-side from a form or some type of input element from the front-end. This may be in the case of sending data via Ajax or by doing a standard page refresh.

Chill Out, Veruca. We're getting there.

Chill Out, Veruca. We’re getting there.

Whatever the case, whenever you’re dealing with $_POST data in WordPress and you’re looking to save information into the database, there may be times where you’re interested in saving empty values, and there maybe times where you’re interested in savings values only if they are not empty.

If you’re going after the latter, there are a couple of safe guards you should introduce in your code to make sure that rows are only being written when there’s data to actually be written.

Single Page Tabbed Navigation in WordPress

In a couple of recent projects, I’ve been tasked with adding tabbed navigation to various WordPress templates. The thing is, these tabs work in such a way that all of the information is loaded on the page so when the user clicks on the tab, the contents of the page appear without having to do a page refresh.

Tabbed Navigation

 

In some cases, it may be best to load pages via Ajax, in some cases, it’s better to load things up all in the first page load. This particular post is about the best strategies for that (that’s a debate for another post).

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.