1

I have a problem with wp_insert_post.

When I'm attempting to insert my post, the post_content essentially shows up as just blanks spaces. I even went into MySQL to confirm. Everything else, post_title, post_excerpt, etc shows up just fine.

I did a print_r($this->postData) Just prior to passing this to wp_insert_post just to be sure it wasn't empty and the right data was present.

The contents of the data are

<object> ... </object> 

Would that be an issue?

Here's my code that creates the $this->postData variable that's passed into wp_insert_post.

$this->postData = array(
        'post_content' => $this->jsonData['data'],
        'post_title' => $this->jsonData['title'],
        'post_excerpt' => $this->jsonData['description'],
        'tags_input' => $this->jsonData['tags'],
        'post_date' => date('Y-m-d H:i:s'),
        'post_status' => 'publish',
        'post_author' => 1,
        'post_date_gmt' => date('Y-m-d H:i:s'),
    );

thanks in advance!

3
  • 1
    is $this->jsonData['data'] unset or empty?
    – Sisir
    Commented Jun 11, 2014 at 17:50
  • Nope it isn't. I did a print_r($this->jsonData['data']) Just prior to passing this to wp_insert_post just to be sure it wasn't empty and the right data was present.
    – chi11ax
    Commented Jun 12, 2014 at 23:38
  • I mean print_r($this->postData)
    – chi11ax
    Commented Jun 13, 2014 at 1:56

1 Answer 1

3

OK, SO this is what I found out, the data

<object>...</object>

was the culprit. Wordpress sanitizes the input before committing to the database. Other non allowed tags include

<embed>...</embed>

I've seen this solution online

$postData = array( 
    ...
    'filter'=>true
    ...
);

However, some people say that's a security flaw. I dug a little deeper and I believe the best method I found out, seems to be this:

kses_remove_filters();
$id = wp_insert_post($postData);
kses_init_filters();

Without setting

'filter' => true,

I hope this helps everyone! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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