Forums

How to clean post data before inserting in db (6 posts)

  1. freeriders
    Member
    Posted 2 years ago #

    Greetins

    I wish to insert posts coming from a CSV file

    I have no problem to extract from the CSB

    to insert datas in the DB I use

    wp_insert_post( $my_post )

    where $my_post is an array of the data I got from my CSV

    I have some problme to "clean" my data before inserting, specially if they includes special characters

    so far I use

    $my_post['post_content']= funky_javascript_fix( $my_post['post_content'] );
    $my_post['post_content']= wp_specialchars( stripslashes( $my_post['post_content'] ));
    $my_post['post_content']= apply_filters( 'default_content', $my_post['post_content']);

    but this does not solve my problme with datas containing for example

    "&é'(-è_çà)"

    this send me back an empty file when the post in inserted

    I believe I need to sanitize those data before inserting, but honestly I'm quite lost on which functions I need to use to achieve that

    so far I would like to sanitize datas for

    -- post title
    -- post content
    -- categories
    -- tags
    -- metas

    Thank you VERY much in advance

    Steffy

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    The function wp_insert_post does call the function sanitize_post.

  3. freeriders
    Member
    Posted 2 years ago #

    thank you MichaelH

    1° if I understand well, there is no need for me to use
    $my_post['post_content']= funky_javascript_fix( $my_post['post_content'] );
    $my_post['post_content']= wp_specialchars( stripslashes( $my_post['post_content'] ));
    $my_post['post_content']= apply_filters( 'default_content', $my_post['post_content']);

    right? they are already called in function sanitize_post

    2° how comes that if in my value

    $my_post['post_content']

    there is characters like :

    $my_post['post_content']="&é'(-è_çà)";

    when I do wp_insert_post( $my_post )

    then the field post_content is empty

    if I use no problem character like

    $my_post['post_content']="toto is here";

    then it works perfectly, the field post_content is correctly filled

    any idea on what I should do that the caractere transformed in something that would be well inserted?

    thank you very much in advance

    Steffy

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    That I can't answer so maybe someone else can add their comments.

  5. freeriders
    Member
    Posted 2 years ago #

    ok MichealH, at least thank you very much for your time, hopefully someone will be able to help to clean the datas

    cheers

    Steffy

  6. shafizaman
    Member
    Posted 1 year ago #

    A simple php function can solve this problem utf8_encode();

    $my_post['post_content']= utf8_encode( $my_post['post_content'] );

Topic Closed

This topic has been closed to new replies.

About this Topic