Thursday, June 25, 2026

WordPress Null Handling Confusion in Custom Fields

I recently built a WordPress plugin that was going to handle custom fields. The functionality was similar to the plugin ACF (Advanced Custom Fields). However, I struggled with understanding how the null handling was made.
After some discussions with ChatGPT it seemed that WordPress treated null as "nothing existing in the database".
To complicate things further, I also used the postmeta table in WordPress which has a EAV type structure, which is used to create a flexible way of adding custom fields.
The structure of the table is: meta_id, post_id, meta_key and meta_value, where meta_key are the name of a custom field added (a so called meta field) and meta_value is the value the user puts in that field.
Simple enough.

So by going by the way, allegedly, WordPress sees null, if nothing is stored in the custom field, neither meta_key nor meta_value is stored and WordPress reads this as null.

This was my initial understanding of null in WordPress and I figured I would adhere to this logic. However, when saving from the Gutenberg editor and not from the front-end posting, a discrepancy was noted.
When posts where submitted from the front-end, it behaved like a WordPress "null": neither meta_key or meta_value was saved, if nothing had been put in that custom field.
However, from Gutenberg, when saving posts there it would save a meta_key and nothing in the meta_value field. The value was however not a database null, but likely an empty string.

This was the behavior I noticed in ACF.
So in the case of a post where many custom fields had been added, when saving from Gutenberg it saved multiple empty fields in the database. After some research I figured that was mostly a space concern which may not be so important, but as it had some type of exponential growth it could easily start to fill up this table with many empty values.

That didn't seem great but as I was writing code with some requirements, I simply followed the logic I thought was necessary. But I also thought, intuitively that maybe deleting both meta_key and meta_value would be a good thing and created a "nullable option" for the custom fields.

This followed the WordPress null logic, where nothing in the database meant "nullable => true" and when keeping meta_key:s, then "nullable => false".

The problem was that this caused some confusion, both for myself and the team that were using the code.

After an extra check, I realized that this was not how ACF handled it when their nullable option was set ("allow null"). To ACF "allow null" meant rather converting empty strings to null values in PHP. And when stored in the database, null was converted to empty string.

Something like this simple schematic:

Database <- '' <- null 
'' -> null -> PHP

Now, ACF used their own function to handle this feature, as a layer in PHP. It was called get_field().
It turns out WordPress has its own function that gives the same result: get_post_meta()
where the null handling however, looks a little bit different:

When using

get_post_meta($post_id, 'fieldname' , true) it returns the first value in a array of values: [value*]

and when using
get_post_meta($post_id, 'fieldname' , false) it returns all of the values in a array of values: [value1, value2]

* value = for example: 1, or "string"

So when the field is empty, it will return [] (nullable, or null)
or [''] when the field contains an empty string ('')

(Whereas ACF seem to do (convert): [value] => value, [''] = null, [] = null.)

Now this functionality wasn't requested and it turned out that deleting both meta_key and meta_value was the requested behavior, hence my code worked using "nullable => true".
But of course, the confusion prevailed until I changed the name of the option from nullable to "remove_when_empty".

Semantically this made a lot more sense and since null handling wasn't requested, no further adjustments needed to be done at that point.

What can also be added, however, is that when creating these options, I had to handle the empty values, and normalize them to empty strings (' ') if no value was added.

When I write this writeup I realize it is still somewhat confusing and by writing this text I try to create some clarity also for myself, besides telling a story about this mysterious null issue.

Maybe it is not so mysterious but I think that I begin to understand the issues that has been raised about "null hell". In this case, I think it stemmed from a question of definition. What is null, really?
How should null be defined?

Going back to theory, it could be conceived as:  null => 'nothing' was ever inserted at all,
while empty string (' '), or ('') means the same as 0.
The difference could for example be a user field, where either "no users has been added", or "users has been added, but was removed".

It is a slight difference in these cases but enough to create a lot of confusion.

tl; dr: I implemented a null that wasn't null, so eventually the function was renamed to remove_when_empty.

Front-ends and SPA-functionality on WordPress

I have for the last couple of weeks been exploring some techniques for creating a SPA type front-end using WordPress as the backend.

