How to Calculate Values in WordPress Posts and Pages

Last updated on

Let’s say you store some data in MySQL database that you retrieve via a shortcode. This shortcode returns a dynamic value that’s updated in real time. Great! Now what if you need to perform some calculation on that value? Let’s say it’s as simple as multiplying it by 2. Now you’re stuck!

Short of changing the shortcode itself to perform that calculation, or multiplying it in the backend via another shortcode, there’s no way for you to achieve this effect. So for example, if you have a database of monthly hosting prices (as this website does), and you want to display the “yearly” total, you can’t – because WordPress doesn’t allow you to calculate values in real time.

So…

I Created the Plugin – “Calculate Values with Shortcodes”

I developed this plugin because I needed to perform calculations with dynamic shortcodes to display in WordPress. Not in fields, or other user generated input – but in the posts and pages themselves. You can download it from here.

Or from within WordPress, just go to Plugins->Add New, and type the name of the plugin as shown here:

Install the Calculate Values with Shortcodes Plugin in WordPress

Now just install and activate it.

Note: It’s a bit difficult to display shortcodes “as is” in WordPress. So the below examples may have an extra “]” or “[” square bracket. See the images – they have accurate code!

Using the [calculate] Shortcode/Plugin in your Posts

Let me demo a simplistic scenario. Look at the expression:

(2+3)*4

Now what if you wanted to calculate this and display the result in WordPress? You might think – simple! Just do the calculation beforehand and type in the value – problem solved! And you would be right. But let me show you how it’s done using my plugin.

To calculate the expression, simply enclose it within [calculate][/calculate] shortcode tags. Like this:

[calculate](2+3)*4[/calculate]

This will output the value of the expression inside the shortcode. You can display the whole thing in an editor like this:

Use formulas in the WordPress Editor

And this will give the following output:

Calculated value displayed in the post

Pretty neat huh? The best part is that the code doesn’t rely on the dangerous “eval” function, so you can implement this shortcode on your site without worrying that some authors can maliciously execute random PHP code on your installation.

But it’s useless with static values. You might as well just type the result and save WordPress the overhead. It’s true power comes when you use dynamic values as in the following example.

Using Shortcodes in Formulas with [calculate]

Now let’s say I have a shortcode that returns a numeric value. As a matter of fact, I do have such a shortcode. Since I compare web hosting prices of various providers on this site, I have a shortcode for each price. Here’s the shortcode for GoDaddy price per month when billed for a year:

[cwd ref="siteground_shared" key="basic_price"]

I can display this value in tables, forms, or even just in the post using this shortcode. And it’ll automatically update whenever I make changes to the database.

But now, what if I want to display the yearly cost? I need to multiply this value by 12. But I can’t just do it manually because the price changes in real time! And that’s where I use this plugin. To just multiply the value of the shortcode by 12, I use:

[calculate dec="2"][cwd ref="siteground_shared" key="basic_price"]*12[/calculate]

Rounding off Values

By default, the plugin rounds the calculated value up to the nearest integer. It uses the PHP “round” function with the default flag PHP_ROUND_HALF_UP.

But sometimes, we need to display decimal places as well. And that’s what the “dec” attribute is for. As shown in the above plugin, I use the attribute:

dec=”2″

to indicate that I want to round the value to two decimal places. I choose this, because that’s how hosting prices are typically displayed on the Internet. Here’s an illustration in the editor for how I display the yearly cost of GoDaddy hosting:

Use Shortcode in WordPress with a Formula1

And this calculates the yearly value in real time:

Calculate value with shortcode1

That’s pretty cool huh?

Now For a Real Example

Here’s a table showing the 1-year, 2-year, and 3-year hosting costs + SSL certificates of major providers. Each of these cells is a complex calculation involving multiple shortcodes. Keep in mind that the hosting and SSL costs differ depending on how long you choose to bill. So this is an actual real world example of the power of this plugin.

Hosing + SSL Costs for 1, 2, and 3 years.

1-year 2-years 3-years
SiteGround Basic Hosting $47.4 $94.8 $142.2
Bluehost Basic Hosting $95.4 $118.8 $142.2
InMotion Basic Hosting $64.68 $114.96 $150.84
Hostgator Basic Hosting $82.92 $148.08 $211.8
DreamHost Basic Hosting $69.36 $142.8 $214.2
GoDaddy Regular Basic Hosting $71.88 $230.52 $299.28
GoDaddy $1 Basic Hosting $71.88 $242.64 $413.4

And all of this is in real time! I just need to update the costs from within my database and these values and calculations are all done automatically. Now that’s pretty damn awesome!!

About Bhagwad Park

Bhagwad has been writing WordPress tutorials for years. He also creates tutorials on Linux server administration, and has a ton of experience writing about and using web hosting products! Send me an e-mail!

Speak Your Mind

*