wp_check_php_version() WordPress Function

The wp_check_php_version() function is used to check the version of PHP installed on a WordPress site. This function is important because it ensures that WordPress sites are running the correct version of PHP, which is necessary for compatibility with plugins and themes. The function will return an error if the site is running an outdated or unsupported version of PHP.

wp_check_php_version() #

Checks if the user needs to update PHP.


Return

(array|false) Array of PHP version data. False on failure.


Top ↑

Source

File: wp-admin/includes/misc.php

1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
function wp_check_php_version() {
    $version = phpversion();
    $key     = md5( $version );
 
    $response = get_site_transient( 'php_check_' . $key );
 
    if ( false === $response ) {
 
        if ( wp_http_supports( array( 'ssl' ) ) ) {
            $url = set_url_scheme( $url, 'https' );
        }
 
        $url = add_query_arg( 'php_version', $version, $url );
 
        $response = wp_remote_get( $url );
 
        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
            return false;
        }
 
        /**
         * Response should be an array with:
         *  'recommended_version' - string - The PHP version recommended by WordPress.
         *  'is_supported' - boolean - Whether the PHP version is actively supported.
         *  'is_secure' - boolean - Whether the PHP version receives security updates.
         *  'is_acceptable' - boolean - Whether the PHP version is still acceptable for WordPress.
         */
        $response = json_decode( wp_remote_retrieve_body( $response ), true );
 
        if ( ! is_array( $response ) ) {
            return false;
        }
 
        set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
    }
 
    if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
        /**
         * Filters whether the active PHP version is considered acceptable by WordPress.
         *
         * Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
         *
         * This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
         * that this filter can only make this check stricter, but not loosen it.
         *
         * @since 5.1.1
         *
         * @param bool   $is_acceptable Whether the PHP version is considered acceptable. Default true.
         * @param string $version       PHP version checked.
         */
        $response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
    }
 
    return $response;
}


Top ↑

Changelog

Changelog
VersionDescription
5.1.1Added the 'wp_is_php_version_acceptable' filter.
5.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.