• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero Blog

NameHero Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • VPS Hosting
    • WordPress Hosting
    • WooCommerce Hosting
  • Reseller
  • Enterprise
  • Domains
  • Account
  • Blog Home
  • Categories

How To Access Data Key/Value Pairs In WordPress

Bhagwad Park

Published on: July 4, 2018

Categories: WordPress 0

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:

Add New Record

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:

Here is the Shortcode

 

In this example, the value is:

[cwd ref="datakey"]

Use this shortcode in your posts like this:

Shortcode in the Editor

And it’ll show up in the output:

Shortcode in the Post

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:

Storing Data in Arrays

Here, the two sub-keys “firstvalue” and “secondvalue” are linked to the parent “datakey”. We can specify any number of subkeys as shown above:

firstvalue=hello
secondvalue=there

Now we can access these values in any post using the “key” parameter as shown here:

Accessing Array Values in Posts

And this will give us the following output:

Access Data Key/Value Pairs in WordPress

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:

prefix_custom_website_data

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!

Bhagwad Park Profile Picture
Bhagwad Park

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!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!

Most Popular Posts

Speed up your site: solving the WordPress slowdown

NameHero’s Recommended WordPress Plugin and Theme Setup (2023)

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • Website Performance
  • Web Hosting
  • Resellers
  • Website Development
  • Website Security
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Domains
Help & Support
  • NameHero Blog
  • Knowledgebase
  • Announcements
  • Affiliates
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2023 NameHero, LLC. All rights reserved.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA