Using WordPress ‘graceful_fail()’ PHP function

The graceful_fail() WordPress PHP function is a deprecated function used to gracefully fail in older WordPress versions. It is recommended to use wp_die() instead.

Usage

graceful_fail('Error message');

Parameters

  • None

More information

See WordPress Developer Resources: graceful_fail

This function is deprecated since WordPress version 1.5.0. It is recommended to use wp_die() instead.

Examples

Graceful fail with a simple error message

Display a simple error message and exit gracefully.

// Display error message and exit gracefully
graceful_fail('Something went wrong. Please try again later.');

Graceful fail when a condition is not met

Gracefully exit if a variable does not match a specific value.

$number = 5;

// Check if the number is equal to 10
if ($number != 10) {
    graceful_fail('The number must be 10.');
}

Graceful fail inside a function

Create a function that checks if a string contains a specific word and gracefully fails if not.

function check_word($string) {
    if (strpos($string, 'word') === false) {
        graceful_fail('The string must contain the word "word".');
    }
}

check_word('This is a test sentence.');

Graceful fail with HTML error message

Display an error message with HTML content and exit gracefully.

// Display error message with HTML content and exit gracefully
graceful_fail('<strong>Error:</strong> Something went wrong. Please <a href="http://example.com">go back</a> and try again.');

Graceful fail after performing an action

Perform an action and gracefully exit if it fails.

$result = false;

// Perform some action
// ...

// Check if the action was successful
if (!$result) {
    graceful_fail('The action failed. Please try again.');
}