Key Takeaways

  • Detect if a website is hosted on WP Engine with PHP. Use the is_wpe() function to determine if a site is on a WP Engine server, providing a clear method for developers to identify the hosting environment.

  • Functionality limitations of is_wpe(). The is_wpe() function only confirms if a site is on WP Engine, not distinguishing between Production, Development, or Staging environments, ensuring clarity on its scope.

  • Practical application of is_wpe() in development. Developers can create a testfile.php to run is_wpe() and verify if a site is hosted on WP Engine, streamlining environment checks during local development.

  • Ensure function availability before execution. Check if the is_wpe() function exists before running it to prevent errors and ensure accurate detection of WP Engine hosting.

This article describes the is_wpe() function, which developers can use to perform actions based on location (live, 1-click staging, or non-WP Engine sites).

If your workflow includes writing code outside of a WP Engine website and you need a way to tell if a website is hosted on WP Engine through code, we have a function included as part of the WP Engine mu-plugin to help.

This function will tell you if a website is on a WP Engine server: is_wpe()

  • Returns true 1 only if you are on a WP Engine environment.
  • The output of this function is a String. "1" = true and "0" = false.
  • A boolean value is incorrect, meaning is_wpe() === true will always return false.
  • This function will not determine if an environment is Production, Development, or Staging.

When to Use is_wpe() Function

If you are developing on a local environment and you want to test whether the site you’re seeing is on WP Engine or not, you can use the following to create a testfile.php file in the root directory of your website:

<?php
# Use WordPress functions outside of WordPress files
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require('wp-load.php');
# Ensure the function exists before running it and printing out the return value.
if (function_exists('is_wpe')) {
echo is_wpe();
} else {
echo "The function does not exist";
}

Visiting yourdomain.com/testfile.php will then print one of the following:

  • 1 if the site is running on a WP Engine environment
  • 0 if it is not running on a WP Engine environment
  • The function does not exist if the is_wpe() function has not been defined in your environment (this should never be the case on a WP Engine server as long as the WP Engine mu-plugin is installed).

The value that is returned is a String value and not a number data type so make sure to use comparisons accordingly. Here is an example used in a theme template file which first sets an $is_wpe variable to false, and then changes it to true if it detects the appropriate response from the function.

<?php
$is_wpe = false;
if (function_exists('is_wpe')) {
        if ( is_wpe() === "1" ) { $is_wpe = true; }
}
echo $is_wpe ? "yes, this is a WPE server" : "no, this is not a WPE server";
?>

NEXT STEP: PHP version and upgrade guide

Tags: