Note for Themes about Core PHP Compatibility Changes in WordPress 5.2

With the release of WordPress 5.2 the minimum recommended PHP version changes from minimum of PHP 5.2 to a minimum of PHP 5.6.

Themes hosted in the WordPress.org directory can choose to fully support any version of PHP they want – but you also must ensure it does not cause any fatal errors when it is run on any of the PHP versions that WordPress itself supports.

Themes should include their own method of handling PHP version compatibility and providing a graceful fallback when it’s run on unsupported versions. The most common way to do this is by preventing activation of your theme on versions of PHP which you do not support and notifying the user about why.

You can do that using the after_switch_theme hook, the phpversion() function or PHP_VERSION constant and version_compare(). Here is some example code that prevents theme activation on a server that’s PHP version is below 5.6.

<?php

/**
 * Set a constant that holds the theme's minimum supported PHP version.
 */
define( 'THEMESLUG_MIN_PHP_VERSION', '5.6' );

/**
 * Immediately after theme switch is fired we we want to check php version and
 * revert to previously active theme if version is below our minimum.
 */
add_action( 'after_switch_theme', 'themeslug_test_for_min_php' );

/**
 * Switches back to the previous theme if the minimum PHP version is not met.
 */
function themeslug_test_for_min_php() {

	// Compare versions.
	if ( version_compare( PHP_VERSION, THEMESLUG_MIN_PHP_VERSION, '<' ) ) {
		// Site doesn't meet themes min php requirements, add notice...
		add_action( 'admin_notices', 'themeslug_min_php_not_met_notice' );
		// ... and switch back to previous theme.
		switch_theme( get_option( 'theme_switched' ) );
		return false;

	};
}

/**
 * An error notice that can be displayed if the Minimum PHP version is not met.
 */
function themeslug_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 this theme.', 'themeslug' ); ?> <br />
			<?php
			printf(
				/* translators: 1 is the current PHP version string, 2 is the minmum supported php version string of the theme */
				esc_html__( 'Actual version is: %1$s, required version is: %2$s.', 'themeslug' ),
				PHP_VERSION,
				THEMESLUG_MIN_PHP_VERSION
			); // phpcs: XSS ok.
			?>
		</p>
	</div>
	<?php
}