在开发 WordPress 插件的时候,我们很可能需要检查当前网站的配置信息。具体来说,就是当前网站所在的服务器的 PHP 版本,以及正在使用的 WordPress 版本。
检查 PHP 最低版本
假设你的插件必须在 php 5.3.0 及以上版本中才能正常使用,那么你可以将下面的函数添加到插件中:
// 检查当前服务器的 PHP 版本
function check_php_version($ver) {
$php_version = phpversion();
if (version_compare($php_version, $ver) < 0) {
throw new Exception("This plugin requires at least version $ver of PHP. You are running an older version ($php_version). Please upgrade!");
}
} |
// 检查当前服务器的 PHP 版本
function check_php_version($ver) {
$php_version = phpversion();
if (version_compare($php_version, $ver) < 0) {
throw new Exception("This plugin requires at least version $ver of PHP. You are running an older version ($php_version). Please upgrade!");
}
}
然后使用下面的代码检查:
check_php_version('5.3.0'); |
check_php_version('5.3.0');
检查 WordPress 最低版本
假设你在插件中使用 WordPress 3.6 新添加的 Heartbeat API ,那我们可以添加下面的函数:
// 检查当前使用的 WordPress 版本
function check_wp_version($ver) {
global $wp_version;
if (version_compare($wp_version, $ver) < 0) {
throw new Exception("This plugin requires at least version $ver of WordPress. You are running an older version ($php_version). Please upgrade!");
}
} |
// 检查当前使用的 WordPress 版本
function check_wp_version($ver) {
global $wp_version;
if (version_compare($wp_version, $ver) < 0) {
throw new Exception("This plugin requires at least version $ver of WordPress. You are running an older version ($php_version). Please upgrade!");
}
}
然后通过下面的代码检查:
额外提醒
使用上面的方法检查 PHP 和 wordpress,如果达不到最低版本,就会抛出错误提示,阻止插件的运行。当然,你可以根据自己的需要修改提示的方法。
参考资料:http://themergency.com/check-minimum-php-and-wordpress-versions/
来源:
https://www.wpdaxue.com/check-minimum-php-and-wordpress-versions.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32243.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57