Title: REST API: Slashed Data in WordPress 4.4 and 4.5
Author: Ryan McCue
Published: April 6, 2016

---

 [  ](https://profiles.wordpress.org/rmccue/) [Ryan McCue](https://profiles.wordpress.org/rmccue/)
9:23 pm _on_ April 6, 2016     
Tags: [4.4 ( 46 )](https://make.wordpress.org/core/tag/4-4/),
[4.5 ( 60 )](https://make.wordpress.org/core/tag/4-5/), [rest-api ( 111 )](https://make.wordpress.org/core/tag/rest-api/)

# REST API: Slashed Data in WordPress 4.4 and 4.5

Hi everyone. The REST APIREST API The REST API is an acronym for the RESTful Application
Program Interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
It is how the front end of an application (think “phone app” or “website”) can communicate
with the data store (think “database” or “file system”) [https://developer.wordpress.org/rest-api/](https://developer.wordpress.org/rest-api/)
team recently discovered a bugbug A bug is an error or unexpected result. Performance
improvements, code optimization, and are considered enhancements, not defects. After
feature freeze, only bugs are dealt with, with regressions (adverse changes from
the previous version) being the highest priority. with parameter parsing in the 
APIAPI An API or Application Programming Interface is a software intermediary that
allows programs to interact with each other and share data in limited, clearly defined
ways. infrastructure, part of WordPress 4.4. For those of you using the API infrastructure,
you need to be aware of a bug fix we’re making with the API.

## The Problem

The REST API has several types of parameters that it mixes together. These come 
from several sources including the request body as either JSONJSON JSON, or JavaScript
Object Notation, is a minimal, readable format for structuring data. It is used 
primarily to transmit data between a server and web application, as an alternative
to XML. or URLURL A specific web address of a website or web page on the Internet,
such as a website’s URL www.wordpress.org-encoded form data (`$_POST`), query parameters(`
$_GET`), the API route, and internally-set defaults. Unfortunately, due to an oversight
on our behalf, these parameters can be inconsistently formatted.

In WordPress, the superglobal request variables (`$_POST` and `$_GET`) are “slashed”;
effectively, [turning magic quotes on](https://core.trac.wordpress.org/ticket/18322)
for everyone. This was originally built into PHPPHP The web scripting language in
which WordPress is primarily architected. WordPress requires PHP 7.4 or higher as
a feature to help guard against SQL injection, but was later removed. Due to compatibility
concerns, WP cannot change this behaviour for the superglobals. This only applies
to the PHP superglobals, not to other sources of input like a JSON body or parameters
in the URL. It additionally does not apply to form data on PUT or DELETE requests.

Internally, some low-level WordPress functions [expect slashed data](https://core.trac.wordpress.org/ticket/21767).
These functions internally call `wp_unslash()` on the data you pass in. This means
input data from the superglobals can be passed in directly, but other data needs
to be wrapped with a call to `wp_slash()`.

When the REST API gathers the data sources, it accidentally mixes slashed and unslashed
sources. This results in inconsistent behaviour of parameters based on their source.
For example, data passed as a JSON body is unslashed, whereas data passed via form
data in the body is slashed (for POST requests).

For example, the following two pieces of data are equivalent in the REST API:

    ```
    // JSON body:
    {"title": "Foo"}

    // Form-data ($_POST)
    title=Foo

    // Both result in:
    $request->get_param('title') === 'Foo';
    ```

However, if the data contains slashes itself, this will be inconsistently passed
to the callback:

    ```
    // JSON body:
    {"title": "Foo\Bar"}

    // Results in:
    $request->get_param('title') === 'Foo\Bar';

    // Form-data ($_POST) (%3D = "\")
    title=Foo%3DBar

    // Results in:
    $request->get_param('title') === 'Foo\\Bar';
    ```

This means that callbacks need to understand where parameters come from in order
to consistently handle them internally. Specifically:

 * Data passed in the query string (`$_GET`, `$request->get_query_params()`) is 
   slashed
 * Data passed in the body as form-encoded (`$_POST`, `$request->get_body_params()`)
   is slashed for `POST` requests, and unslashed for `PUT` and `DELETE` requests.
 * Data passed in the body as JSON-encoded (`$request->get_json_params()`) is unslashed.
 * Data passed in the URL (`$request->get_url_params()`) is unslashed.
 * Data passed as a default (`$request->get_default_params()`) is unslashed.

In addition, parameters set internally via `$request->set_param()` are unslashed.
Unit and integration tests for API endpoints typically use these directly, so the
majority of tested code (such as the WP REST API pluginPlugin A plugin is a piece
of software containing a group of functions that can be added to a WordPress website.
They can extend functionality or add new features to your WordPress websites. WordPress
plugins are written in the PHP programming language and integrate seamlessly with
WordPress. These can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
or can be cost-based plugin from a third-party.) assumes parameters are unslashed.

See the related TracTrac An open source project by Edgewall Software that serves
as a bug tracker and project management tool for WordPress. Ticketticket Created
for both bug reports and feature development on the bug tracker. [#36419](https://core.trac.wordpress.org/ticket/36419)
for more information.

## The Solution for WordPress 4.4 and 4.5

We are regarding inconsistently-slashed data as a major bug, and **are changing 
the API infrastructure to ensure unslashed data**. This will ensure that data is
consistent regardless of the source. Callbacks will now receive unslashed data only,
and can rely on this regardless of the original data source or request method.

If you are using functions that expect slashed data in your callback, you will need
to slash your data before passing into these functions. Commonly used functions 
that expect slashed data are `wp_insert_post`, `wp_update_post`, `update_post_meta`,`
wp_insert_term`, `wp_insert_user`, along with others. Before passing data into these
functions, you must call `wp_slash()` on your data.

The fix for this issue, will be included in the WordPress 4.5 release candidates
and final release. Due to the severityseverity The seriousness of the ticket in 
the eyes of the reporter. Generally, severity is a judgment of how bad a bug is,
while **priority** is its relationship to other bugs. of the bug, we are also backporting
the fix to the next minor WordPress 4.4 update. This also ensures you can update
your plugins can act consistently across all versions of the REST API.

We understand that this may inadvertently break some plugins that are expecting 
slashed data. Right now, it’s not possible to consistently ensure that callbacks
receive slashed data, so it is likely that these plugins will already break in some
conditions.

**tl;dr:** if you’re using `wp_insert_*` or `*_post_meta` in your REST API callback,
you need to ensure you are calling `wp_slash()` on data you are passing in, regardless
of source.

We apologize for this bug existing in the first place. Slashed data is a problem
that has plagued WordPress for a long time, and we’re not immune to getting caught
by the issue ourselves.

[#4-4](https://make.wordpress.org/core/tag/4-4/), [#4-5](https://make.wordpress.org/core/tag/4-5/),
[#rest-api](https://make.wordpress.org/core/tag/rest-api/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?share=linkedin)

 1.  ![](https://secure.gravatar.com/avatar/19064ed4f839086c8fd557e5d00c62cdb52cdba868e6bf100a64d8a4f0974aed?
     s=32&d=mm&r=g)
 2.   [Mike Schinkel](https://profiles.wordpress.org/mikeschinkel/)  7:16 am _on_ April
     7, 2016
 3.  > We apologize for this bugbug A bug is an error or unexpected result. Performance
     > improvements, code optimization, and are considered enhancements, not defects.
     > After feature freeze, only bugs are dealt with, with regressions (adverse changes
     > from the previous version) being the highest priority. existing in the first
     > place. Slashed data is a problem that has plagued WordPress for a long time,
     > and we’re not immune to getting caught by the issue ourselves.
 4.  Given that this issue created such a major bug, wouldn’t it be a good time to 
     take backward compatible action to slowing start eliminating this as an issue?
 5.   * ![](https://secure.gravatar.com/avatar/19064ed4f839086c8fd557e5d00c62cdb52cdba868e6bf100a64d8a4f0974aed?
        s=32&d=mm&r=g)
      *  [Mike Schinkel](https://profiles.wordpress.org/mikeschinkel/)  7:35 am _on_
        April 7, 2016
      * In case of interest in addressing this moving forward:
         - [https://core.trac.wordpress.org/ticket/36438](https://core.trac.wordpress.org/ticket/36438)
      * ![](https://secure.gravatar.com/avatar/c21ff90e030a07e3a20f48a0496530828cf86ef69a464d326351423eb2570ef0?
        s=32&d=mm&r=g)
      *  [Ryan McCue](https://profiles.wordpress.org/rmccue/)  3:40 am _on_ April 8,
        2016
      * As it happens, this is part of my Secret Master Plan for Unslashing, and part
        of why I wanted to ensure we fixed this right now.
      * My Secret Master Plan:
         - Take the generic parts of WP_REST_Request and make a new WP_HTTP_Request
           object
         - Ensure the Request object has unslashed data
         - Pass the Request around liberally to replace the superglobals (discussed
           somewhat in [#36292](https://core.trac.wordpress.org/ticket/36292))
         - Finally introduce internals that take unslashed data, and turn the existing
           functions into wrapper functions.
 6.  ![](https://secure.gravatar.com/avatar/fcbc6869ca65225c6bfbd46b7f1720efc6bd0b966f1a3ea9401663d9e9870459?
     s=32&d=mm&r=g)
 7.   [NateWr](https://profiles.wordpress.org/natewr/)  7:54 am _on_ April 7, 2016
 8.  Is this change in the latest WP 4.5 RCrelease candidate One of the final stages
     in the version release cycle, this version signals the potential to be a final
     release to the public. Also see [alpha (beta)](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?output_format=md#alpha-beta).
     or do we need to get the latest commits to test our apps?
 9.   * ![](https://secure.gravatar.com/avatar/e1a850bca164e3d29c1d8ae8117fa4b341ed150ea4d8ab29a285726c944c453b?
        s=32&d=mm&r=g)
      *  [Marius (Clorith)](https://profiles.wordpress.org/clorith/)  12:56 pm _on_
        April 7, 2016
      * It is not available in RCrelease candidate One of the final stages in the version
        release cycle, this version signals the potential to be a final release to 
        the public. Also see [alpha (beta)](https://make.wordpress.org/core/2016/04/06/rest-api-slashed-data-in-wordpress-4-4-and-4-5/?output_format=md#alpha-beta).-
        1, that’s out at this time, as it was pushed in last evening/earlier today (
        depending on your time zones), the goal is to get RC-2 out some time this week,
        and it will include this change.
      * If you are running trunktrunk A directory in Subversion containing the latest
        development code in preparation for the next major release cycle. If you are
        running "trunk", then you are on the latest revision. you’ll have access to
        it at this time, alternatively you can manually apply the patchpatch A special
        text file that describes changes to code, by identifying the files and lines
        which are added, removed, and altered. It may also be referred to as a **diff**.
        A patch can be _applied_ to a codebase for testing. to your own install if 
        you wish to start testing it right away (the patch from [#36419](https://core.trac.wordpress.org/ticket/36419))
         - ![](https://secure.gravatar.com/avatar/fcbc6869ca65225c6bfbd46b7f1720efc6bd0b966f1a3ea9401663d9e9870459?
           s=32&d=mm&r=g)
         -  [NateWr](https://profiles.wordpress.org/natewr/)  1:44 pm _on_ April 7,
           2016
         - Thanks Marius!

Comments are closed.

# Post navigation

[← Road to 4.5: All Hands on Deck](https://make.wordpress.org/core/2016/04/04/road-to-4-5-all-hands-on-deck/)

[Week in Core, Mar 29 – Apr 5 2016 →](https://make.wordpress.org/core/2016/04/07/week-in-core-mar-29-mar-5-2016/)