Skip to content

Instantly share code, notes, and snippets.

@pattonwebz
Created April 12, 2018 03:19
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 pattonwebz/3a90d7b9cbc89d6b7a92b9355af29569 to your computer and use it in GitHub Desktop.
Save pattonwebz/3a90d7b9cbc89d6b7a92b9355af29569 to your computer and use it in GitHub Desktop.
Halt theme activation if attempting to activate on unsupported older php versions.
<?php
/**
* Define the min version you support. Here I use 5.6 as example but change it to match your support version.
*/
define( 'PREFIX_MIN_PHP_VERSION', '5.6' );
/**
* Attach a function to check php version on the `after_theme_switch` hook.
*/
add_action( 'after_switch_theme', 'prefix_test_for_min_php' );
/**
* Compair PHP versions against theme defined minimum version required. If min
* version is not met add an admin notice informing the user and revert to the
* previously active theme.
*
* @return boolean false on failure and nothing on success.
*/
function prefix_test_for_min_php() {
// Compare PHP versions against our defined min version.
if ( version_compare( phpversion(), PREFIX_MIN_PHP_VERSION, '<' ) ) {
// Theme doesn’t meet min php requirements - add admin notice.
add_action( 'admin_notices', 'prefix_min_php_not_met_notice' );
// Switch back to previous theme to prevent errors.
switch_theme( get_option( 'theme_switched' ) );
return false;
};
}
/**
* A partial to generate a dismissable admin notice that should appear if the
* user attempts to activate this theme on an unsupported php version.
*/
function prefix_min_php_not_met_notice() {
?>
<div class="notice notice-error is_dismissable">
<p>
<?php esc_html_e( 'You need to update your PHP version to run themename.', 'prefix' ); ?> <br />
<?php esc_html_e( 'Actual version is:', 'prefix' ); ?> <strong><?php echo phpversion(); // WPCS: XSS ok. ?></strong>, <?php esc_html_e( 'required is', 'prefix' ); ?> <strong><?php echo esc_html( PREFIX_MIN_PHP_VERSION ); ?></strong>
</p>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment