$wpdb->update or $wpdb->insert results in slashes being added in front of quotes $wpdb->update or $wpdb->insert results in slashes being added in front of quotes wordpress wordpress

$wpdb->update or $wpdb->insert results in slashes being added in front of quotes


After spending the day on this, the answer is as follows:

Wordpress escapes at the $_POST declaration, not at the actual insert, which is bizarre.

$id = stripslashes_deep($_POST['id']); //added stripslashes_deep which removes WP escaping.$title = stripslashes_deep($_POST['title']);$message = stripslashes_deep($_POST['message']);$wpdb->update('table_name', array('id'=>$id, 'title'=>$title, 'message'=>$message), array('id'=>$id));

Doing this will mean that WP will not add slashes before any quotes.


a little more info--WordPress decided to make people think they were going crazy by adding 'magic quotes' even if you've got it turned off starting in version 3.0. Any access to $_REQUEST, $_GET, $_POST, $_COOKIE, or $_SERVER will be affected. See wp-includes/load.php.

 /* Add magic quotes to $_GET, $_POST, $_COOKIE, and $_SERVER. * @since 3.0.0 */function wp_magic_quotes() {        // If already slashed, strip.        if ( get_magic_quotes_gpc() ) {                $_GET    = stripslashes_deep( $_GET    );                $_POST   = stripslashes_deep( $_POST   );                $_COOKIE = stripslashes_deep( $_COOKIE );        }        // Escape with wpdb.        $_GET    = add_magic_quotes( $_GET    );        $_POST   = add_magic_quotes( $_POST   );        $_COOKIE = add_magic_quotes( $_COOKIE );        $_SERVER = add_magic_quotes( $_SERVER );        // Force REQUEST to be GET + POST.        $_REQUEST = array_merge( $_GET, $_POST );}


WordPress ignores the built in php magic quotes setting and the value of get_magic_quotes_gpc() and will always add magic quotes (even after the feature is removed from PHP in 5.4).

you can use this instead

//replace $_POST with $POST$POST      = array_map( 'stripslashes_deep', $_POST);$wpdb->insert(         'wp_mytable',         array(             'field_name'        => $POST['field_name'],             'type'              => $POST['type'],            'values'            => serialize($POST['values']),            'unanswered_link'   => $POST['unanswered_link'],         ),         array(             '%s','%s','%s','%s'        )     );

WordPress does this because too much core and plugin code has come to rely on the quotes being there, so disabling quotes on the super globals (as is done in both the "Basic Example" and "Good Coding Practice" examples above) is likely to cause security holes.

http://codex.wordpress.org/Function_Reference/stripslashes_deep