Q: wp_insert_post truncates post content on quotes... is there a proper way to escape these things?
WPMU DEV Community
Q: wp_insert_post truncates post content on quotes... is there a proper way to escape these things?
Elite
572 pointsLike some sort of WPMU DEV God"I'm helpful
David
Member
—
5th June 2010 (1 year ago)
So, I've made my first attempt to import from the old CMS. When I bring in content, I've noticed that wp_insert_post seems to truncate the post_content array element on quotes (and possibly other things).
If I want to make sure content is imported as specified, what do I have to do to the content first? Do I have to run some kind of magic quotes thing, or some level of escape code?
But $article is getting truncated. I'm assuming I should have something like:
`$article = clean_me_up_somehow($article); before the insert, but I don't know what that is, and I can't check 100,000 pages individually once I flip the switch and do the import.
So, I've made my first attempt to import from the old CMS. When I bring in content, I've noticed that wp_insert_post seems to truncate the post_content array element on quotes (and possibly other things).
If I want to make sure content is imported as specified, what do I have to do to the content first? Do I have to run some kind of magic quotes thing, or some level of escape code?
But $article is getting truncated. I'm assuming I should have something like:
`$article = clean_me_up_somehow($article); before the insert, but I don't know what that is, and I can't check 100,000 pages individually once I flip the switch and do the import.
Yeah, from all the docs, you'd think it expected unfiltered content. But from a bunch of random forum postings (not here, but around the Googlescape), others had similar problems.
I will check the importer code, tho... from textpattern.php:
I just did something similar with one of my site, I imported everything, but I needed to create the categories also when I posted the info. I used the following code to do that. I looked for hours on how to create the categories though a wp function VS using sql.
Well, that's a bummer. I set global $wpdb and then used $wpdb->escape as above -- and it didn't work. Imports are still truncating at quote chars (and possibly others).
Now what?
Any ideas appreciated, meanwhile I'll dig into other import code and see if I can find a clue.
I think something else is going on here as the spec says the function does sanitation. Are you very sure your $article var contains all the data before inserting? Perhaps it's getting split previously.
I'm sorry for dragging up an old topic, but I'm have the same problem and it looks like it wasn't solved. I'm trying to import old stories from just html pages. Testing on the first few and I've have one that looks like its dropping off at dash character.
Responses (11)
Erstwhile founder — 5th June 2010 (1 year ago) #
I'd take a look at the code for one of the importers and see how it's been handled there.
Thanks,
Andrew
Lead Developer — 5th June 2010 (1 year ago) #
Hmm, I was under the Impression that that function expected unfiltered content. You can use mysql_real_escape_string() or the $wpdb class.
Better run title through it to.
Member — 6th June 2010 (1 year ago) #
Yeah, from all the docs, you'd think it expected unfiltered content. But from a bunch of random forum postings (not here, but around the Googlescape), others had similar problems.
I will check the importer code, tho... from textpattern.php:
Looks like that's the answer. I'll try it and let you know.
Lead Developer — 6th June 2010 (1 year ago) #
Make sure you declare the $wpdb global first:
global $wpdb;Member — 6th June 2010 (1 year ago) #
I just did something similar with one of my site, I imported everything, but I needed to create the categories also when I posted the info. I used the following code to do that. I looked for hours on how to create the categories though a wp function VS using sql.
wp_create_categories( $this->catname);
$catid = get_cat_ID( $this->catname);
-Brad
Member — 6th June 2010 (1 year ago) #
Well, that's a bummer. I set global $wpdb and then used $wpdb->escape as above -- and it didn't work. Imports are still truncating at quote chars (and possibly others).
Now what?
Any ideas appreciated, meanwhile I'll dig into other import code and see if I can find a clue.
Lead Developer — 6th June 2010 (1 year ago) #
I think something else is going on here as the spec says the function does sanitation. Are you very sure your $article var contains all the data before inserting? Perhaps it's getting split previously.
Member — 6th June 2010 (1 year ago) #
Yeah, it is. I wrote a big entity cleaner and that seems to have helped... sort of.
Member — 6th June 2010 (1 year ago) #
What about? $wpdb->escape(stripslashes($Body));
-Brad
Member — 14th March 2011 (1 year ago) #
I'm sorry for dragging up an old topic, but I'm have the same problem and it looks like it wasn't solved. I'm trying to import old stories from just html pages. Testing on the first few and I've have one that looks like its dropping off at dash character.
I've tried
$wpdb->escape
mysql_real_escape_string
stripslashes
and it's still dropping off.
Member — 26th December 2011 (4 months ago) #
This is still an issue in Wordpress 3.3.
Become a member