How to display a WooCommerce product price shortcode
Create a [product_price id="123"] shortcode that prints a WooCommerce product's price anywhere, with sale prices, tax display and safe output handled.
At a glance
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.
Updated November 29, 2026 · For WooCommerce store owners and developers
- 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
On this page
- Why not just type the price?
- What is the code?
- How do I use it?
- What does the code do, line by line?
- How do I show a plain number or a different format?
- Common problems
- Worked example: a landing page that quotes a plan price
- How do I test it?
- What are the security points?
- Should I use the shortcode or a block?
- Can it do more?
- FAQ
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.
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.
What is the code?
<?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:
$price = wc_get_price_to_display( $product );
return esc_html( wc_format_decimal( $price, wc_get_price_decimals() ) );For a currency symbol and formatting without the sale markup, wrap the number in wc_price():
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 |
| 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?
- Use a copy
Create the shortcode on a staging copy, or at least take a backup.
- Try four products
A simple product, one on sale, a variable product and one with a tax-inclusive setting.
- Try bad input
A wrong ID and a draft product's ID should print nothing and show no error.
- Deactivate WooCommerce on staging briefly
The page must not crash.
- 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.
Price displays by role or country, tested on your store.
WooCommerce experts02Plugin developmentSmall plugins built and maintained properly.
Plugin development03Text around pricesAdd a prefix or suffix everywhere.
Before or after the priceA WooCommerce developer reads your brief, today.
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.
Link to this answerDoes 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.
Link to this answerCan 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.
Link to this answerHow 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.
Link to this answerWhy 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.
Link to this answerWhy 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.
Link to this answerWhy 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.
Link to this answerWhy 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.
Link to this answerHow 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.
Link to this answerIs 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.
Link to this answerShould 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.
Link to this answerCan 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.
Link to this answerKeep reading
Want an engineer on it?
Tell us what is wrong in plain words. A person reads it, today.