I think it can be done.
I use this process to check for minimum version on plugin activation. You can use a variation on this to check during an upgrade (although I think an upgrade to a new version that contained this code would cause the plugin to deactivate).
The code to check minimum versions:
function is_requirements_met()
{
$min_wp = '4.6' ; // minimum WP version
$min_php = '5.3' ; // minimum PHP version
// Check for WordPress version
if ( version_compare( get_bloginfo('version'), $min_wp, '<' ))
{
return false ;
}
// Check the PHP version
if ( version_compare(PHP_VERSION, $min_php, '<'))
{
return false ;
}
return true ;
}
.... and then this to disable the plugin if the versions don't match the requirements (function returned false)
if ( is_plugin_active( plugin_basename(__FILE__)))
{
deactivate_plugins( plugin_basename(__FILE__)) ;
// Hide the default "Plugin activated" notice
if ( isset ($_GET['activate']))
{
unset ($_GET['activate']) ;
}
}
Then I display an admin message to let them know the plugin was disabled:
add_action('admin_notices', 'show_notice') ;
Where the show_notice
function displays a dismissable admin notice.
function show_notice()
{
echo '<div class="notice notice-error is-dismissible"><h3><strong>Plugin </strong></h3><p> cannot be activated - requires at least WordPress 4.6 and PHP 5.3. Plugin automatically deactivated.</p></div>' ;
return ;
}
Works just fine.
Edited to add
What if you put code in your plugin to block the plugin upgrade (something like this: https://stackoverflow.com/questions/17897044/wordpress-how-to-disable-plugin-update ).
The new version of the plugin has a pre-load stub. It will do a check for PHP version. If OK, load the rest of the plugin. If not, don't load the rest of the plugin. Code in pre-load stub will work in all PHP versions.
The pre-stub doesn't contain any PHP 7x code, so preprocessing the stub will not cause an error. The pre-stub will also disable the plugin using something similar to my original answer.
If PHP version is 7x, then pre-load stub loads the rest of the plugin. And Bob's your uncle.