Storing woocommerce_page_title in a variable and display it in Woocommerce Storing woocommerce_page_title in a variable and display it in Woocommerce wordpress wordpress

Storing woocommerce_page_title in a variable and display it in Woocommerce


The template function woocommerce_page_title() is echoed by default as you can see in its source code… But there is an optional argument to be used to return the data, instead of echoing it. For that you need to turn this argument from (default) true to false.

This is useful when you need to set this data in a variable, just as you are trying to do. So the functional code should be a bit different:

$html .= '<h1 class="page-title">' . woocommerce_page_title( false ); . '</h1>';

Tested and works.

Or alternatively you can use php buffering functions, to get the same thing, this way:

ob_start();?><h1 class="page-title"><?php woocommerce_page_title(); ?></h1><?php$html .= ob_get_clean();

Nothing will be outputted and the data will be appended in the $html variable.


Try this code:

$html .= '<h1 class="page-title">' . woocommerce_page_title() .'</h1>';

Instead of:

$html .= '<h1 class="page-title">'<?php woocommerce_page_title(); ?>'</h1>';