0

We are trying to retrieve HTML code stored as a LONGTEXT in an UTF8 MySQL table then store this as a post meta value in WordPress.

If the HTML contains a pound (£) symbol for example then the ‘update_post_meta’ function does not store data correctly, this occurs for only some characters / for example is fine.

The issue does not exist when saving these characters through the wp_update_post function.

The code below is the page that we are using for processing the data the function in question is updatecdata()

function updatecdata()
{
            $SERVER = 'REDACTED';
            $USERNAME = 'REDACTED';
            $PASSWORD =  'REDACTED';

            $DB = 'DATABASE';
            $link = mysqli_connect($SERVER,$USERNAME,$PASSWORD,$DB);

            mysqli_select_db($link, $DB);

            $sql="SELECT * FROM table;";

            $result = mysqli_query($link, $sql);

            if(! $result ) {
  die('Could not get data: ' . mysqli_error($result));
            }

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

                            $post_id = $row["post_id"];
                            $post_title = $row["post_title"];
                            $post_content = $row["post_content"];
                            $post_content_reg = row["post_content_reg"];


                                             #####update data####
                            $post = array(
                            'ID' => $post_id,                                                       
                            'post_title' => $post_title,
                            'post_content' => $post_content
                            );

                            //UPDATE POST
                            wp_update_post($post);

                            //UPDATE POST_META
                            update_post_meta($post_id, 'contentreg', $content_reg);

            }              

            mysqli_close($link);

}
  • This seems to be an encoding issue. Can you show us what is stored instead of a pound sign? – Lajos Arpad Jan 04 '19 at 11:57
  • Hi the pound symbol is stored as a pound symbol in the source table eg.

    Overall Fee in (£)

    – Aspurian Jan 04 '19 at 12:05
  • if we alter this line to $post_content_reg = row["post_content_reg"]; to $post_content_reg = '

    Overall Fee in (£)

    '; then everything works as expected it seems when we try and retrieve from the table the problem creeps in
    – Aspurian Jan 04 '19 at 12:06
  • Make sure that the HTTP POST request has charset UTF8. – GMB Jan 04 '19 at 12:30
  • Hi GMB can confirm we are setting header('Content-Type: text/html; charset=utf-8'); in the page – Aspurian Jan 04 '19 at 12:40

0 Answers0