I am sending stringified JSON object to a wordpress action
console.log( JSON.stringify(alloptions) );
$.ajax({
type: "post",
dataType: "json",
url: ajaxurl,
processData: false,
data: {
'action': 'create_preset',
'preset': JSON.stringify(alloptions)
},
success: function( response ) {
console.log( response );
}
});
the console log of the stringified object before sent via ajax is this
so the string is correctly processed ,
but on the other side it comes out with slashes
function _create_preset(){
if(!is_admin() && !isset($_POST['preset'])) return;
print_r($_POST['preset']);
}
add_action("wp_ajax_create_preset", "_create_preset");
gives
{\"get_presets\":\"eedewd\",\"site_width\":\"1400px\",\"layout_type\":...
I know I can use
stripslashes( $_POST['preset'] )
to clean it up but that is what I am trying to avoid. I need the JSON string to be sent to the action as it is before the ajax, without slashes.
any help is appreciated!
and magic quotes is not on
*UPDATE AND SOLUTION
Jesse nailed it and WP was causing the trouble.
Since the wp_unslash()
is on its way to fix this in 5.0
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/172
I added this to my code
global $wp_version;
$new_preset_options = $_POST['preset'];
if ( version_compare( $wp_version, '5.0', '<' ) ) {
$new_preset_content = wp_unslash( $new_preset_options );
}else{
$new_preset_content = $new_preset_options ;
}