wppaste
WordPress

WP_Error::__construct( string|int $code = '', string $message = '', mixed $data = '' )

Since
2.1.0
Source
wp-includes/class-wp-error.php:61

Populates a new WP_Error object with an initial error code, message, and optional data by internally calling add(). Passing an empty string (or any falsey value) as the code makes the constructor return immediately, so the message and data arguments are silently ignored and the resulting object holds no errors. Use add() afterward on the same instance to attach further codes and messages.

Initializes the error.

Description

If $code is empty, the other parameters will be ignored.
When $code is not empty, $message will be used even if it is empty. The $data parameter will be used only if it is not empty.

Though the class is constructed with a single error code and message, multiple codes can be added using the add() method.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$codestring|intoptional
Error code.Default: ''
$messagestringoptional
Error message.Default: ''
$datamixedoptional
Error data. Default empty string.Default: ''

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Build a WP_Error when a post meta value fails validation

Check the numeric price meta on post 2 and construct a WP_Error describing the outcome.

$post_id = 2;
$price   = get_post_meta( $post_id, 'price', true );

if ( ! is_numeric( $price ) ) {
	$error = new WP_Error(
		'invalid_price',
		sprintf( 'The price meta value "%s" for post %d is not numeric.', $price, $post_id )
	);
} else {
	$error = new WP_Error(
		'valid_price',
		sprintf( 'Post %d has a numeric price of %s.', $post_id, $price )
	);
}

echo esc_html( $error->get_error_code() . ': ' . $error->get_error_message() );

The branch that runs depends on whatever value the price meta key holds in your sandbox.

See what happens when the error code is empty

Compare an instance constructed with an empty code against one constructed with a real code.

$empty_error     = new WP_Error();
$populated_error = new WP_Error( 'missing_price', 'No price meta was found for this post.' );

echo 'Codes on empty-code instance: [' . esc_html( implode( ', ', $empty_error->get_error_codes() ) ) . ']<br>';
echo 'Codes on populated instance: [' . esc_html( implode( ', ', $populated_error->get_error_codes() ) ) . ']<br>';
echo 'Populated message: ' . esc_html( $populated_error->get_error_message() );

new WP_Error() with no arguments produces an object with zero error codes, not an error with an empty code.

Common problems and fixes · 4

Why does my WP_Error object show no errors at all?

The constructor checks $code with empty() first and returns immediately if it is falsey, ignoring $message and $data entirely. This happens with new WP_Error() or new WP_Error( '' ), new WP_Error( 0 ), or new WP_Error( false ).

How do I add a second error code to the same WP_Error instance?

The constructor only registers the first code by calling add() once. To attach more codes and messages to that same object, call add() again on the instance instead of creating a new WP_Error.

Why is get_error_message() returning an empty string?

As long as $code is not empty, $message is stored exactly as passed, even if it is an empty string. Passing '' as the message intentionally, or by accident, is not the same as omitting the error entirely.

Why doesn't get_error_data() return the value I passed in $data?

Per the constructor's behavior, $data is only attached when it is not empty, so an empty string, empty array, or 0 passed as $data is discarded. Pass a non-empty value, or attach data afterward on the instance.

Alternatives and related functions

WP_Error::add
When you already have a WP_Error instance and need to attach an additional code and message to it.
WP_Error::add_data
When you need to attach or update data for an error code without constructing a whole new WP_Error object.
is_wp_error
When you need to check whether a value returned from a function is a WP_Error before reading its message or code.
WP_Error::get_error_message
When you already have a WP_Error object and just need to read the message for its first or a specific code.

Performance profile

How much work a call to WP_Error::__construct() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–11

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 12.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Error::__construct() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($code)6none
!empty($code)11->add()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 12 instructions, 6–11 executed per call, 1 branch. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • WP_Error::add()Adds an error or appends an additional message to an existing error.

Used by · 50

Show all 50

Source code

	public function __construct( $code = '', $message = '', $data = '' ) {		if ( empty( $code ) ) {			return;		} 		$this->add( $code, $message, $data );	}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-error.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.