Saulius Antanavicius
Saulius Antanavicius

Reputation: 1381

Adding WordPress post via PHP

I need to somehow create a form in which the user could fill in several fields and select an image that would be added to the post,

The post would be formated from the input fields and I can do that just fine, but my problem is, I've done this before, soem tiem ago and can't remember how to assign a post to the correct category and also upload/assign the image via WP functions?

Any help would be great!

Upvotes: 0

Views: 146

Answers (1)

Chris Carson
Chris Carson

Reputation: 1845

To insert a post...

wp_insert_post($postarr, $wp_error = false)

The code's in wp-includes/post.php.

To insert an image...

media_handle_upload($file_id, $post_id, 
   $post_data = array(), $overrides = array( 'test_form' => false ))

$file_id is the index in $_FILES. $post_id is the post that you want to attach the file to.

That code's in wp-admin/includes/media.php, which might pose a problem if you are doing this on the front end rather than in the admin.

Upvotes: 1

Related Questions