WP_Ajax_Response::add( string|array $args = '' ): string
- Since
- 2.1.0
- Source
wp-includes/class-wp-ajax-response.php:67
Description
With $args defaults, extra data output would be:
<response action='{$action}_$id'>
<$what id='$id' position='$position'>
<response_data><![CDATA[$data]]></response_data>
</$what>
</response>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
$argsstring|arrayoptional- An array or string of XML response arguments.Default:
''$whatstringdefault: 'object' (<object>)XML-RPC response type. Used as a child element of<response>.$actionstring|falsedefault: falseValue to use for theactionattribute in<response>. Will be appended with_$idon output. If false,$actionwill default to the value of$_POST['action'].$idint|WP_Errordefault: 0The response ID, used as the response typeidattribute. Also accepts aWP_Errorobject if the ID does not exist.$old_idint|falsedefault: falseThe previous response ID. Used as the value for the response typeold_idattribute. False hides the attribute.$positionstringdefault: 1 (bottom)Value of the response typepositionattribute. Accepts 1 (bottom), -1 (top), HTML ID (after), or -HTML ID (before).$datastring|WP_Errordefault: emptyThe response content/message. Also accepts a WP_Error object if the ID does not exist.$supplementalarraydefault: empty arrayAn array of extra strings that will be output within a<supplemental>element as CDATA.
Return value
string- XML response.
Performance profile
How much work a call to WP_Ajax_Response::add() 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
- Light
- Scaling
- Scales with input
- Instructions
- 67–83
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 154.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below WP_Ajax_Response::add()
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_Ajax_Response::add() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_wp_error() | 67–80 | wp_parse_args(), is_wp_error(), is_wp_error() |
is_wp_error() | 69–83 | wp_parse_args(), is_wp_error(), is_wp_error(), ->get_error_codes() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 154 | 67–83 | 15 | |
| 8.5 | 154 | 67–83 | 15 | |
| 8.4 | 154 | 67–83 | 15 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 158 | 71–87 | 15 | |
| 8.2 | 158 | 71–87 | 15 | |
| 8.1 | 158 | 71–87 | 15 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 161 | 71–88 | 15 |
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 · 2
- wp_parse_args()Merges user defined arguments into defaults array.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Used by · 1
- WP_Ajax_Response::__construct()Constructor - Passes args to WP_Ajax_Response::add().
Source code
public function add( $args = '' ) { $defaults = array( 'what' => 'object', 'action' => false, 'id' => '0', 'old_id' => false, 'position' => 1, 'data' => '', 'supplemental' => array(), ); $parsed_args = wp_parse_args( $args, $defaults ); $position = preg_replace( '/[^a-z0-9:_-]/i', '', $parsed_args['position'] ); $id = $parsed_args['id']; $what = $parsed_args['what']; $action = $parsed_args['action']; $old_id = $parsed_args['old_id']; $data = $parsed_args['data']; if ( is_wp_error( $id ) ) { $data = $id; $id = 0; } $response = ''; if ( is_wp_error( $data ) ) { foreach ( (array) $data->get_error_codes() as $code ) { $response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message( $code ) . ']]></wp_error>'; $error_data = $data->get_error_data( $code ); if ( ! $error_data ) { continue; } $class = ''; if ( is_object( $error_data ) ) { $class = ' class="' . get_class( $error_data ) . '"'; $error_data = get_object_vars( $error_data ); } $response .= "<wp_error_data code='$code'$class>"; if ( is_scalar( $error_data ) ) { $response .= "<![CDATA[$error_data]]>"; } elseif ( is_array( $error_data ) ) { foreach ( $error_data as $k => $v ) { $response .= "<$k><![CDATA[$v]]></$k>"; } } $response .= '</wp_error_data>'; } } else { $response = "<response_data><![CDATA[$data]]></response_data>"; } $s = ''; if ( is_array( $parsed_args['supplemental'] ) ) { foreach ( $parsed_args['supplemental'] as $k => $v ) { $s .= "<$k><![CDATA[$v]]></$k>"; } $s = "<supplemental>$s</supplemental>"; } if ( false === $action ) { $action = $_POST['action']; } $x = ''; $x .= "<response action='{$action}_$id'>"; // The action attribute in the xml output is formatted like a nonce action. $x .= "<$what id='$id' " . ( false === $old_id ? '' : "old_id='$old_id' " ) . "position='$position'>"; $x .= $response; $x .= $s; $x .= "</$what>"; $x .= '</response>'; $this->responses[] = $x; return $x; }Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/class-wp-ajax-response.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.