Software Engineering in WordPress, PHP, and Backend Development

Category: Notes (Page 22 of 49)

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

Using jQuery To Set Select2 Selected Value

As much as I firmly believe in making sure that anything we build for WordPress especially as it relates to the dashboard should remain as consistent as possible.

As with anything, there are a few exceptions that I’ve made in the work specifically when it revolves around large select elements (multiselect or no).  That is, I’m a big fan of Select2 – I’ve written about it a couple of times and how I’ve used it in a couple of projects.

Because this is something that I regularly use, and because I know a number of WordPress developers (and general web developers, as well) also use this in their work – both in the dashboard functionality and in the public-facing functionality, as well – I wanted to share one way in which I’ve needed to programmatically set an option.

Continue reading

Display an Error if a File Is Too Large for WordPress

If you’re working on a project for WordPress that’s going to allow users to upload files – be it a video, an image, a CSV, or any other type of data – then you’re likely going to be faced with a situation where you’re going to need to determine if a file is too large for WordPress.

Yeah, it's a little too large.

Yeah, it’s a little too large.

What’s considered “too large” can be related to any number of factors:

  • The size of the file is larger than you want to accept (or the file system accepts)
  • A PHP timeout occurs when uploading a file because of its size
  • The file system doesn’t have enough space
  • …and so on.

Whatever the case may be, there are two things that you’re going to need to be able to do:

  1. Determine if the file fits within constraint of the system (whatever the constraint is)
  2. Display an error message to the user before the upload occurs

It doesn’t exactly provide for a stellar experience when trying to upload something only to have it rejected by the server without a proper error message, does it?

Continue reading

Resolving PHP Timeouts in WordPress

When it comes to working with long running scripts and WordPress, you’re usually at the mercy of one of two things:

  1. PHP configuration file
  2. The server’s PHP configuration

Granted, the case could be made that these are one and the same, and in a sense they are, but if you’re working with PHP on your local machine, you clearly have more control over the environment than when you’re working on a web server.

Technically, if you’re working on a dedicated server, you should have full control over the configuration of the environment.

If that’s not the case, this is article won’t be of much use; however, if you’re in the business of working with PHP scripts on your local machine and a shared server, and you’ve hit the maximum execution timeout message, then there are a few of ways to go about handling the problem.

Continue reading

Upload Files to the WordPress Media Library

Programmatically uploading files to WordPress is really just the same as uploading files from any source location to a destination.

That is to say, there are a number of PHP functions all of which make it pretty easy to deal with file-level operations, grabbing files from one location, and moving them to the next.

And yes, there are some nuances that can come with PHP’s configuration such that you may not be able to write to certain directories, perform certain options via HTTP, and so on. All of these can be managed by either changing up the configuration or by changing the way in which files are handled by the code.

The WordPress Media Library

One thing that WordPress offers that manual uploads don’t manage is adding files – specifically media types – to the Media Library after uploading a file. This is relatively easy to do given media_sideload_image.

But let’s say the situation is a little more complex.

Continue reading

Programmatically Add Multiple Post Terms in WordPress

A couple of weeks ago, I shared a simple gist for how to programmatically add post terms in WordPress. If you’ve read the series on importing CSV files into WordPress, then you’re likely to encounter something like the following scenario:

Given a CSV, apply multiple terms to a single post when the terms are delimited by another character.

So, for example, let’s say that you have a CSV and each value is, naturally, separated by a comma. Within one of the columns, words – or terms, in our case – are delimited by semicolons. Each value that precedes a semicolon represents a term (related to any given taxonomy in the system – this is irrelevant for this particular post).

Adding multiple terms to a post, or post type, is relatively simple and can be based off the functionality already shared.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