Althought WordPress is a stable and trustworthy CMS system, one thing that may be a bit old school is the way the CMS works with regards to page reloads. Since everything is rendered on the server, using PHP, every time a page needs to be updated, a page reload is required.

However, using JavaScript and AJAX, it is possible to achieve a Single Page Application (SPA) type front-end.

My first idea was simply to just use WordPress as a backend, using the REST-API that is built in and not worry about trying to use the front-end part of WordPress: working with the theme in wp-content.

The setup is basically that you use WordPress as a backend server and configure the web server so that WordPress is not on port 80.

Instead you create a standalone front-end in your preferred front-end framework – my choice was React and then you use the REST-API endpoints from your WordPress “bare metal” back-end server.

This worked quite well, but one thing that became obvious was that you would also need to handle the authorization and authentication as well. At least if you wanted user system functionality.

You could keep the front-end and “admin system”, using the login /wp-login.php or /wp-admin for administrative use and only use material from WordPress that is considered “public”.

This can be good enough for certain purposes but if you want users to be able to login and so on (and not login using the above mentioned wp-admin area), this might not be ideal.

However, I quite early on this attempt and it could be interesting to explore it a bit further at a later point.

My second attempt was to work with the theme and not a stand alone front-end. Instead, I opted for using some JavaScript/AJAX technology. Some possible options would include vanilla JavaScript for fetching JSON, jQuery AJAX/fetch or HTMX.

I decided to try HTMX as I had not used it before… And it turns out it was quite simple to implement and I got some nice results, being able to swap out parts of the DOM.

This worked well for certain type of material but I soon ran into some difficulties with state management. For example, to keep track of “back link URL:s”, turned out the be more troublesome than I first expected. However, a similar problem had occurred without HTMX so maybe it is JavaScript that introduces such issues.

I got my custom post type “works” to function quite well with HTMX, which made it possible to filter type of work, that was categorized using a custom taxonomy. Left to to do is the mentioned back links, so that you get back to filtered results. Another thing that needs to be solved is pagination, and pagination for filtered results.

But for now, it works quite well and is not over-engineered nor underdeveloped, in my humble opinion… But of course, every time one develops features like this, one runs into the risk of reinventing the wheel. Which I guess adds to the *fun* of web development.

To conclude, this is not my final say in this matter, but some reflections on my progress. Hopefully I will be able to come back to this topic at some point, perhaps when I have gotten some more experience with this type of development.

Friday, March 6, 2026

The State of WordPress and Perhaps Writing a Book About It

I have been preoccupied with certain features of WordPress and building plugins lately. It has been good knowledge to pick up and interesting, but I have not had so much time to work on my WordPress theme, nor on my portfolio WordPress "themed" website. Then, I haven't had time to do preparations for my bachelor's thesis neither.

So the list is probably:
  • WordPress plugin work
  • WordPress theme work
  • Portfolio site and features
  • Bachelor's thesis preparation
Which I consider my main items of attention at the moment. During some downtime I started to do a project in Laravel, and that is also an item that can be added to the "should do more of when I get around to it".

But lately I have been thinking about what I want to focus more on. There are some stuff I thought I would do related to my Portfolio site but haven't started. I thought I would create a small web shop type thing with possibility to subscribe through Stripe.

Of course, it would mostly be an exercise at this point, but potentially a real world project. One problem though is that I would need to create some material for the subscribers. It wouldn't be much of a subscription service otherwise.

So I'm thinking about what I should add. And as of recently my mind has gone to writing some sort of WordPress book. I have already posted about a few WordPress related topics on this blog but the idea would probably be to expand on those and write some more from my experiences with this classic CMS.

I don't think it would be a introduction type book "Learning WordPress for Beginners" and bring up too basic topics, because I think that is covered a lot elsewhere. But rather, a book where I try to expand on a few topics, so it hopefully can be an interesting read for someone who is already somewhat familiar with WordPress.

The question now is of course if there would be a market for a book like that and if WordPress is worthwhile to write about in 2026 still. Personally, I would say that those "cookbook" type programming books can usually be good for advanced beginners, intermediate and even expert users. And regarding the topic itself, I think WordPress is still relevant.

