One of the strengths of WordPress is that it can be used to create just about any kind of site. It handles regular static sites just as well as dynamic data-driven ones. Normally, dynamic sites are created using certain themes and plugins like WooCommerce. This allows you to easily create pages that display new prices, discounts, or coupons.
However, sometimes you want a little more flexibility. Perhaps you don’t want all the bells and whistles of WooCommerce and want something much simpler – the ability to store data in the backend and call it using a shortcode in your posts. In this article, I’ll show you how to do that.
Using Custom Website Data for Data Store/Retrieval
There are many possible approaches to the problem. I can create a new table in the backend via phpMyAdmin and create a shortcode to access it. Or I can create individual custom fields that are specific to each post. But the first one is a bit too complicated for simple needs, and the latter doesn’t provide global access to data.
Rather, I’m going to use a somewhat older plugin that works perfectly. It’s called “Custom Website Data“. It allows you to create any number of key/value pairs and retrieve the values in your posts. You can even store them as arrays to group data together! Let me demonstrate how it works.
Step 1: Downloading and Creating the First Key/Value
Download and install the “Custom Website Data” plugin into WordPress. Now we get started by clicking “Custom Data -> New Record” as shown here:
This brings us to the entry form. First, we enter the “key” in the top field, and then the value in the box like this:
Save your changes, and you’ve created your first key/value pair!
Step 2: Displaying the Value in your Posts
After saving, it takes you to the screen showing all your key values. And here, you can see the shortcode you use in your post to make the value appear:
In this example, the value is:
Use this shortcode in your posts like this:
And it’ll show up in the output:
Storing Array Values
For me, the real power of this plugin comes due to its ability to group key/value pairs together into arrays. So instead of having a huge number of disparate key/value pairs, you can bunch them together like this:
Here, the two sub-keys “firstvalue” and “secondvalue” are linked to the parent “datakey”. We can specify any number of subkeys as shown above:
Now we can access these values in any post using the “key” parameter as shown here:
And this will give us the following output:
So now you can have a data key called “price”, and store a whole bunch of related items like its weight, inventory quantity, discounted price etc.
Limitations of Array Values
The limitation is that internally, the database field for the “values” is a TINYTEXT instead of a TEXT and so the total length of the value field cannot exceed 256 characters. But I’ve manually changed that in the database!
If you want to do this for yourself, log into your cPanel, open phpMyAdmin and select the database for your WordPress installation. The table for this plugin will be:
Here, the column name called “data” needs to be changed from “tinytext” to “text”. Once you do this, you can store pretty much as many key/value pairs in the array as you want. I personally make this change so I can have a large number of items in the array. It works great!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!