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

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How To Display Dynamic Calculations On A Static Page In WordPress

Bhagwad Park

Published on: July 31, 2019

Categories: WordPress 0

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!

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!

Related Posts

Blogger vs. WordPress: Which One to Use?

Let's compare WordPress and Blogger across five key areas. Then, we'll show you how to set up a professional blog with WordPress.

How To Back Up Your WordPress Site (And Why You Should Do It Today)

In this post, we’ll take a closer look at why backups matter. We’ll also show you different ways to back up your WordPress website.

What Is Self-Hosted WordPress?

In this post, we’ll differentiate self-hosted WordPress from WordPress.com, and discuss the benefits of the former.

How To Start A Newsletter In WordPress

Check out our complete guide on why and how you should start your own newsletter in WordPress.

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

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

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
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • 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
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

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