How to Calculate Values in WordPress Posts and Pages

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.


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:


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:


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:


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
Hostgator Basic Hosting $31.68 $82.8 $94.32
SiteGround Basic Hosting $47.88 $95.76 $143.64
Bluehost Basic Hosting $35.4 $118.8 $106.2
InMotion Basic Hosting $29.88 $71.76 $89.64
DreamHost Basic Hosting $31.08 $94.8 $93.24
GoDaddy Regular Basic Hosting $779.88 $2003.52 $3209.28
GoDaddy $1 Basic Hosting $779.88 $2027.64 $3275.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

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!


  1. Searchngp says

    I wish you would provide support for the plugin. Its a great concept which would be very useful


  2. This plugin is super! You should also consider adding separators for bigger numbers. For example 345000 would better display as 345,000.
    Otherwise great work


  3. Hi, this error has started showing up
    Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EvalMath has a deprecated constructor in /home3/sername/ on line 89


  4. That’s great. Good to know i contributed positively


  5. Do you offer customizations?
    Am looking for a way to save the calculated results in a way that it can be used for other calculations. Also is it possible to run a cronjob so that a calculation from the saved result can be done periodically?


    • Hi Kristina,

      You could always call the shortcode from another function and save the results in a database. In the same way, you could add a cronjob to run a single PHP file as well. At this time, I don’t do personal customizations. Sorry!


  6. Hi there,

    Are you still supporting this plugin? I posted in the WordPress plugin support and haven’t received a response yet. I posted the following:

    I am using the plugin “Favorites for WordPress” from I need to add a number to their Favorite Count which is a Real Integer / non-decimal value.

    The value returned by your plugin is always “0”. This is the formula I am using:
    [calculate] 5 + [favorite_count] [/calculate]

    When I display [favorite_count] outside of the [calculate] shortcode it returns “1”. When I add 5+5 within the [calculate] shortcode it returns “10”. I have also tried other integer generating shortcodes and they do not work either – they always return a value of “0” when I try to perform an operation on them within the Calculate plugin.

    Is there something I am missing? I am using version 2.1.1 of the plugin and WP 5.2.

    Any support is appreciated.



  7. I wanted to calculate the date of birth of a person in real time can this plugin do the same work?


  8. Pedro Ortega says


    I just want to let you know this is a fantastic PLUGIN it really help me a lot. I will just wait for may business is ready and making some money to support you in continue making the plugin with a lot of features.

    Good job


Speak Your Mind