Software Engineering in WordPress, PHP, and Backend Development

Category: Notes (Page 27 of 49)

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

An Example of How To Remove Empty HTML Tags

One of the most tedious aspects of building WordPress themes is customizing and styling the comments template. This includes not only the comment form and the pingbacks, but the response text, as well.

Don’t get me wrong: It could be worse, and after you’ve done it a few times, it’s likely that you’re going to use many of the same strategies that you’ve used in previous themes or templates.

But there are examples in which certain elements will render as empty HTML tags. If you have given those tags a specific, say, background style then it can really create somewhat of an ugly experience for your readers.

The challenge, then, comes at being able to remove empty elements before the user can see them. But there’s a catch: It can’t be done on the server side because the server side sees the HTML as you would expect it to be rendered whereas the browsers take the liberty of parsing the document and adjusting the markup so that it’s a bit more semantic.

At least that’s what most of them try to do.

Anyway, this can cause some unintended side-effects.

Continue reading

Using WP_Query To Get Posts From Last Week

One of the most powerful aspects of the WordPress API is WP_Query as it allows us to retrieve a wide variety of content through a relatively simple interface.

That is, it allows us to retrieve complicated datasets ranging from custom post types, post status, date ranges, taxonomies, meta data, categories, and so on all with a relatively standard set of PHP constructs (mainly arrays) and with a really solid reference in the associated Codex article.

One of the nicest things about WP_Queryespecially since WordPress 3.7, are the advanced date query parameters that we can use in order to retrieve information from a specific date range without having to do a lot of complicated date math (which, of course, is every programmer’s favorite aspect of development).

If you learn how to use that particular aspect of WP_Query and you’re familiar with PHP’s strtotime function, then you can retrieve posts from a range of dates easily.

Continue reading

A Quick Fix For Google Web Font Rendering

Like many, Chrome is my browser of choice, but the past couple of updates have resulted in some problems specifically with Google web font rendering (as well as some other web font rendering).

I had considered linking to a number of different issues that I had seen in bug trackers, but you can see just how popular this particular issue has gotten by performing a quick Google search.

To be honest, I’m not sure if this is relegated only to Chrome or to all WebKit-based browsers.

Anyway, since Chrome normally does silent version updates in the background, this should be fixed in an upcoming version – last I checked, it was resolved in the latest build of Chromium – but until then, I thought it may be worth sharing one way to fix the issue.
Continue reading

A Practical Example of The WordPress Heartbeat API

The WordPress Heartbeat API is not exactly a new feature. That is, it was first released in WordPress 3.6. Maybe I’ve just done a poor job of keeping up with what others are doing with the API, or maybe I’ve just not paid attention to some of the stuff that people are doing with the API, but I haven’t seen that many examples other than how to get started with it.

On top of that, the Heartbeat API codex is lacking in documentation (though this is an open source project, so if anyone’s to blame, it’s all of us), so perhaps that’s a reason that few people have begun to implement it into their projects.

Whatever the reason, I thought it may be worth showing a practical example of how I recently implemented it within a theme I’m working on, and how I’m using it within conjunction with the Theme Customizer so that when an administrator makes a change to the theme using the Theme Customizer, the visitor will automatically (or is it automagically?) see the changes.

Continue reading

How to Sort an Array by Locale in PHP

As easy as it is to translate content within WordPress, there are  times in which some of the content in the database may be different than the original locale of the site.

Say, for example, you’re running a site that includes a list of names in a flavor German when your server (or even perhaps your site) is based on an English locale. The goal is to read the names into a collection – rather, an array – and then sort them alphabetically.

Obviously, different locales have different alphabets so what works in, say, the United States won’t necessarily work in Germany.

There are a couple of ways to go about handling this, but the easiest way that I’ve found is to use PHP’s Collator class.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