
When you install version 2.2.1 of the WordPress Extra theme from Elegant Themes on a server that runs PHP 7.2, you’ll immediately see the following warning message spread out all throughout the site:
Warning: count(): Parameter must be an array or an object that implements Countable in F:\websites\wpextra\wp-content\themes\Extra\includes\template-tags.php on line 54
The warnings are the result of a change in PHP 7.2 which causes the count() function to now yield a warning when an invalid countable type is passed into it. In short when the value is neither an array nor an object that implements Countable.
Since this only happens with PHP 7.2 and it’s not actually an error, but a warning, Elegant Themes have already stated that they aren’t going to create a hot-fix for it. Their proposed solution is to either switch to a lower version of PHP, or to turn off warnings altogether. Which does a good job of hiding the issue. Unfortunately this also hides other warnings you might come across while working on the site.
Luckily we can easily resolve this problem ourselves.
The error message already provides us with a convenient hint of where to look: wp-content/themes/Extra/includes/template-tags.php on line 54.
Starting from line 53, we find the following bit of code:
$review_breakdowns = get_post_meta( $post_id, '_post_review_box_breakdowns', true );
if ( 1 == count( $review_breakdowns ) && empty( $review_breakdowns[0]['title'] ) && empty( $review_breakdowns[0]['rating'] ) ) {
return false;
}
What happens is that if there is no post meta-data, then the value returned from the get_post_meta function is empty (in most cases an empty string). Now an empty value is neither an array, nor does it implement Countable, so passing it into the count() function will trigger a warning.
To solve this, all we need to do is make sure that the returned value is not empty, before we pass it into the count method. For that we can simply replace the code which starts on line 53 of the wp-content/themes/Extra/includes/template-tags.php file with the following snippet:
$review_breakdowns = get_post_meta( $post_id, '_post_review_box_breakdowns', true );
if ( !empty($review_breakdowns) && 1 == count( $review_breakdowns ) && empty( $review_breakdowns[0]['title'] ) && empty( $review_breakdowns[0]['rating'] ) ) {
return false;
}
By adding the !empty($review_breakdowns) we prevent any other code on that line, including the count() function call whenever the value of $review_breakdowns is empty.
Usually I would recommend using a child theme for changes like this. However, since the issue is supposed to get fixed in the next version of the Extra theme, you can simply modify the code of the theme itself.