Title: Note for Themes about Core PHP Compatibility Changes in WordPress 5.2
Author: williampatton
Published: April 1, 2019
Last modified: July 10, 2019

---

 [  ](https://profiles.wordpress.org/williampatton/) [williampatton](https://profiles.wordpress.org/williampatton/)
11:43 am _on_ April 1, 2019      

# 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. [https://www.php.net/manual/en/index.php](https://www.php.net/manual/en/index.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/](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.

    ```wp-block-code
    <?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
    }
    ```

### Share this:

 * [  Threads ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=bluesky)
 * [  Facebook ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=facebook)
 * [  X ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=x)
 * [  LinkedIn ](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?share=linkedin)

 * [Login to Reply](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fmake.wordpress.org%2Fthemes%2F2019%2F04%2F01%2Fcore-php-compatibility-changes-in-wordpress-5-2%2F%23respond&locale=en_US)

### Leave a Reply 󠀁[Cancel reply](https://make.wordpress.org/themes/2019/04/01/core-php-compatibility-changes-in-wordpress-5-2/?output_format=md#respond)󠁿

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fmake.wordpress.org%2Fthemes%2F2019%2F04%2F01%2Fcore-php-compatibility-changes-in-wordpress-5-2%2F&locale=en_US)
to post a comment.

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)

# Post navigation

[← Addition of new ‘wp_body_open’ hook.](https://make.wordpress.org/themes/2019/03/29/addition-of-new-wp_body_open-hook/)

[New Privacy Policy page helpers coming in 5.2 →](https://make.wordpress.org/themes/2019/04/03/new-privacy-policy-page-helpers-coming-in-5-2/)