WooCommerce: Product Title Shortcode

WordPress shortcodes allow you to save time. For example, on my WooCommerce product pages I found myself typing the product title at least three times: in the product name input field, ok, but also again in the product long description and in a custom product tab inside a text link.

So, while finding ways to save time and reduce errors and typos, I coded the shortcode below so that I could avoid writing the title 3 times (and they’re very long!).

How does it work? Simply use [post_title] in a WooCommerce product, WordPress page, WordPress post or custom post and this will magically turn into the current post title. Simple!

In this custom WooCommerce product tab I have a text link where I need to pass the current product name as URL parameter, so that my contact form is pre-filled with the correct product name. By using the shortcode below, I’m able to save time and avoid errors.

PHP Snippet: Print Current WooCommerce Product / WordPress Post Title Via a Shortcode

Yep, it’s only one line of PHP magic!

/**
 * @snippet       Echo Current Product/Post Name (Shortcode)
 * @usage         Use [post_title] on the current post
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_shortcode( 'post_title','get_the_title' );

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

5 thoughts on “WooCommerce: Product Title Shortcode

  1. Very useful as always. Thank you Rodolfo.

  2. hello, i pasted code given by about “WooCommerce: Product Title Shortcode”
    in mobile mode im using page title as shortcode on header but some page title have more length of words how i can limits page title length by words? please help me.

    1. Hello Alan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. What file does the PHP code snippet go in?

    1. Hello Mark, sorry for the delay! Custom code goes preferably into functions.php of your child theme – or in a Code Snippet plugin snippet. Let me know

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *