Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be of type int|float, string given in

Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be of type int|float, string given in /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/includes/template_builtin.php:33

Yesterday I noticed the above error on this website.  My home page was still online as normal, but each of the posts was throwing an error.  The error that showed up was actually:

There has been a critical error on this website.

Learn more about troubleshooting WordPress.

I first noticed it because my broken link detector plugin showed some broken links.  It turns out they were to my own website.  When visiting any posts, I got the critical error message.  I checked my other websites and didn’t see any of these issues.  So I realized I had to dig in a little deeper.

First thing to do when getting an error message like that is to turn on debugging mode in WordPress.  To do that, you edit the wp-config.php file located in your home directory.  Just before the “stop editing” line near the end, you add or change:

define( 'WP_DEBUG', true );

After turning on debugging mode, I visited a page giving me an error and got the following details:

Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be of type int|float, string given in /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/includes/template_builtin.php:33 
Stack trace: 
#0 /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/includes/template_builtin.php(33): round() 
#1 /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php(1655): include('...') 
#2 /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php(1527): YARPP->get_template_content() 
#3 /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php(1384): YARPP->display_related() 
#4 /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/classes/YARPP_Core.php(2020): YARPP->display_basic() 
#5 /rmalm/public_html/wp-includes/class-wp-hook.php(308): YARPP->the_content() 
#6 /rmalis.com/public_html/wp-includes/plugin.php(205): WP_Hook->apply_filters() 
#7 /rmalis.com/public_html/wp-includes/formatting.php(3885): apply_filters() 
#8 /rmalis.com/public_html/wp-includes/class-wp-hook.php(308): wp_trim_excerpt() 
#9 /rmalis.com/public_html/wp-includes/plugin.php(205): WP_Hook->apply_filters() 
#10/rmalis.com/public_html/wp-includes/post-template.php(431): apply_filters() 
#11 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/opengraph/class-opengraph.php(147): get_the_excerpt() 
#12 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/opengraph/class-opengraph.php(124): RankMath\OpenGraph\OpenGraph->fallback_description() 
#13 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/opengraph/class-twitter.php(174): RankMath\OpenGraph\OpenGraph->get_description() 
#14 /rmalis.com/public_html/wp-includes/class-wp-hook.php(308): RankMath\OpenGraph\Twitter->description() 
#15 /rmalis.com/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() 
#16 /rmalis.com/public_html/wp-includes/plugin.php(565): WP_Hook->do_action() 
#17 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/traits/class-hooker.php(88): do_action_ref_array() 
#18 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/opengraph/class-opengraph.php(73): RankMath\OpenGraph\OpenGraph->do_action() 
#19 /rmalis.com/public_html/wp-includes/class-wp-hook.php(308): RankMath\OpenGraph\OpenGraph->output_tags() 
#20 /rmalis.com/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() 
#21 /rmalis.com/public_html/wp-includes/plugin.php(565): WP_Hook->do_action() 
#22 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/traits/class-hooker.php(88): do_action_ref_array() 
#23 /rmalis.com/public_html/wp-content/plugins/seo-by-rank-math/includes/frontend/class-head.php(156): RankMath\Frontend\Head->do_action() 
#24 /rmalis.com/public_html/wp-includes/class-wp-hook.php(308): RankMath\Frontend\Head->head() 
#25 /rmalis.com/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() 
#26 /rmalis.com/public_html/wp-includes/plugin.php(517): WP_Hook->do_action() 
#27 /rmalis.com/public_html/wp-includes/general-template.php(3043): do_action() 
#28 /rmalis.com/public_html/wp-content/themes/astra/header.php(26): wp_head() 
#29 /rmalis.com/public_html/wp-includes/template.php(783): require_once('...') 
#30 /rmalis.com/public_html/wp-includes/template.php(718): load_template() 
#31 /rmalis.com/public_html/wp-includes/general-template.php(48): locate_template() 
#32 /rmalis.com/public_html/wp-content/themes/astra/single.php(15): get_header() 
#33 /rmalis.com/public_html/wp-includes/template-loader.php(106): include('...') 
#34 /rmalis.com/public_html/wp-blog-header.php(19): require_once('...') 
#35 /rmalis.com/public_html/index.php(17): require('...') 
#36 {main} thrown in /rmalis.com/public_html/wp-content/plugins/yet-another-related-posts-plugin/includes/template_builtin.php on line 33

