How to check PHP version before switching theme

We recently started created a new theme for our company website. Obviously we were excited about using all the swish (relatively) new PHP features, closures, short array syntax etc etc. As you may or may not know WordPress’ minimum PHP version is PHP 5.2.4 which has none of those features and will error if it finds them. This isn’t really a problem for us as it’s a private theme and we have full control of the server, however I have seen these PHP features used in themes before and obviously it causes some confusion. So here is how to check the PHP version when switching theme.


// Minimum required version
define( 'THEME_REQUIRED_PHP_VERSION', '5.3.0' );

add_action( 'after_switch_theme', 'oz_check_theme_setup' );
function oz_check_theme_setup(){

	// Compare versions
	if ( version_compare( PHP_VERSION, THEME_REQUIRED_PHP_VERSION, '<' ) ) :

		// Theme not activated info message
		add_action( 'admin_notices', 'oz_php_version_admin_notice' );
		function oz_php_version_admin_notice() {
			<div class="update-nag">
				<?php printf( __( 'This theme requires a minimum PHP version of %s. Your version is s%. Your previous theme has been restored.', 'text-domain' ), THEME_REQUIRED_PHP_VERSION, PHP_VERSION ); ?>

		// Switch back to previous theme
		switch_theme( get_option( 'theme_switched' ) );
			return false;