WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

You write a plugin which requires PHP 5.1. Someone tries to install it on a server with PHP 4. How do you deal with that in a safe and user-friendly manner?

share|improve this question
    
Why would you need that? WP doesn't run on PHP < 5 anymore – onetrickpony Dec 13 '12 at 16:10
    
It’s a general question. You might want to require any other higher PHP version. That being said, I wasn’t aware PHP 5.2.4 is required since WP 3.2. – Geert Dec 13 '12 at 16:15

This function and activation hook prevents the plugin from activating and allows you to check for both a minimum PHP and WordPress version.

register_activation_hook( __FILE__, array( 'Your_Plugin_Class_Name', 'activate' ) );

/**
  * Plugin Activation hook function to check for Minimum PHP and WordPress versions
  * @param string $wp Minimum version of WordPress required for this plugin
  * @param string $php Minimum version of PHP required for this plugin
  */
 function activate( $wp = '3.1', $php = '5.2.4' ) {
    global $wp_version;
    if ( version_compare( PHP_VERSION, $php, '<' ) )
        $flag = 'PHP';
    elseif
        ( version_compare( $wp_version, $wp, '<' ) )
        $flag = 'WordPress';
    else
        return;
    $version = 'PHP' == $flag ? $php : $wp;
    deactivate_plugins( basename( __FILE__ ) );
    wp_die('<p>The <strong>Insert PLugin Name Here</strong> plugin requires'.$flag.'  version '.$version.' or greater.</p>','Plugin Activation Error',  array( 'response'=>200, 'back_link'=>TRUE ) );
}
share|improve this answer
    
This do not work if due to php syntax changes the php file can not be parsed at all – Mark Kaplun Apr 14 at 14:53
up vote 6 down vote accepted
/**
 * Plugin Name: Foo
 */

// Check for required PHP version
if (version_compare(PHP_VERSION, '5.1', '<'))
{
    exit(sprintf('Foo requires PHP 5.1 or higher. You’re still on %s.',PHP_VERSION));
}

// The rest of your plugin code ...

I'm not sure since which WP version this happened, but in 3.5 the plugin actually fails to activate and the error message is shown to the user in the admin, which is neat.

The error message is not translated, though. In order to do that you’d have to load your translation files right before the exit call.

share|improve this answer
    
I think I still prefer this method for its simplicity. Also, the plugin is never actually installed, so no need to trigger deinstallation routines in case of a too old PHP version. – Geert Dec 20 '12 at 7:48

You could activate it and show an error message:

// if PHP version is lower than 5.1
if(version_compare(PHP_VERSION, '5.1') < 0){

  // show a message inside the dashboard
  if(is_admin()){

    function my_plugin_notice(){      
      ?>
      <div class="error below-h2">
        <p>
        <?php
          printf(__('The abc plugin requires at least PHP 5.1. You have %s'), PHP_VERSION);
         ?>
        </p>
      </div>
      <?php
    }

    add_action('admin_notices', 'my_plugin_notice');

  }

  // stop here and do nothing further
  return;  
}

// if PHP version is equal or higher than 5.1
require dirname(__FILE__) . '/php51code.php';

It's also probably possible to deactivate it programmatically, before the return statement...

share|improve this answer
    
Yeah, you still need to deactivate it programmatically if you use this setup because in spite of the admin notice, the plugin will show up as activated. – Geert Dec 13 '12 at 16:45
1  
see deactivate_plugins – onetrickpony Dec 13 '12 at 16:50
1  
the function version_compare have a thrid param for the comparison; please use if ( version_compare( phpversion(), '5.1a', '<' ) ) – bueltge Dec 13 '12 at 20:54
1  
That's only required when you want this function to return boolean – onetrickpony Dec 13 '12 at 22:07
    
But, the boolean value is faster as a comparison with the integer value 0. – bueltge Dec 17 '12 at 12:20

I know this is an older question, but for those searching for a good solution, Gary Pendergast had a good route to go that covers a few of the bases mentioned in the other answers (see his post here, I've updated the code below to check the PHP version, but you can use it for virtually any check):

//  In this example, only allow activation on WordPress 3.7 or higherclass 
MyPlugin {
function __construct() {
    add_action( 'admin_init', array( $this, 'check_version' ) );

    // Don't run anything else in the plugin, if we're on an incompatible WordPress version
    if ( ! self::compatible_version() ) {
        return;
    }
}

// The primary sanity check, automatically disable the plugin on activation if it doesn't// meet minimum requirements.static
function activation_check() {
    if ( ! self::compatible_version() ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die( __( 'My Plugin requires PHP 5.1 or higher!', 'my-plugin' ) );
    }
}

// The backup sanity check, in case the plugin is activated in a weird way,
// or the versions change after activation.
function check_version() {
    if ( ! self::compatible_version() ) {
        if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
            deactivate_plugins( plugin_basename( __FILE__ ) );
            add_action( 'admin_notices', array( $this, 'disabled_notice' ) );

            if ( isset( $_GET['activate'] ) ) {
                unset( $_GET['activate'] );
            }
        }
    }
}

function disabled_notice() {
    echo '<strong>' . esc_html__( 'My Plugin requires PHP 5.1 or higher!', 'my-plugin' ) . '</strong>';
}

static function compatible_version() {
    if ( version_compare(PHP_VERSION, '5.1', '<') ) {
        return false;
    }

    // Add sanity checks for other version requirements here

    return true;
}
}
global $myplugin;
$myplugin = new MyPlugin();
register_activation_hook( __FILE__, array( 'MyPlugin', 'activation_check' ) );

I've also saved the code above in a gist.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.