In my another tutorial about syncing users using WordPress REST API I received a question in comments, where I was asked about metadata. After googling for quite a bit I found out that there is no clear explanation out there about using user’s meta in REST API requests.
Even though Gutenberg (WordPress Block Editor) has become our new reality and FSE themes are also on their way, we still need meta boxes on our projects. The meta boxes aren’t necessarily classic meta boxes for posts, it could be additional settings fields for taxonomies, users, comments and even options pages. It means that we are going to need them for a very long time. Maybe even forever.
Of course the best way to create any of the mentioned above is to code by yourself. But it could be really time consuming especially if you have a lot of fields (and working on lots of projects).
That’s when metabox plugins come into play. The most popular ones are Advanced Custom Fields and Carbon Fields. Also in this article I am going to mention my own metabox plugin I developed for my needs which is Simple Fields.
The idea of this tutorial is to become more conscious to what plugins we are using to achieve our goals, because there are lots of slow WordPress websites out there, not because WordPress is slow but because sites are overloaded with tens and even hundreds heavy plugins.
Let’s look the truth in the face – of course I am going to tell you how cool my Simple Fields plugin is. But I am not going to say “use it”, because I understand clearly that sometimes its functionality is not enough, then you can probably look at Carbon Fields plugin. And even Advanced Custom Fields PRO has its own advantages though I am not a big fan of it.
In this guide I am assuming that you already have some basic knowledge how to work with WP_Query class in WordPress. And this tutorial will be all about working with custom fields.
Either you are working with Gutenberg or with Classic Editor, maybe you’re using some custom fields plugins like ACF, Carbon Fields, Simple Fields (it is my plugin, highly recommend) – in all of these cases the idea of working with Meta_Query will be the same.
In this tutorial we are going to talk about how to add custom fields to WooCommerce products. I am not going to talk about plugins here – we will work with code snippets only and all the additional fields will be added to “Product Data” metabox.
Please also keep in mind that WooCommerce allows to hide or display specific product fields depending on a product type. So for example we can show our additional product fields only for “Downloadable” or “Variable” products.
Just like that:
I already have a plenty of tutorials on my blog where we created different kind of fields for Gutenberg sidebars, for example an image field, a gallery field, a checklist and even a dropdown with posts.
There is also my Simple Fields plugin for those of you guys who don’t want to mess up with JSX and React and prefer to use simple PHP code snippets instead.
But now let’s talk about creating a repeater field. First things first – a screenshot:
I already have a couple of tutorials about working with metadata in Gutenberg, so you can find a complete guide to it and a separate tutorial about image fields. Now let’s continue to dive into it and discuss types of fields that consist of multiple input elements.
We are not going to dive straight into the repeater fields (let’s do it later) and discuss something more simple, a checklist field for example.
That’s how it is going to look like: