Software Engineering in WordPress, PHP, and Backend Development

Tag: WordPress (Page 27 of 219)

Articles, tips, and resources for WordPress-based development.

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

WordPress Taxonomies: Terms, Parents, Children

Programmatically creating taxonomies seems to be a point that comes up now and again for those who are building solutions for others on WordPress.

Taxonomies themselves can even be a little confusing; however, I’ve found that the following usually helps to solidify the concept a bit:

Hierarchical taxonomies are analogous to categories; non-hierarchical taxonomies are analogous to tags.

But still, let’s say that you’re creating a solution for someone such that you need to import information as a post and apply a taxonomy to it. Further, perhaps you want to apply a parent taxonomy to the post, as well.

How can we do that?

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