The reason for it being relevant could be a few things, like:
  • Still being used for a lot of websites
  • A "standard" type of website that is flexible and possible to tailor to the needs of the user
  • Lots of plugins for various purposes helps in that regard
  • A good content system that allows the users to easily change the content of the website without having any particularly advanced computer skills

I would also reason it is a good choice for a beginner developer who wants to use traditional web technologies such as PHP, SQL, HTML, JavaScript/jQuery and CSS. Sure it is not the latest and greatest, but it works and is a nice way to repeat some of that knowledge you probably already know.

Then, you can even advance into more difficult areas where WordPress is possible to adapt, such as the REST-API endpoints, writing Gutenberg blocks in React and more. You can also learn how to setup things like e-mail and CAPTCHAs. 

A lot of features that you might think should be easy to have on a website, you will realize usually demands a lot of code. A simple way to achieve those features without having to reinvent the wheel can be to use third party plugins. Often they are free, but can also have premium features. And eventually you might find out that you are missing something in the plugins that are available and then it's a good opportunity to create it yourself.

However, why I think WordPress is still relevant is mainly because it's a good content management system, as in a good system to store content: from blog posts, html pages and images, to custom post types with custom meta fields, making it possible to adapt the system to practically *anything* you want it to represent.

There is also a good taxonomy system for categorizing the data, using both hierarchical and nonhierarchical taxonomies.
Last but not least, there are good opportunities to create a nice front-end that is SEO compatible, because it uses simple server rendered technology. While it's not a Single Page Application typ website, if that is the requirement, headless WordPress can be used for back-end and the front-end framework of your choice as front-end.

This can serve as a good start before moving on to more complex and dedicated back-end framworks.

It's always difficult to say what the future may hold. Especially at times like these where AI builders and other sites and software becomes available for creating websites but WordPress has survived longer than people might have expected and I don't think it's popularity will decrease for a particular group of people, who enjoy the option of being able to easily modify the system.

Code preview

In this post I'm trying out to add "code" text block, for better overview of code.

This might work...   
  Your code here

Or this... using: https://codeformatter.blogspot.com/
 function hello() {  
 echo "world";  
 }  

Another option seem to be to embed gists from Github.

No syntax hightlighting though... to be continue.

Monday, March 2, 2026

The Difference Between Hierarchical and Nonhierarchical Taxonomies in WordPress

The Difference Between Hierarchical and Nonhierarchical Taxonomies in WordPress with regards to slugs and public (frontpage) access.

I'm mainly writing this as a reminder for myself but this is how I believe it is organized and I believe it could be helpful if someone ever run into the same issue.

So the main different between hierarchical and nonhierarchical taxonomies in WordPress is that hierarchical taxonomies are the typical "category" taxonomy and nonhierarchical taxonomy is a "tag" taxonomy.

When you set 'hierarchical' => 'true' or 'false' in the taxonomy settings, you define this behavior which also impacts the access to the taxonomy on the front-end of your WordPress site.

For hierarchical taxonomies, like categories, you may name it like such:

'custom-category'.

Now you can access it: example.com/custom-category

And the template for this is archive-custom-category.php in your theme folder.

The template for the single is single-custom-category.php.


Now you could assume, it is the same way for the nonhierarchical taxonomy.

Lets say you have a nonhierarchical taxonomy called "custom-tag".

But trying to access example.com/custom-tag/ will yield a 404.

Why? Because the taxonomy is nonhierarchical and it doesn't have an archive page.

However, you can access example.com/custom-tag/tag-term

which will list all posts associated with the taxonomy that are also "tagged" as "tag-term".

(This is also possible for hierarchical categories: example.com/custom-category/category-term)


Now, you might still want to have a /custom-tag/ "archive", and to achieve this is to create a "page" that you call "custom-tag" with the slug "custom-tag". Leave this page blank.

Then create a template in your theme folder with the name: page-custom-tag.php and set the page you created to use this template.

In the template you need to create some code that lists all your posts for the various tags, using the WP_loop and some filtering.

Now you can visit: example.com/custom-tag/ and have it list all your posts assoicated with the custom tag taxonomy.

