Note for Themes about Core PHP Compatibility Changes in WordPress 5.2

With the release of WordPress 5.2 the minimum recommended PHPPHP PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. http://php.net/manual/en/intro-whatis.php. version changes from minimum of PHP 5.2 to a minimum of PHP 5.6.

Themes hosted in the WordPress.orgWordPress.org The community site where WordPress code is created and shared by the users. This is where you can download the source code for WordPress core, plugins and themes as well as the central location for community conversations and organization. https://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
}