wppaste
WordPress

wp_xmlrpc_server::escape( string|array $data ): string|null

Since
1.5.2
Source
wp-includes/class-wp-xmlrpc-server.php:361

Slashes a string, or every string inside an array, by delegating to wp_slash() so XML-RPC input matches what wp_insert_post() and similar functions expect. Arrays are walked recursively and modified by reference, so the call returns null in that case, while a plain string is returned directly. Objects found inside an array are skipped entirely, which surprises code that assumes every value comes back slashed.

Escapes string or array of strings for database.

Compatibility

WordPress
since 1.5.2
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

$datastring|array
Escape single string or array of strings.

Return value

string|null
Returns with string if passed, alters by-reference when array is passed.

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.

Escape a single string before passing it to an XML-RPC handler

Custom XML-RPC methods often need to slash a raw client value the same way mw_editPost and friends do before it reaches wp_insert_post().

require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php';

$server = new wp_xmlrpc_server();

$incoming_title = "O'Neill's Guide to WordPress";
$escaped_title  = $server->escape( $incoming_title );

echo esc_html( $escaped_title );

wp_xmlrpc_server is not loaded on a normal request, so it has to be required manually before it can be instantiated.

Escape an array of post meta values in place

When $data is an array, escape() rewrites each string element by reference instead of returning a new array.

require_once ABSPATH . WPINC . '/class-wp-xmlrpc-server.php';

$server = new wp_xmlrpc_server();

$fields = array(
	'price'   => get_post_meta( 2, 'price', true ),
	'tagline' => 'Bob\'s "Deluxe" Widget, in stock now',
);

$return_value = $server->escape( $fields );

echo 'Return value: ' . var_export( $return_value, true ) . "\n";
echo '<pre>' . esc_html( print_r( $fields, true ) ) . '</pre>';

$return_value is always null here; the escaped values have to be read back from $fields itself.

Common problems and fixes · 4

Why did escape() return null instead of my escaped array?

The method only returns the slashed value when $data is a scalar string. For arrays it explicitly returns null after mutating the array by reference, per the source's foreach ( $data as &$v ) loop.

Why are the objects inside my array still unescaped?

Inside the array branch the source checks elseif ( ! is_object( $v ) ) before calling wp_slash(), so any element that is an object is left completely untouched, unlike nested arrays which are escaped recursively.

Does escape() protect my XML-RPC input from SQL injection?

No. It only calls wp_slash() to add backslashes, mirroring the slashed format WordPress expects from $_POST-style data for functions like wp_insert_post(). It performs no validation or SQL-specific escaping.

Why do I get a 'Class wp_xmlrpc_server not found' error when calling escape()?

wp_xmlrpc_server lives in wp-includes/class-wp-xmlrpc-server.php, which core only loads for actual XML-RPC requests or from wp-admin, not on a typical front-end or admin page load.

Alternatives and related functions

wp_slash
When you only need to slash a single string or array without instantiating wp_xmlrpc_server, call wp_slash() directly.
wp_unslash
When you need to remove slashes from an already-escaped value before validating or comparing it.
wpdb::prepare
When you are building a raw SQL query, since escape() does not guard against SQL injection.
sanitize_text_field
When you need to sanitize user-submitted text for safe storage or display, not just add slashes.

Performance profile

How much work a call to wp_xmlrpc_server::escape() 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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–7

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

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_xmlrpc_server::escape() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_array($data)6–7none
!is_array($data)7wp_slash()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 24 instructions, 6–7 executed per call, 5 branches. 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 · 2

Used by · 50

Show all 50

Source code

	public function escape( &$data ) {		if ( ! is_array( $data ) ) {			return wp_slash( $data );		} 		foreach ( $data as &$v ) {			if ( is_array( $v ) ) {				$this->escape( $v );			} elseif ( ! is_object( $v ) ) {				$v = wp_slash( $v );			}		}		return null;	}

Changelog

Introduced in 1.5.2. One change between 6.7.7 and 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.

7.1.0
Return type changed from string|void to string|null.verified against source
1.5.2
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-xmlrpc-server.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.