Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#41579 closed defect (bug) (invalid)

Post_content / meta value unable to store slashed content

Reported by: compute's profile Compute Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords:
Focuses: Cc:


Hi there,

In my plugin I'm storing data from another service. This service returns regex data on how some fields are getting validated.

When saving these values to either post_content or a post meta the values are getting slashed. I could save these are options but due to it being specific to a post_type this does not make sense.

To test:

$regex = '(\b16-\d{3}\b)';
$post_id = wp_insert_post( [
	'post_title' => 'Test regex',
	'post_content' => $regex,
	'post_type' => 'post',
] );
echo '<br>';
var_dump( get_post($post_id )->post_content );


string(14) "(\b16-\d{3}\b)" 
string(11) "(b16-d{3}b)"

It seems like there is no way of skipping the current API from slashing the values with wp_unslash.

Change History (3)

#1 @ocean90
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version trunk deleted

Hello @Compute,

by default wp_insert_post() doesn't add slashes to the post data because it expects the data already to be slashed. Before the data gets stored into the database wp_unslash() is called. That's why the slashes from the regex are removed.
To solve this you have to call wp_slash() on your $regex first.

This ticket was mentioned in Slack in #core by compute. View the logs.

7 years ago

#3 @johnbillion
7 years ago

Follow-up: #41593

Note: See TracTickets for help on using tickets.