0

I'm using wp_insert_post (for several reasons) in a script I'm writing and it's causing any string that has an apostrophe in it to be truncated up to that character (including the character). Here's the code I've got to test it:

$question = "If you’re in need, this is who you call";

echo $question."<br />";

$post = array(
    'post_title'     => $question,
    'post_status'    => 'publish',
    'post_type'      => 'question'
);  

echo $post['post_title'];

$the_post_id = wp_insert_post($post);

It's weird because when I echo out both the strings, the echo the correct thing on both ends so I've isolated it down to the wp_insert_post function that's causing the issue. When I view it in WordPress, the title is just 'If you' and then ends.

I found a similar problem here:

http://premium.wpmudev.org/forums/topic/q-wp_insert_post-truncates-post-content-on-quotes-is-there-a-proper-way-to-escape-these-things

But there doesn't seem to be any solution to it on that page. Any ideas on what's going on here and how to solve it? No idea why it's causing it. I'm looking into it to see if has something to do with something else in my code but it seems like it shouldn't because the array element seems formatting just fine when it's echoed out before the wp_insert_post() function.

Thanks for your help!

EDIT

I should have specified that these are having to be called from an array where I'm grabbing data from a different source so I won't necessarily have access to the strings directly. I tried mysql_real_escape_string() which should do that job automatically as well and that didn't seem to work...

0

Try escaping the apostrophe character, i.e. $question = "If you\’re in need, this is who you call"; Apostrophes have a special meaning in PHP code, which is probably why it's truncating.

  • 1
    Mark, while your answer is correct, you might want to show how escaping would happen in this case. – kaiser Jun 3 '14 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.