How to Calculate Values in WordPress Posts

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 calculations 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 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 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, go to Plugins->Add New, and type the name of the plugin as shown here:

Install the Calculate Values with Shortcodes Plugin in WordPress
Install the Calculate Values with Shortcodes Plugin in WordPress

Now install and activate it.

Note: It’s 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

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, 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
Use formulas in the WordPress Editor

And this will give the following output:

Calculated value displayed in the post
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, and you might as well type the result and save WordPress the overhead. Its 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. I do have such a shortcode. Since I compare the 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"]

Using this shortcode, I can display this value in tables, forms, or even just in the post. 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 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

The plugin defaults 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 Formula
Use Shortcode in WordPress with a Formula

And this calculates the yearly value in real-time:

WordPress Yearly Cost Shortcode Calculation
WordPress Yearly Cost Shortcode Calculation

That’s pretty cool, huh?

Using Locales for Decimal Digits and Large Numbers

You can use the “int” parameter and pass a number format for your desired locale for large numbers. For example, if you want to display number separators like they do in France, type:

[calculate int="fr_FR"]1+250000000[/calculate]

Here’s a comprehensive list of available locales.

Now For a Real Example

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

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

1-year 2-years 3-years
Hostgator Basic Hosting $30.84 $82.8 $135
SiteGround Basic Hosting $35.88 $71.76 $107.64
Bluehost Basic Hosting $35.4 $118.8 $106.2
InMotion Basic Hosting $29.88 $71.76 $89.64
DreamHost Basic Hosting $35.4 $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 need to update the costs from within my database, and these values and calculations are 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!

Comments

  1. Searchngp says

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

    Reply

  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

    Reply

  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/xxxx.com/wp-content/plugins/calculate-values-with-shortcodes/evalmath.class.php on line 89

    Reply

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

    Reply

  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?

    Reply

    • 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!

      Reply

  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 https://favoriteposts.com/. 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.

    Thanks
    D

    Reply

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

    Reply

  8. Pedro Ortega says

    Hi

    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

    Reply

  9. I can’t seem to use your shortcode to provide a value as a parameter for another shortcode. It’s not what you could call a nested shortcode. Should this be possible or am I dreaming?

    Specifically, instead of providing the typed value ’35’ to this Avada theme shortcode…
    [fusion_counter_box value=53 unit=”%”]TE50 – [tax_count tax=”portfolio_category” slug=”t2-te50″] of the 141 produced
    [/fusion_counter_box]

    I’d instead like to pass a calculated value to it.
    [fusion_counter_box value=[calculate]([tax_count tax=”portfolio_category” slug=”t2-te50″]/141)*100[/calculate] unit=”%”]TE50 – [tax_count tax=”portfolio_category” slug=”t2-te50″] of the 141 produced
    [/fusion_counter_box]

    https://www.tseriesclub.org/registry/t2-registry/

    Reply

  10. Hi.

    I have installed your plugin for my website (SaiShiko.com). Plugin is well developed and it is working well for my most requirements. I have small doubt. Plugin Working only in WordPress Website, when i send score card in email, then short codes its not working. please help.

    Reply

Speak Your Mind

*

WP-Tweaks