# How to display a WooCommerce product price shortcode

URL: https://wp24x7.com/how-to-display-product-price-using-shortcode/
Updated: 2026-11-29
Type: blog post
Audience: WooCommerce store owners and developers

> A how-to for a WooCommerce shortcode that prints one product's price anywhere on a site and stays correct when the price changes. It returns WooCommerce's own price markup, so sale prices, variable ranges and tax display are handled. The post gives the full plugin code, a plain-number variant, testing steps and common problems.

## Key facts

- For: WooCommerce store owners and developers
- Usage: [product_price id="123"] in a post, page or Shortcode block
- Output: WooCommerce price HTML, sale and tax aware
- Where to put code: a small plugin or a child theme, never a parent theme
- Safety: ID forced to an integer, output filtered, drafts hidden
- Test: simple, sale, variable and tax-inclusive products

Sometimes you want one product's price in a blog post, a landing page or a banner, and it must stay correct when the price changes. **Register a shortcode that looks up the product and returns WooCommerce's own price markup, so sales, variable ranges and tax display are handled for you.**

> **Quick answer**
Add the plugin code below, activate it, then write `[product_price id="123"]` wherever the price should appear. Back up first, and keep the code in a small plugin or a child theme, never a parent theme.

## Why not just type the price?

A typed price goes out of date the moment you run a sale or change currency rules. A shortcode asks WooCommerce each time the page is built, so one edit to the product updates every page that mentions it. If you use page caching, the cached page updates when the cache refreshes, which WooCommerce normally triggers when a product changes.

_One product edit updates every page that shows its price_

## What is the code?

```php
<?php
/**
 * Plugin Name: Product Price Shortcode
 * Description: Adds [product_price id="123"] to print a product's price.
 */

defined( 'ABSPATH' ) || exit;

add_shortcode( 'product_price', function ( $atts ) {
	if ( ! function_exists( 'wc_get_product' ) ) {
		return '';
	}

	$atts = shortcode_atts( array( 'id' => 0 ), $atts, 'product_price' );

	$product = wc_get_product( absint( $atts['id'] ) );

	if ( ! $product || 'publish' !== $product->get_status() ) {
		return '';
	}

	return '<span class="product-price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
} );
```

Save it as `product-price-shortcode.php` inside `wp-content/plugins/product-price-shortcode/`, then activate it under **Plugins**. If you prefer to avoid a plugin file, put the `add_shortcode` call, without the header comment, in your child theme's `functions.php`.

## How do I use it?

Find the product ID: edit the product and look at the number in the browser address bar (`post=123`), or hover over the product in the list. Then write the shortcode in a post, page, or Shortcode block:

```
The Starter plan costs [product_price id="123"] per year.
```

## What does the code do, line by line?

| Line or call | What it does |
|:--|:--|
| `function_exists( 'wc_get_product' )` | Stops a fatal error if WooCommerce is deactivated |
| `shortcode_atts` | Sets defaults and ignores unknown attributes |
| `absint` | Forces the ID to a positive integer, so `id="abc"` becomes 0 |
| `wc_get_product` | Returns `false` for an unknown ID, which the code checks. The old version of this snippet called a method on the result without checking and crashed on a wrong ID |
| Status check | Hides prices of drafts, private and trashed products from visitors |
| `get_price_html()` | Returns the HTML WooCommerce prints on the shop, including the struck-through regular price during a sale and a range for variable products. It respects the tax display settings |
| `wp_kses_post` | Allows only safe HTML tags, a defensive habit for markup filtered by other plugins |

## How do I show a plain number or a different format?

`get_price_html()` is usually best. Sometimes you want a number, for example for a table or a script. Use `wc_get_price_to_display()` so tax display is respected:

### Plain number
```php
$price = wc_get_price_to_display( $product );
return esc_html( wc_format_decimal( $price, wc_get_price_decimals() ) );
```
### With currency symbol
For a currency symbol and formatting without the sale markup, wrap the number in `wc_price()`:

```php
return wp_kses_post( wc_price( wc_get_price_to_display( $product ) ) );
```

Raw `$product->get_price()`, which the earlier version of this tip printed, ignores tax display settings. If your shop shows prices including tax, it prints the wrong figure.

## Common problems

| Symptom | Cause and fix |
|:--|:--|
| Prints as text | The text is in a block that does not process shortcodes. Use the Shortcode block, or run the string through `do_shortcode()`, as in our guide to [running shortcodes in custom fields](/execute-shortcode-string-wordpress-custom-field) |
| Nothing appears | Check the ID, that the product is published, and that WooCommerce is active |
| Variable product shows a range | Intended. To show one variation's price, pass its ID |
| Price looks stale | Clear page and object caches after a price change |
| Different currency | A multi-currency plugin filters price HTML, so the shortcode follows it |

