when I create an input
<input type="text" name="456" value="123">
and pass that data to another php site, WordPress automatically adds backslashes like:
"my text"
But I need the text in that following way:
"my text".
And that is why I use the function before i further process the data:
stripslashes()
Is that an unsafe solution against hacking? It is crucial to know since I send passwords over an API.
For future readers, the previous answers are straight-up wrong. As per Ian‘s comment, WordPress enforces the equivalent of magic quotes internally to ensure consistent input, backwards compatibility, and to protect newbies from compromising WP installs. It’s a hangover from the bad old days⢠when it was quite common for people to insert data directly into the database without any sanitising/validation, then write hacky workarounds only for the issues they witnessed rather than doing it properly.
It is safe to use
stripslashes()
andstripslashes_deep()
on your data, as long as you’re correctly passing any unescaped data through$wpdb->insert()
,$wpdb->prepare()
, etc. when saving it to the database. Never write unescaped data to the superglobals like$_POST
which are expected to remain escaped, use a copy like$my_post = stripslashes_deep($_POST);
. It is mostly expected that you will do unescaping yourself, which is why it used to be common to see a lot of backslashes littering WP content.This is completely separate to escaping for html output which should always be done on untrusted user input.
It should be safe.
According to http://fearlessflyer.com/getting-rid-of-unwanted-backslashes-in-wordpress-form-input/
See: http://php.net/manual/en/security.magicquotes.disabling.php
Quick answer
Add this to your PHP:
I would guess that this is not WordPress but your installation of PHP having magic quotes enabled. You should modify your php.ini to disable magic quotes. Magic Quotes are EVIL!
http://php.net/manual/en/security.magicquotes.disabling.php
Magic Quotes are DEPRECATED as of PHP 5.3 and they are completely removed from PHP 5.4.
UPDATE
I’ve never heard of stripslashes being compromised. It’s a string on the way in, it’ll be a string with less slashes on the way out. So, if you can’t turn off magic quotes, then I would use stripslashes to remove the quotes from the $_POST, etc. If you’re worried about other plugins being affected by your removal of slashes (though I would be surprised), then you could do something like this:
Then, you can access your vars as:
Http::instance()->get('myvar');
. Once you do this, then you just need to properly sanitize the input before you do SQL operations, mail() headers, etc., which is where injections would likely occur. For the DB, you should use something like mysql_real_escape_string (legacy) or Pdo::quote() (currently)…or better yet, only use prepared statements. These methods take the DB context and properly sanitize the data for THAT DB, which is much safer than addslashes for the same thing.Personally, I would rather disable magic quotes. But if you can’t do that, this approach should work.