Quantcast
Channel: __ – WP First Aid
Viewing all articles
Browse latest Browse all 3

Plugin Compatibility Check

$
0
0

It’s a common practice to add a line in the readme.txt file of a plugin that says:

Requires at least: 4.4

This is referencing the minimum WordPress version the plugin is expected to work correctly with; it does not necessarily mean the plugin will not work with lower versions but is a very good indicator all the same of what the plugin author expects (and often has tested specifically with).

That’s all well and good, and useful for the end-user of the plugin, but what to do about helping to ensure that the required version is met so the user doesn’t install and activate the plugin on sites using older versions of WordPress.

The following is an example (it also uses an example function posted earlier on this site):

/**
 * Check installed WordPress version for compatibility
 *
 * @package     WPFA_Version_Compatibility_Check
 * @since       1.0
 * @date        November 27, 2015
 *
 * @internal    Version 4.4 being ... well, um ... because
 *
 * @uses        wpfa_plugin_data
 * @uses        __
 * @uses        deactivate_plugins
 * @uses        get_bloginfo
 */
function install() {

	$plugin_data         = wpfa_plugin_data();
	$version_requirement = '4.4';

	$exit_message = sprintf( __( '%1$s requires WordPress version %2$s or newer.', 'wpfa-vcc' ), $plugin_data['Name'], $version_requirement );
	$exit_message .= '<br />';
	$exit_message .= sprintf( '<a href="http://codex.wordpress.org/Upgrading_WordPress" target="_blank">%1$s</a>', __( 'Please Update!', 'wpfa-vcc' ) );

	if ( version_compare( get_bloginfo( 'version' ), floatval( $version_requirement ), '<' ) ) {

		deactivate_plugins( basename( __FILE__ ) );
		exit( $exit_message );

	}

}

Now that we have the code let’s go over what’s happening with it …

First off, we’re getting the plugin header data so we have that information to work with; and, we’re setting the required version of WordPress needed (wanted) for the plugin.

Next, we create an exit message so the user knows what is happening if the plugin cannot be activated due to their site having too old of a WordPress version in use.

Then we do our basic PHP version comparison. If the version is not current enough we make sure the plugin is deactivated; and send the exit message to the screen with a link to a codex article about updating WordPress.


Viewing all articles
Browse latest Browse all 3

Trending Articles