- WordPress 3 Plugin Development Essentials
- Brian Bondari Everett Griffiths
- 342字
- 2021-04-09 21:20:01
Checking WordPress versions
It is common for scripts to perform some "pre-flight" tests at runtime to ensure that they can execute correctly. Common examples of these types of tests include checking the version of PHP or of WordPress. To avoid any potential catastrophes that could occur if our plugin is activated on incompatible WordPress versions, we will perform a simple WordPress version check, and you can use this format as a guideline for how you might perform similar pre-flight tests. Let's add another PHP constant to define the minimum version of WordPress required. We want this plugin to require WordPress version 3 or greater:
define('DIGGTHIS_MIN_WORDPRESS_VERSION', '3.0');
Again, we place this near the top of our plugin in the area we allotted for configuration and settings.
In order to check the current version of WordPress, we can use the global WordPress variable $wp_version
. We can then use the PHP function version_compare()
to compare the current version with the minimum required. The following is how we updated the diggthis_check_wordpress_version()
function:
/** * Checks that the current version of WordPress is current enough. * * @return none exit on fail. */ function diggthis_check_wordpress_version() { global $wp_version; $exit_msg='"Digg This" requires WordPress ' .DIGGTHIS_MIN_WORDPRESS_VERSION .' or newer. <a >Please update!</a>'; if (version_compare($wp_version,DIGGTHIS_MIN_WORDPRESS_VERSION,'<')) { exit ($exit_msg); } }
This will ensure that only users who are running WordPress 3.0 or later will be able to use the plugin. Technically, everything we've done would work on many older versions of WordPress, but since we authored this plugin on WordPress 3, it's best not to assume that it will work on older versions.
To execute this function, we will tie into the same init
method as we did before. Simply add this below our existing add_action()
and add_filter()
functions:
add_action('init', 'diggthis_check_wordpress_version');
To ensure that this works, try temporarily changing the minimum version constant:
define('DIGGTHIS_MIN_WORDPRESS_VERSION', '4.0');
Save your file and refresh your browser. You should see the $exit_msg
displayed:
"Digg This" requires WordPress 4.0 or newer. Please update!
Once you've verified that the error message works, you can change the constant back.
- 中文版AutoCAD 2016從入門到精通
- After Effects CC影視后期制作實(shí)戰(zhàn)從入門到精通
- Premiere Pro 2022短視頻剪輯、調(diào)色與特效制作實(shí)戰(zhàn)(全彩微課版)
- 中文版3ds Max 2016/VRay效果圖制作實(shí)戰(zhàn)基礎(chǔ)教程
- Microsoft Dynamics GP 2010 Reporting
- 有趣的Flutter:從0到1構(gòu)建跨平臺(tái)App
- JBoss Tools 3 Developers Guide
- Puppet權(quán)威指南
- Word-Excel-PowerPoint 2010三合一辦公應(yīng)用實(shí)戰(zhàn)從入門到精通(超值版)
- Photoshop+CorelDRAW平面設(shè)計(jì)案例實(shí)戰(zhàn):從入門到精通(視頻自學(xué)全彩版)
- Quality Assurance for Dynamics AX-Based ERP Solutions
- 數(shù)碼攝影修圖師完全手冊(cè)(第2卷)
- Microsoft Dynamics Sure Step 2010
- 3ds Max三維動(dòng)畫制作項(xiàng)目式教程
- 中老年學(xué)微信(圖解大字版)