## Worked example: a landing page that quotes a plan price

For example, a landing page for a course says "Enrol today for [product_price id="321"]". During a sale, the store owner edits the product and sets a sale price. The landing page now shows the regular price struck through with the sale price beside it, without anyone editing the page. After the sale ends, it returns to the normal price on its own. Without a shortcode, the owner would have had to remember every page that mentioned the price.

## How do I test it?

1. **Use a copy.** Create the shortcode on a staging copy, or at least take a backup.
2. **Try four products.** A simple product, one on sale, a variable product and one with a tax-inclusive setting.
3. **Try bad input.** A wrong ID and a draft product's ID should print nothing and show no error.
4. **Deactivate WooCommerce on staging briefly.** The page must not crash.
5. **Check as a visitor.** Clear caches and view the page logged out.

## What are the security points?

The shortcode takes one attribute, forced to an integer, so it cannot be used to inject markup. It prints only the price HTML WooCommerce generates, filtered through `wp_kses_post`. If you add text attributes such as a prefix, escape them with `esc_html`. Never print an attribute straight into the page.

## Should I use the shortcode or a block?

If your page is built with blocks, check whether your WooCommerce version offers a Product Price block, which needs no code and follows editor styling. The shortcode is still useful in places blocks cannot reach, such as widgets, emails written in a plugin, custom field output and older builders. It is also easy to reuse in templates with `do_shortcode( '[product_price id="123"]' )`. Pick whichever your editors find easiest to maintain, and document the product ID somewhere visible, because a shortcode that points at a deleted product quietly prints nothing.

## Can it do more?

You can add attributes, for example `[product_price id="123" prefix="From "]` with `esc_html( $atts['prefix'] )`. If you want text before or after prices everywhere, not just through a shortcode, see [adding text before or after the price in WooCommerce](/woocommerce-add-text-before-after-price).

- **WooCommerce experts** Price displays by role or country, tested on your store. [WooCommerce experts](/woocommerce-experts)
- **Plugin development** Small plugins built and maintained properly. [Plugin development](/wordpress-plugin-development)
- **Text around prices** Add a prefix or suffix everywhere. [Before or after the price](/woocommerce-add-text-before-after-price)

**Want store customizations built and tested?**
A WooCommerce developer reads your brief, today.
[Post a project](/post-project)
[Call the help desk](/help-desk)

## Frequently asked questions

### Where do I put the shortcode code?

In a small custom plugin, or in a child theme's functions.php. Do not add it to a parent theme, because updating the theme erases it.

### Does the shortcode follow sales and tax settings?

Yes, if it returns the product's price HTML, which respects sale prices, variable ranges and your tax display setting.

### Can I use it in a page builder or block editor?

Yes. Use a Shortcode block in the block editor, or a shortcode widget in your builder.

### How do I find a product's ID?

Edit the product and look at the number in the browser address bar, shown as post equals a number, or hover over the product in the products list. Use that number as the id attribute.

### Why does the shortcode print nothing?

Check the ID, that the product is published, and that WooCommerce is active. The code deliberately prints nothing for unknown IDs and for draft, private or trashed products.

### Why does a variable product show a range?

That is the intended output. To show one variation's price, pass the variation's ID, because variations are products too and wc_get_product accepts a variation ID.

### Why does the shortcode appear as plain text?

The text sits in a block that does not process shortcodes, such as a code or escaped HTML area. Use the Shortcode block. In a custom field or widget, run the string through do_shortcode.

### Why is the price stale after I changed it?

A page cache or object cache may still hold the old page. Clear caches after a price change, or avoid long cache lifetimes on pages that carry price shortcodes.

### How do I print only a number instead of the HTML?

Use wc_get_price_to_display so tax display settings are respected, then format it. Avoid get_price, which ignores tax display and can print the wrong figure if your shop shows prices including tax.

### Is the shortcode safe to expose to editors?

Yes. It takes one attribute, forced to an integer, and prints only the price HTML WooCommerce generates, filtered through wp_kses_post. If you add text attributes, escape them with esc_html.

### Should I use a Product Price block instead?

If your page is built with blocks and your WooCommerce version offers one, a block needs no code. The shortcode still helps in widgets, custom fields, templates and older builders.

### Can WP24x7 build store customizations like this?

Yes. Price displays by role or country and other store changes are WooCommerce development work. Post a project and a developer will scope it and test it for you.
