wp_xmlrpc_server::escape( string|array $data ): string|void
- Since
- 1.5.2
- Source
wp-includes/class-wp-xmlrpc-server.php:346
Slashes a string, or recursively slashes every string inside an array, using wp_slash() to prepare XML-RPC input for WordPress's database functions. It returns the slashed value for a plain string but modifies arrays in place by reference and returns nothing, silently skipping any objects it finds along the way. Because it only adds slashes rather than performing real SQL escaping, pair it with wpdb::prepare() or esc_sql() before building raw queries from the result.
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|void- Returns with string is 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 value from an XML-RPC request
Instantiate wp_xmlrpc_server directly to see how escape() slashes quotes in a plain string.
$server = new wp_xmlrpc_server();
$raw_title = 'A "quoted" post title with a don\'t-miss offer';
$escaped_title = $server->escape( $raw_title );
echo esc_html( $escaped_title );The slashed value is meant for WordPress's database functions, not for direct display, wp_slash() adds backslashes that a template shouldn't echo unmodified.
Slash an array of XML-RPC parameters in place
Pass an array by reference so escape() walks every string recursively, including a nested array, while leaving objects untouched.
$server = new wp_xmlrpc_server();
$post_data = array(
'post_title' => 'Weekend Sale: 50% Off "Everything"',
'custom_field' => array(
'price' => 'Now only $9.99, was $19.99',
),
);
$server->escape( $post_data );
print_r( $post_data );escape() returns nothing when given an array, the modification happens on $post_data itself because the parameter is passed by reference.
Common problems and fixes · 4
- Why does wp_xmlrpc_server::escape() return null when I pass it an array?
- Why do my strings show backslashes before quotes after calling escape()?
- Why doesn't escape() slash an object nested inside my data array?
- Can I call wp_xmlrpc_server::escape() statically?
Why does wp_xmlrpc_server::escape() return null when I pass it an array?
Why do my strings show backslashes before quotes after calling escape()?
Why doesn't escape() slash an object nested inside my data array?
elseif ( ! is_object( $v ) ), so any array element that is an object is skipped entirely and left unmodified.Can I call wp_xmlrpc_server::escape() statically?
Alternatives and related functions
wp_slash- When you just need to slash a single string or a plain array without the class instance this method requires.
wpdb::prepare- When you're building a raw SQL query and need placeholders and quoting handled together, rather than just adding slashes.
esc_sql- When you need to escape a value for direct inclusion in a SQL query outside of wpdb::prepare().
stripslashes_deep- When you need to reverse recursive slashing on an array that was previously escaped this way.
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
- Scaling
- Scales with input
- Instructions
- 6–7
- Plugin surface
- None
- Called by
- 50
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 24.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($data) | 6–7 | none |
!is_array($data) | 7 | wp_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
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_xmlrpc_server::escape()Escapes string or array of strings for database.
Used by · 50
- wp_xmlrpc_server::blogger_deletePost()Deletes a post.
- wp_xmlrpc_server::blogger_editPost()Edits a post.
- wp_xmlrpc_server::blogger_getPost()Retrieves a post.
- wp_xmlrpc_server::blogger_getRecentPosts()Retrieves the list of recent posts.
- wp_xmlrpc_server::blogger_getUserInfo()Retrieves user's data.
- wp_xmlrpc_server::blogger_getUsersBlogs()Retrieves blogs that user owns.
- wp_xmlrpc_server::blogger_newPost()Creates a new post.
- wp_xmlrpc_server::escape()Escapes string or array of strings for database.
- wp_xmlrpc_server::mt_getCategoryList()Retrieves the list of all categories on a blog.
- wp_xmlrpc_server::mt_getPostCategories()Retrieves post categories.
- wp_xmlrpc_server::mt_getRecentPostTitles()Retrieves the post titles of recent posts.
- wp_xmlrpc_server::mt_publishPost()Sets a post's publish status to 'publish'.
Show all 50
- wp_xmlrpc_server::mt_setPostCategories()Sets categories for a post.
- wp_xmlrpc_server::mw_editPost()Edits a post.
- wp_xmlrpc_server::mw_getCategories()Retrieves the list of categories on a given blog.
- wp_xmlrpc_server::mw_getPost()Retrieves a post.
- wp_xmlrpc_server::mw_getRecentPosts()Retrieves list of recent posts.
- wp_xmlrpc_server::mw_newMediaObject()Uploads a file, following your settings.
- wp_xmlrpc_server::mw_newPost()Creates a new post.
- wp_xmlrpc_server::pingback_extensions_getPingbacks()Retrieves an array of URLs that pingbacked the given URL.
- wp_xmlrpc_server::pingback_ping()Retrieves a pingback and registers it.
- wp_xmlrpc_server::wp_deleteCategory()Deletes a category.
- wp_xmlrpc_server::wp_deleteComment()Deletes a comment.
- wp_xmlrpc_server::wp_deletePage()Deletes a page.
- wp_xmlrpc_server::wp_deletePost()Deletes a post for any registered post type.
- wp_xmlrpc_server::wp_deleteTerm()Deletes a term.
- wp_xmlrpc_server::wp_editComment()Edits a comment.
- wp_xmlrpc_server::wp_editPage()Edits a page.
- wp_xmlrpc_server::wp_editPost()Edits a post for any registered post type.
- wp_xmlrpc_server::wp_editProfile()Edits user's profile.
- wp_xmlrpc_server::wp_editTerm()Edits a term.
- wp_xmlrpc_server::wp_getAuthors()Retrieves authors list.
- wp_xmlrpc_server::wp_getComment()Retrieves a comment.
- wp_xmlrpc_server::wp_getCommentCount()Retrieves comment counts.
- wp_xmlrpc_server::wp_getCommentStatusList()Retrieves all of the comment status.
- wp_xmlrpc_server::wp_getComments()Retrieves comments.
- wp_xmlrpc_server::wp_getMediaItem()Retrieves a media item by ID.
- wp_xmlrpc_server::wp_getMediaLibrary()Retrieves a collection of media library items (or attachments).
- wp_xmlrpc_server::wp_getOptions()Retrieves blog options.
- wp_xmlrpc_server::wp_getPage()Retrieves a page.
- wp_xmlrpc_server::wp_getPageList()Retrieves page list.
- wp_xmlrpc_server::wp_getPageStatusList()Retrieves page statuses.
- wp_xmlrpc_server::wp_getPageTemplates()Retrieves page templates.
- wp_xmlrpc_server::wp_getPages()Retrieves Pages.
- wp_xmlrpc_server::wp_getPost()Retrieves a post.
- wp_xmlrpc_server::wp_getPostFormats()Retrieves a list of post formats used by the site.
- wp_xmlrpc_server::wp_getPostStatusList()Retrieves post statuses.
- wp_xmlrpc_server::wp_getPostType()Retrieves a post type.
- wp_xmlrpc_server::wp_getPostTypes()Retrieves post types.
- wp_xmlrpc_server::wp_getPosts()Retrieves posts.
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 ); } } }Changelog
Introduced in 1.5.2. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
string|void to string|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.