Skip Navigation

[Resolved] PHP 8.0 error

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 1 year ago.

Assisted by: Waqar.

Author
Posts
#2667307

Tell us what you are trying to do?
My site was recently migrated to a new server running PHP 8.0 and I'm getting an error on only certain pages that utilize this function. This only occurs on a few pages, most others seem to operate okay. When I comment this out, page loads (without the currency being added). I'm hoping you can help.

Function:
//This formats number to currency format
function format_my_number($atts)
{
$atts = shortcode_atts(
array(
'num' => '',
'sym' => '',
),
$atts
);
$num = $atts['num'];

return $atts['sym'].''.number_format($num, 0, '.', ',');
}
add_shortcode('format-currency', 'format_my_number');

Error message:
#0 /home/nk21itv2x7ao/public_html/wp-content/themes/total-child-theme/functions.php(327): number_format('', 0, '.', ',')
#1 /home/nk21itv2x7ao/public_html/wp-includes/shortcodes.php(433): format_my_number(Array, '', 'format-currency')
#2 [internal function]: do_shortcode_tag(Array)
#3 /home/nk21itv2x7ao/public_html/wp-includes/shortcodes.php(273): preg_replace_callback('/\\[(\\[?)(format...', 'do_shortcode_ta...', '

Is there any documentation that you are following?
No

Link where error occurs:
hidden link

When loops are placed individually by community, the show up without issue. But the "Search" function is not present.

hidden link

#2667467

Hi,

Thank you for contacting us and I'd be happy to assist.

The error can be experienced when the 'num' attribute in the shortcode is given an empty or a non-numeric value.

To avoid this error, you can include an 'is_numeric' check in your custom shortcode's function:


function format_my_number($atts)
{
	$atts = shortcode_atts(
		array(
			'num' => '',
			'sym' => '',
			),
		$atts
	);

	$num = $atts['num'];
	if (is_numeric($num)) {
		return $atts['sym'].''.number_format($num, 0, '.', ',');
	}

}
add_shortcode('format-currency', 'format_my_number');

I hope this helps and please let me know if you need further assistance.

regards,
Waqar