Software Engineering in WordPress, PHP, and Backend Development

Author: Tom (Page 57 of 428)

WordPress Class Serialization with PHP

If you’re used to working with models (in any foundation or framework, but specifically WordPress), then there’s a chance that you may need to serialize an instance of the model at some point.

Sure, writing the class to a database using PHP’s built-in functions is easy enough; however, introducing a bit of flexibility especially as it relates to making it available on other platforms is important.

For example, let’s say you’re building an application on WordPress that’s going to have some type of unique piece of information represented in a model. The model will then be accessible via a mobile application through the REST API.

WordPress Class Serialization: PHP

Arguably, one of the easiest ways to get this done is to use JSON. It’s a format that works across various languages and platforms, can be easily serialized and de-serialized by said platforms, and sent across the wire as needed.

And it’s incredibly easy to implement this in PHP. You just need to make sure your class implements the JsonSerializable interface.

WordPress Class Serialization: PHP

From the documentation, the interface does the following:

Objects implementing JsonSerializable can customize their JSON representation when encoded with json_encode().

The only method a class needs to provide is jsonSerialize, and though it’s likely you will want to serialize all of the properties of an object (as well as its state whenever its called), you can customize the implementation however you’d like.

Continue reading

WordPress Widgets: Refactoring, Part 11

In the previous post, we walked through a lot of refactoring that separated concerns into their own classes.

Ultimately, this helps show how we can maintain a high level of cohesion while not only working with classes in WordPress but doing so alongside pre-existing APIs.

Because the last few posts on refactoring the code base have been so long, the current set of posts are focused on small, incremental changes and thus shorter, more focused posts.

As mentioned in the previous article:

But if you refresh the page, you may notice that the sanitization and serialization do not seem to work when retrieving the data. And that’s what we’re going to look into in the next post.

So that’s where we’re going to pick up in this article.

Continue reading

Adding Custom WordPress Plugin Links

If you’ve ever built a custom plugin for yourself or for someone else, then you’ve likely done something with the WordPress plugin links even if it’s just providing author information a URL to the homepage for the plugin.

And you know what I’m talking about: These are the links that appear below the name of the plugin.

WordPress Custom Plugin Links

According to the code reference, this information is:

The plugin’s metadata, including the version, author, author URI, and plugin URI.

Occasionally, though, you may find that you want to add or modify the links. That is, you can add your own custom links to appear in the list below.

Continue reading

Shorter Blog Posts, Revisited

A little over three years ago, I wrote a post about starting to write shorter blog posts. And in that particular posts, I made two comments that I think are still relevant but that I’ve gotten away from doing.

First, I wrote:

Because the truth is with the amount of information coming from other blogs, Twitter, and whatever other social networks and news sources you read, odds are that this site is one that can also be easily marked as read or thrown into Pocket oblivion never to be read again.

And I also wrote:

Secondly, it’ll aim to be more to the point than anything else. The idea being that you can load up the article, read through it quickly, save whatever information you’d like (or not), and then move on to whatever’s in your queue.

I don’t know if old habits die hard or if I’ve simply gotten away from it and back to writing longer form writing because that’s how I tend to write.

Shorter Blog Posts: Revisited

But I don’t want all of my posts to be that way.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