Fix unexpected ‘[’ error of XML Sitemap & Google News feeds
If you upgrade your XML Sitemap & Google News feeds to version 4.7, your wordpress may get a error message:
Parse error: syntax error, unexpected '[' in ...wp-content\plugins\xml-sitemap-feed\includes\class-xmlsitemapfeed.php on line 703...
.
If you don’t want to wait a newer version to fix this error, and don’t want to downgrade this plugin, you can fix this error by yourself.
If you are in a hurry, go the end of this article, and read the section How to fix it.
The problem
After upgrading the plugin XML Sitemap & Google News feeds to version 4.7, you will find this plugin is off. Try turn it on, and fail with a error message like:
Parse error: syntax error, unexpected '[' in ...wp-content\plugins\xml-sitemap-feed\includes\class-xmlsitemapfeed.php on line 703...
.
The reason
Let’s review the code near the line 703:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function get_language( $id ) { $language = null; if ( empty($this->blog_language) ) { // get site language for default language $blog_language = convert_chars(strip_tags(get_bloginfo('language'))); $allowed = ['zh-cn','zh-tw']; //This is 703 if ( !in_array($blog_language,$allowed) ) { // bloginfo_rss('language') returns improper format so // we explode on hyphen and use only first part. $expl = explode('-', $blog_language); $blog_language = $expl[0]; } $this->blog_language = !empty($blog_language) ? $blog_language : 'en'; } |
The key is line 703, $allowed = ['zh-cn','zh-tw'];
. If you know PHP, what do you think, now?
Nothing weird.
Yes, Nothing weird there. This line creates a array, just clean, clear and simple.
The real problem is not the code, but the environment of your server.
Refer to this article Arrays, see this:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $array = array( "foo" => "bar", "bar" => "foo", ); // as of PHP 5.4 $array = [ "foo" => "bar", "bar" => "foo", ]; ?> |
You should know that you can’t create a array with ‘[]’ until you running PHP 5.4 or higher.
If your PHP is 5.3 or lower, you will get error.
In the WordPress minimum requirements, it tells that WordPress also works with PHP 5.2.4+ ,so you can run wordpress on a low version of PHP, but not all plugins.
How to fix it
Just know your server’s PHP version may be 5.2 or 5.3. Let’s just create a array with a traditional way:
Edit the line 703.
Before:
1 |
$allowed = ['zh-cn','zh-tw']; |
After:
1 |
$allowed = array('zh-cn','zh-tw'); |
Fixed!
1 comment
夏天烤洋芋
2016 年 5 月 11 日 在 下午 1:32 (UTC 8) Link to this comment
不是很懂这说的是什么?