Jump to content

wordpress, wp_insert_post and utf8_decode help


herghost
Go to solution Solved by herghost,

Recommended Posts

Hi all

 

I am having problems with french characters when using a custom function to upload posts from a database into wordpress

 

Firstly here is my code:

 

//This gets the values that is sent to the class to create post

<?php

require_once('wp-content/plugins/PostAdder/index.php');




try {
  		$host = 'localhost';
 		//$dbname = 'wordpress';
		$dbname = 'HotelWifi';
 		$user = 'root';
 		$pass = '';
 
  		$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING, PDO::MYSQL_ATTR_INIT_COMMAND =>  'SET NAMES utf8') );

	}
catch(PDOException $e) 
	{
    	echo $e->getMessage();
	}


		$STH = $DBH->query('select * from activepropertylist');
		$STH->setFetchMode(PDO::FETCH_ASSOC);
		while($row = $STH->fetch()) 
		{
						$hotelname = ucwords(utf8_decode($row['Name']));
						$city = $row['City'];
						
						$STH1 = $DBH->query('select * from wp_terms WHERE name = "' . $city . '"');
						$STH1->setFetchMode(PDO::FETCH_ASSOC);
						while($row1 = $STH1->fetch())
							{
								$cat = $row1['term_id'];
							}
						$tcat = array($cat);
						
						post_a_newhotel($hotelname, $tcat)	;
		}
						
		
		
		

?>

// this is the function that fills the class and calls the wp_insert_post function

<?php
/*
Plugin Name: POstHotel
Plugin URI: http://www.hotelwifiscore.com
Description: Creates post from supplied data
Version: 1
Author: davegrix
Author URI: http://www.hotelwifiscore.com
*/

require_once('wp-load.php');

class wm_mypost {
var $post_title;
var $post_category;
}

function post_a_newhotel($title, $post_cat) {


if (class_exists('wm_mypost')) 
{
    $myclass = new wm_mypost();
}
else
	{
		class wm_mypost 
		{
			var $post_title;
			var $post_category;
		}

}


// initialize post object
$wm_mypost = new wm_mypost();

// fill object
$wm_mypost->post_title = utf8_decode($title);
$wm_mypost->post_category = $post_cat;
$wm_mypost->post_status = 'publish';


array_walk_recursive($wm_mypost, function (&$value) 
{
   $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
}
 );

wp_insert_post($wm_mypost);

}

?>

The string I am having problems with is 'Best Western Premier Hôtel L'Aristocrate'

 

The result stored using the above is: Best Western Premier H?tel L'Aristocrate

 

database is set to utf8_general_ci

 

Can anyone shine any light on what to do?

Edited by herghost
Link to comment
Share on other sites

Well the ' appears to be url-wise  '  to my opera browser.  And you used post!

 

HTML 1.1?  Does the html have a language?  Are you naturalized?  My browser does not list UTF8 as international option.  There is a Unified Canadian Aboriginal Syllabic.  Go to site in France and see if there are clues.

Link to comment
Share on other sites

Hello herghost! Character encoding problems can be a pain hu!? ;)

 

So, you need to find where the problem is coming from in all the chain of "events". 

 

Here's what I understood from your post and code. Please feel free to correct me if I misunderstood something:

 

1. You press a button or do another action? (here I'll need more details)

2. Some data about hotels is selected in the database

3. This data is then inserted in another table

4. The data is not well displayed (?)

 

at #1, what is the action that starts all the process? Where does it come from?

 

at #4 : Where and how are you looking at the data? From a web page or did you logged in using a tool?

 

at #3 : Before inserting the data into the database, did you try to print it out to see what it looked like? I would try something like:

echo "accents: éàô?'";
echo $yourStringFromSelect;

The variable "yourStringFromSelect" should contains the data you selected from the table. You could also try that in a standalone PHP script just to see the result. 

 

This way, you'll see if the problems comes from the "select", from the "insert" or if it's in the configuration of the table. Just be sure that your files are saved in the same encoding as the string you are trying to print.

Edited by mogosselin
Link to comment
Share on other sites

Hi,

 

The page is just called from the browser directly, it then gets the details from one table (echo's here correctly) gets passed to the function to insert new post (echo s here correctly as well!) Then gets sent to the database in the WordPress insert_post function, this is where I am sure something dodgy is happening :-)

 

The tables all have the same encoding

Link to comment
Share on other sites

  • Solution

Got it!

<?php
/*
Plugin Name: POstHotel
Plugin URI: http://www.hotelwifiscore.com
Description: Creates post from supplied data
Version: 1
Author: davegrix
Author URI: http://www.hotelwifiscore.com
*/

require_once('wp-load.php');

class wm_mypost {
var $post_title;
var $post_category;
}

function post_a_newhotel($title, $post_cat) {


if (class_exists('wm_mypost')) 
{
    $myclass = new wm_mypost();
}
else
	{
		class wm_mypost 
		{
			var $post_title;
			var $post_category;
			
		}

}

echo $title;

// initialize post object
$wm_mypost = new wm_mypost();

// fill object
$wm_mypost->post_title = $title;
$wm_mypost->post_category = $post_cat;
$wm_mypost->post_status = 'publish';


$result = utf8_encode_deep($wm_mypost);



array_walk_recursive($wm_mypost, function (&$value) 
{
   $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
}
 );

print_r($result);
wp_insert_post($wm_mypost);

}


function utf8_encode_deep(&$input) {
	if (is_string($input)) {
		$input = utf8_encode($input);
	} else if (is_array($input)) {
		foreach ($input as &$value) {
			utf8_encode_deep($value);
		}
		
		unset($value);
	} else if (is_object($input)) {
		$vars = array_keys(get_object_vars($input));
		
		foreach ($vars as $var) {
			utf8_encode_deep($input->$var);
		}
	}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.