Skip to content

Instantly share code, notes, and snippets.

@johnalarcon
Last active May 6, 2018 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnalarcon/aa5edeca200bf7dad4fec347f1b63e41 to your computer and use it in GitHub Desktop.
Save johnalarcon/aa5edeca200bf7dad4fec347f1b63e41 to your computer and use it in GitHub Desktop.
A simple object-oriented approach to ensure that a minimum version of PHP is installed before allowing installation of your WordPress plugin. This code should live in your plugin's root file; your actual (PHP7) plugin code is included at line 73.
<?php
/**
* Plugin Name: My Plugin Name
* Description: Create a robust admin settings page for your WordPress plugin in minutes! More than 20 HTML5 input types are supported, including clickable labels, input hints, and even beautiful tooltips. Simply define your needed sections and inputs – everything else is automatic. Your settings are displayed with tabbed navigation that looks exactly like WordPress built it – no wonky styles, no silly cartoon characters, no in-your-face nags. The CSS totals less than 50 lines and the Javascript is less 25. This is built entirely inline with WordPress core standards and has no outside dependencies. The interface loads super-fast – you can even navigate across sections/tabs, changing settings along the way, and then click “Save” just once to save all the settings at once. Because I love you. But, you know, just as friends.
* Version: 1.0.0
* Author: John Alarcon
* Author URI: https://github.com/johnalarcon/my-plugin-name
* Text Domain: my-plugin-name
* Domain Path: /languages
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See http://www.gnu.org/licenses/gpl-2.0.txt.
*/
class MyPluginName_PhpGate {
/**
* Minimum PHP version for this plugin
*
* If you use a single digit, x, all versions >= x.0.0 will be allowed. When
* you need to be more specific, you can do so. For example, if the variable
* is set to '7.2.3', then sites running on PHP < 7.2.3 would not be able to
* complete the installation – sites running on PHP >= 7.2.3 would get right
* through the installation.
*
* @var string Minimum PHP version required for this plugin.
*
* @since 1.0.0
*/
public $minimum_php = '7';
/**
* Simple constructors have more friends.
*
* @since 1.0.0
*/
public function __construct() {
$this->verify_php_requirement();
}
/**
* This method verifies if the PHP version meets the minimum requirement and
* then adds an action to either prevent installation [or] load the plugin.
*
* @since 1.0.0
*/
public function verify_php_requirement() {
if (version_compare(PHP_VERSION, $this->minimum_php, '<')) {
add_action('admin_notices', array($this, 'prevent_installation'));
} else {
add_action('init', array($this, 'load_plugin'));
}
}
/**
* A method to load plugin code only when the minimum PHP version is met.
*
* @since 1.0.0
*/
public function load_plugin() {
include_once('my-plugin-name-plugin.php');
}
/**
* A method to prevent installation when the minimum PHP version requirement
* is not met and, instead, print an error message.
*
* @since 1.0.0
*/
public function prevent_installation() {
// Prevent installation.
deactivate_plugins(plugin_basename(__FILE__));
if (isset($_GET['activate'])) {
unset($_GET['activate']);
}
// Print an error message.
$markup = '<div class="error"><p>';
$markup .= sprintf(
__('This plugin requires <strong>PHP %s</strong>. Your current version is <strong>PHP %s</strong>.', 'my-plugin-name'),
$this->minimum_php,
phpversion()
);
$markup .= '</p></div>'."\n";
echo $markup;
}
}
new MyPluginName_PhpGate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment