0

We have a custom developed theme that gets broken with a 500 Error when updating to the latest WordPress.

I am receiving this error in the log file:

PHP Fatal error: Cannot redeclare is_iterable() (previously declared in 
/home/smartservice/dev.smartservice.com/wp-includes/compat.php:536) in 
/home/smartservice/dev.smartservice.com/wp- 
content/themes/smartservice/custom_functions.php on line 40

I’m unfamiliar with Php to this extent and i've heard it's outdated. We are running PHP 7.0 on the current site that this error is on.

The code for these lines are - custom-function.php compact.php

function is_iterable ( $var ) {
return ( is_array($var) || $var instanceof Traversable );
}
J.Thomas
  • 13
  • 2

1 Answers1

2

By below code it will clear this error.

if(!function_exists('is_iterable'){
    function is_iterable ( $var ) {
        return ( is_array($var) || $var instanceof Traversable );
    }
}
Akshay Shah
  • 3,391
  • 2
  • 20
  • 33
  • This works. This is because the `is_iterable` function was added in PHP 7.0 (and as a sidenote does the exact same thing that OP's function does). You can't redeclare native functions. https://secure.php.net/manual/en/function.is-iterable.php – Loek Jun 08 '18 at 13:02
  • Yes that is the reason and above code with handle that situation well – Akshay Shah Jun 08 '18 at 13:02
  • Thank you! I will try this out. Much appreciate it! – J.Thomas Jun 08 '18 at 13:13
  • Here is the line of full code line if ( ! function_exists( 'is_iterable' ) ) { /** * Polyfill for is_iterable() function added in PHP 7.1. * * Verify that the content of a variable is an array or an object * implementing the Traversable interface. * * @since 4.9.6 * * @param mixed $var The value to check. * * @return bool True if `$var` is iterable, false otherwise. */ function is_iterable( $var ) { return ( is_array( $var ) || $var instanceof Traversable ); } } Tried to switch out the code it still breaks the theme. – J.Thomas Jun 11 '18 at 15:06
  • Could you explain how this fix's my issue? It seems to be just a duplicate of the code that I have – J.Thomas Jun 15 '18 at 13:30
  • What will be the problem if you remove it from the functions.php – Akshay Shah Jun 15 '18 at 14:28