-1

in a custom plugin I am developing, I am getting this error as soon as I activate the plugin:

Parse error: syntax error, unexpected '[' in /home/site/public_html/wp-content/plugins/my_plugin/plugin.php on line 106

I have hunted down the exact line throwing the fatal error:

$gallery_pictures_id = get_post_meta( $product_id, '_product_image_gallery')[0];

It's worth noting that this line works both in local development and on a staging server.

Why is this happening? What is so weird about it?

3
  • this is a php question, not a wordpress one.... Mar 11, 2015 at 18:57
  • @MarkKaplun Technically, yes. But as the answer by TheDeadMedic below shows, there is a WP-native work-around. So I'd say, let it stand. Mar 11, 2015 at 21:14
  • @DougalCampbell, that is a php solution.... by definition wordpress can not overcome differences in language syntax between different php versions Mar 12, 2015 at 2:31

1 Answer 1

4

This is known as array dereferencing and is only available in PHP 5.4+

To support older versions, you need to assign the array, and then access the index:

$data = get_post_meta( $product_id, '_product_image_gallery' );
$gallery_pictures_id = $data[0];

Having said that, in your case use the third argument "single":

$gallery_pictures_id = get_post_meta( $product_id, '_product_image_gallery', true );

Not the answer you're looking for? Browse other questions tagged or ask your own question.