There has been a critical error on this website.

Learn more about troubleshooting WordPress.

That’s a lot of gobblygook that I don’t really understand (yet).  I did recognize that the main error appears to be line 33 of a part of my Yet Another Related Posts plugin (YARPP) I use to show related posts at the end of each message.  I also noticed that some of the lines include the plugin SEO by Rank Math.

First, I tried disabling YARPP and that got rid of the error message.  I found that odd as I also use that plugin on other sites and I’ve never noticed that error before.  I also looked and the plugin hasn’t been updated in several months, yet the problem started within the past few days.

Second, I looked to see when the SEO by Rank Math plugin was updated, and I noticed that was updated about 2 days ago.  To see if maybe that was contributing to the problem, I tried disabling SEO by Rank Math and re-enabling YARPP and interestingly, that also prevented any error messages.  So it seems to be something with the two plugins running at the same time.

What added to the confusion, is I have another website using both of those plugins, yet that site was not having any errors.  The only thing different between the two sites is that the other site is set to use php version 7.4 and this site was set to use php 8.0.  To test whether that was a factor, I changed the php version to 7.4 and re-enabled both plugins.  That also prevented the error from occurring.

So, I can now conclude that the error is due to using php version 8, along with SEO by Rank Math (likely something changed in their most recent update) and YARPP.

Doing some more research on this particular error, it does seem other people have had similar errors when updating to php 8.0, but with other plugins or programs.  It seems to be something to do with how php 8.0 handles the

round($val)

and changing it as follows will resolve the errors:

round((int)$val)

I looked at line 33 of the code that is identified as throwing the error:

$round = round( get_the_score(), 1 );

Which would round a number for get_the_score() to the nearest number including the 10ths column (the ,1 means how many digits after the decimal point to round to).  But I’m thinking that if get_the_score() is 0, it will throw an error with php8.

Based on what worked in other scripts, I tried:

$round = round( (int)get_the_score(), 1 );

And that did fix the error from being thrown, but I realized that (int) was not the best choice for this use.  I am basically trying to round the relatedness score to the nearest 10ths.  If I use the (int) command first, it will truncate the number, removing anything after the decimal point.  Rounding after that will essentially round the number to the nearest integer, but it is already truncated to an integer.

I then tried:

$round = round((float) get_the_score(), 1 );

And that seems to have fixed the problem completely and the “get_the_score” appears to be rounded to the nearest 10ths instead of just a truncated integer.

 

Upon further review though, I’m seeing there is some error with Rank Math messing up the YARPP values.  It looks like it is still listing the correct related threads, but it is showing as 0 for a score for many pages relatedness when it should definitely not be 0.  I’ll have to see what Rank Math did with the most recent update to mess with the get_the_score values.

Update 11/13/2022:

It looks like the bug was fixed in Yet Another Related Posts Plugin, with the author using the same solution that I came up with.

Looking closer, the solution does prevent the error from occurring, but there is still some problem between how Rank Math (and likely other plugins) interact with the YARPP.  For some reason, Rank Math is changing the scores to a string rather than a number, and then they get displayed as 0’s instead of their actual values.  Interestingly, the related posts shown are the same whether I have Rank Math enabled or not – the only difference is that the actual score shows when Rank Math is disabled and the number 0 shows when Rank Math is enabled.  At this point, I’m not spending the time to understand and fix the errors.  If I do, I will update the post at that time.

Leave a Reply