Wednesday, January 28, 2026

WordPress Custom Taxonomies and Permalink Structure

As I was doing some development of a WordPress plugin where custom post types were being added and also add and use a custom taxonomy, I bumped into the problem of getting "nice" and "flexible" permalink structures in WordPress. If you find this topic confusing, read the whole blog post as it will hopefully be more clear to you when you get through a few of the examples.

If you are new to the subject, WordPress uses a flexible taxonomy structure so that you can create your own taxonomies, similar to how you can create your own custom post types. The standard taxonomies in WordPress are "category" (which is a hierarchical taxonomy) and "tags" (which is a non-hierarchical taxonomy). More on this later.

Initially I thought it would be simple to add a structure like such: example.com/post-type-slug/custom-taxonomy-category/

So for example: if you have created a custom post type called "books", you may want to have a custom taxonomy called "genre". The structure I wanted was:

example.com/books/genre/fiction

and then you could add more categories like:

example.com/books/genre/nonfiction

example.com/books/genre/biographies

and so on.

(Maybe just example.com/books/fiction would be quite cool ...) 

I thought this would be easy to achieve but apparantly I couldn't find any sources that supported the claim that this type of more flexible type of structure was possible to create: but rather the opposite. WordPress doesn't seem to support this, but rather rely on having all slugs for everything on one "level":

example.com/books/ is fine

example.com/genre/fiction is fine

As you can see: as long as you don't try to "build" on that "category" taxonomy-slug, it will work and you will need to separate the custom post type slug and the custom taxonomy slug - since they're presumably in the same namespace.

So how would you distinguish "genre" from a "custom" taxonomy for pages with the same name?

For example if you created a different custom post type called "movies" and wanted a custom taxonomy for that as well, also called "genre": then I believe that would not work (if they have the same slug) and they could not have the same slug name, occupying the same namespace.

The solution I found was to simply prepend the custom taxonomy slug with "book-": "book-category":

example.com/book-category/

example.com/book-category/fiction

and so on.

The slug for custom post is still books:

example.com/books/

and the posts will follow this structure primarily:

example.com/books/bookpost

Although example.com/book-category/fiction/bookpost will redirect to  example.com/books/bookpost.

So as you can see custom posts share the namespace with other custom posts for taxonomies and the standard taxonomy "categories" which exists for "pages" and "posts", and "tags".

Since a constraint was to not introduce additional plugins, I chose this option, in trying to understand the namespace and by doing so knowing how to avoid duplicate taxonomy slugs. Another option would be to use one of the permalink structure plugins that exists. There are a few, but the one that worked best for me was wp better permalinks.

There are a few others, some free and some at a cost. They can be worth checking out but wp better permalinks was the easiest one to use in my case.

With this structure, you can get a nice permalink structure which in my view, is a bit easier when you want to organize things.

Okay, so I said I would get back to hierarcical and nonhierarchical taxomonies. When should they be used? Well, "categories" are a typical "hierarchical" taxonomy. Using that you can sort your posts into nice categories, which can be multilevel, as in a tree structure: top level categories and sub categories beneath them. Nonhierarcical taxonomies, like "tags", doesn't have any levels, it's just one "flat" level, where you add "tag words" to your posts (which is also a way or sorting your posts).

The permalink structure would be something like this:

example.com/book-tag/funny

example.com/book-tag/sad

