I need your help. I'm trying to install ACF plugin and give me this error:
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /home/htb/mysite/wp-content/plugins/acf-for-woocommerce/vendor/catsplugins/cp-core/src/common/class-cp-util.php on line 23
This is the line 23:
public static function pathToUrl(string $path): string {
Here's the code:
<?php
namespace CastPlugin;
if (class_exists('CpUtil')) {
return;
}
class CpUtil
{
public static function startsWith($haystack, $needle)
{
return strncmp($haystack, $needle, strlen($needle)) === 0;
}
public static function contains($haystack, $needle)
{
return strpos($haystack, $needle) !== false;
}
public static function pathToUrl(string $path): string {
$ssl = @$_SERVER['HTTPS'];
$serverName = $_SERVER['SERVER_NAME'];
$documentRoot = $_SERVER['DOCUMENT_ROOT'];
$correctPath = $path;
$uri = str_replace($documentRoot, '', $correctPath);
$protocol = $ssl === 'on' ? 'https' : 'http';
return "$protocol://$serverName$uri";
}
}
Any idea how to solve this? Thank you. :)
The version of ACF you're using requires PHP 7.0+. The error message you're getting indicates you're using PHP 5.6 or older. You should talk to your hosting provider about allowing you to upgrade to newer version of PHP.
This error specifically is caused by a new PHP method declaration syntax introduced in PHP 7.0 that allows you to declare that the method you're defining will return an array, which you can read about here: http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
If your hosting provider won't upgrade your PHP version, I suggest you find a new hosting provider. But if you need ACF to work in the meantime, you can go to the advanced page for ACF in the repository and download an older version at the bottom of the page.
Note: Both of these practices (using an outdated PHP version and using an outdate plugin) can be detrimental to the long-term health of your WordPress installation. I'd recommend getting on to PHP 7.2 at least as soon as you're able, which will allow you to use the most recent version of ACF.