WordPress is a curious combination of the static and the dynamic. The overwhelming majority of WordPress pages are static – they hardly ever change. On the other hand, the entire architecture of the CMS is based on a dynamic database. Pages are generated from scratch each time a request is made. It seems a bit of an overkill, but it’s pretty fundamental to the way WordPress works.
Despite this backend-driven approach however, it’s hard to display dynamic values in a given WordPress post. If you want to show a specific database value as a price for example, you need a shortcode. There are plenty of suggestions on how to do this. But what’s been missing so far, is a way to display dynamic calculations on a web page.
Developing the Calculate Values with Shortcodes Plugin
I first encountered this problem when I wanted to show constantly changing prices and discounts on my site without hardcoding the values into each page. That way, I could update one value in my database and all the changes and discounts would propagate everywhere else.
Since this feature was integral to the working of my site, I decided to develop the plugin myself. Hence the birth of the “Calculate Values with Shortcodes” plugin. Using this, you can perform calculates on your site and display the result in a post.
For example, let’s say you want to add two numbers, you show it like this:
[calculate]2+3[/calculate]
This will print “5” on the page.
Substituting Dynamic Values for Static Ones
By itself, the above example isn’t very useful. You could have simply typed “5” to begin with! The true power of the plugin becomes visible however, when you want to use dynamic values in your calculation to begin with.
So let’s say you have a shortcode that fetches the price of a product. You can then calculate a discount of 25% like this:
[calculate] [shortcode_to_get_price]*0.75 [/calculate]
And it would display the new price after the 25% discount! Now, whenever you need to make a change to the price, you simply need to update the value in the database and the page will show the final result after the price cut.
No Dangerous Functions like “Eval”
PHP has always had the ability to use “eval” to calculate expressions. However, as the documentation makes clear, this is a very dangerous thing to do. The reason is that any code within eval will execute as is without any checks. For this reason, it’s widely accepted amongst programmers that almost no use of eval is valid. Rasmus Lerdorf, one of the founders of PHP once said:
If eval() is the answer, you’re almost certainly asking the wrong question.
Given all these warnings, this plugin instead uses the open-source math library called “EvalMath” . This is a library that sanitizes math input and uses a safe eval implementation to ensure that nothing can go wrong. So you can use any math functions supported in the documentation and rest easy that you’re not doing anything dangerous!
Working with Caching
Of course, as with any dynamic changes, you need to be aware that caching systems on your server or via a plugin can mess up your output. Depending on your caching system, you may or may not have a way of purging the cache programmatically. Cloudflare for example, used to have an API for purging the cache, but things have changed a lot since then, so I can’t be sure if they still support it.
So if you need your values to constantly change on a minute by minute basis, you’ll probably need to disable that specific page from server-based caching. Otherwise, you risk showing outdated information to your visitors.
Bottom Line
This plugin is best used when the data needs to be changed on a semi-regular basis. That way, it works great with all caching systems. If you can organize the caching to exclude those posts where you need more regular data changes, that will work too!
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!
Leave a Reply