• Resolved Guido

    (@guido07111975)


    Hi,

    I save my form submission in the database using wp_insert_post() but I’m not fully certain whether this function also sanitizes (single) quotes or not. Don’t want to mess up my database.

    It uses sanitize_post_field() for sanitizing and this filter uses esc_js() for escaping quotes… for use in JS.

    But this has nothing to do with escaping it for the databse itself… am I right?

    Guido

Viewing 7 replies - 1 through 7 (of 7 total)
  • When using sanitize_post_field(), if you set the fourth argument ($context) to db then it will make sue to sanitise the data for safe storage in the database. By default that parameter sanitises the data for display on the frontend, so it’s important to specify what type of sanitisation you require.

    Thread Starter Guido

    (@guido07111975)

    Hi Hugh,

    But I don’t call sanitize_post_field() directly, it’s being used by wp_insert_post().

    I stumbled upon this comment.. is this the proper way to do it?

    Or should I not worry about this too much and let wp_insert_post() do it’s thing, without extra filters?

    Note: I do sanitize all values already, before using wp_insert_post().

    Guido

    It looks like wp_insert_post() does, in fact, use sanitize_post_field() correctly to sanitise data for the database (see line 3073 in the function here: https://developer.wordpress.org/reference/functions/wp_insert_post/).

    If your content may contain slashes, then yes – that solution is a good one. All you would need to do is add wp_slash() around the content you are passing through.

    Thread Starter Guido

    (@guido07111975)

    Great:

    
    $postarr = sanitize_post($postarr, 'db');
    

    Meaning I don’t have to add extra filters, such as wp_slash(), for escaping quotes and such, because all data goes through sanitize_post / sanitize_post_field. Right?

    Guido

    That sounds correct to me yes – you should be good to go by just using wp_insert_post() 🙂

    Thread Starter Guido

    (@guido07111975)

    That simplifies things for me, thanks Hugh!

    Guido

    Happy to help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Escaping (single) quotes when using wp_insert_post’ is closed to new replies.