Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.
Code Sample:
function my_function( $array ) {
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
The Warning:
Warning: Illegal string offset 'where' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 14
Warning: Illegal string offset 'echo' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15
Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15
Warning: Illegal string offset 'content' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16
Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16
Warning: Illegal string offset 'shortcode' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 18
Warning: Illegal string offset 'devs' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 19
For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!
The usage of the function is_array()
in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* @params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
It looks like the function is expecting an array and is getting a string instead.
You can require an array in the function definition.
function my_function(array $array) { ...
Then if you call it with something other than an array, you'll get a TypeError.
Unfortunately, there will still be a problem somewhere else in your code, where something you thought was an array is actually a string.
Setting up your function like this will generate an error earlier, which is good, because it will make it more obvious where the problem is. If you modify your function to ignore the problem instead, it will probably just create more confusing behavior and potentially different errors.