and so on. On a more practical side, given that you are just going to use one level of "hierarchy" then "hierarchical" taxonomys (like categories) have a quite "fixed" UI where you edit the posts: the categories will show up with checkboxes which you can check, in order to assign a post to a category. Tags on the other hand has an auto-complete type function, where you will have to type (if the tag doesn't exist yet) or start type the whole tag name in order to assign it to a post.

Not a big deal, but I prefer the category UI. You can of course also use both hierarchical and nonhierarchical taxonomies for your custom post type at the same time. Now this makes it a bit more "meta data" to add to each post but they will be "doubly sort-able", if you like those kinds of things.

Whereas "category" can be used for fixed types like "fiction", "nonfiction" and so on, "tags" can be used to add more "content related" keywords, like "funny" or "sad", or whatever you think is a good keyword for your post content.

I hope this blog post clarifies some things about (custom) taxonomies and permalink structure in WordPress. There's some additional details about things like SEO, redirects, what templates to use and how a query_var-version of permalinks also can be used as an URL. But hopefully I will get back to that in a later blog post. 

Saturday, September 20, 2025

A Rapid Review on Website Accessibility

I hereby present to you a rapid review on accessibility in development of websites, with the title: Automated Testing for Website Accessibility.

Now, a rapid review could be said to be a method for providing a "quick glance" (overview) at a particular requested topic. By finding evidence in the sources (primary sources perhaps), the goal is to create a type of review, more slim than a full literary review, using perhaps only a single or a few selected databases.

If you are more interested in the rapid review approach in the field of computer science and software engineering, see the reference to Cartaxo in the end of this blog post, which I was provided with in the course I took about scientific method and where I wrote this rapid review.  

This was my first try at the approach, and also first time to use a thematic analysis (which I will write more on eventually, and have already written a few initials thoughts on, which I intend to elaborate further on).

So with that disclaimer in place, I now present my completed rapid review, which I hope might be useful for practitioners and interesting to researchers.

As I recently wrote in my bachelor's thesis proposal, the aim of this rapid review was to give a useful overview of the current (as of 2025) of what tools are being used in accessibility website development and testing, and by applying a more quantitative approach (frequency analysis), yet of a limited sample size: give some indications on what tools are being used, primarily in accessibility research, and their 'popularity'. 

This was explored in RQ2: What types of automatic accessibility testing tools are there? where the following figures can be found.

The most frequently used tools in the studies. 
The most frequently used tools in the studies.
 

Fig. 2. The ten most frequently used tools in the studies. See appendix for a figure of all studies.

 Fig. 2. The ten most frequently used tools in the studies. See appendix for a figure of all studies.

A similar analysis was carried out on the WCAG versions being used in these primary sources (studies), which again, in this limited sample size, indicated that there might be a "lag" in the adoption of the latest WCAG version. The following figure was included in RQ1.

The different WCAG versions in the studies, as accumulated number of studies per WCAG version over time (year).
Fig. 1. The different WCAG versions in the studies, as accumulated number of studies per WCAG version over time (year).

Research question 1 (RQ1) was summarized as: What is possible to test and how effective is automated testing? Besides my analysis of WCAG versions, I looked into various measurements such as coveragecompleteness and correctness.

Besides from these more quantitative measures that were discovered in the studies, concepts like test-ability and effectiveness were also explored. 

Test-ability, as in: What is possible to test? And, how effective are these automatic WCAG based testing tools?

Finally, some more qualitative aspects were examined in research question 3 (RQ3) that dealt with best practices: What are common best practices of using automatic testing tools?

Where some of the key takeaways was: do not solely rely on automated testing. And combine tools. (See sources in the rapid review).

Again, repeating the disclaimer: as this is a limited study the conclusions and the results may be limited as well. And this is merely a bachelor level study, yet I thought it might be an interesting source for both practitioners and researchers.

In either case, I learned a lot myself and will hopefully write my bachelor's thesis in a related area. But as for now, I enjoyed the methodology and implementing it in the work so to speak; as well as working with the analysis.

You can find a link to the rapid review here.

If you wish to cite this rapid review, I'm unsure if it's possible, since it's not peer-reviewed nor published on any official university source. It is only my own personal publication, so to speak. Therefore, something like:

Larsson, Nils, Rapid Review: Automated Testing for Website Accessbility, 2025, written in the course Computer Science C: Scientific Method at Mid Sweden University, published on the compartdev blog on September 20, 2025.

Other related references

You can also watch the video on performing a thematic analysis using PDF sources and open source software on this link, which I used when I wrote this rapid review: here.

B. Cartaxo, G. Pinto, and S. Soares, “Rapid Reviews in Software Engineering,” Mar. 22,
2020, arXiv: arXiv:2003.10006. doi: 10.48550/arXiv.2003.10006. 

WordPress Null Handling Confusion in Custom Fields

I recently built a WordPress plugin that was going to handle custom fields. The functionality was similar to the plugin ACF (Advanced Custom...