Skip to content
WooCommerce

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.

By WP24×7 engineers5 min readPublished September 23, 2022Updated November 29, 2026

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

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.

One product edit updates every page that shows its price

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 callWhat it does
function_exists( 'wc_get_product' )Stops a fatal error if WooCommerce is deactivated
shortcode_attsSets defaults and ignores unknown attributes
absintForces the ID to a positive integer, so id="abc" becomes 0
wc_get_productReturns 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 checkHides 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_postAllows 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() ) );

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

SymptomCause and fix
Prints as textThe 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 appearsCheck the ID, that the product is published, and that WooCommerce is active
Variable product shows a rangeIntended. To show one variation's price, pass its ID
Price looks staleClear page and object caches after a price change
Different currencyA 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.

Want store customizations built and tested?

A WooCommerce developer reads your brief, today.

FAQ

Questions people ask

12 answers. Can't find yours? Ask an engineer.

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 answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer
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.

Link to this answer

Keep reading

Want an engineer on it?

Tell us what is wrong in plain words. A person reads it, today.