php - Programmatically adding Wordpress post with attachment -


i getting post_title, post_content , other things in $_request image file. want save post in wordpress database. have on page

<?php require_once("wp-config.php"); $user_id; //getting function $post_title = $_request['post_title']; $post_content = $_request['post_content']; $post_cat_id = $_request['post_cat_id']; //category id of post $filename = $_files['image']['name'];  //i got in array  $postarr = array(  'post_status' => 'publish',  'post_type' => 'post',  'post_title' => $post_title,  'post_content' => $post_content,  'post_author' => $user_id,  'post_category' => array($category)   ); $post_id = wp_insert_post($postarr);  ?> 

this things in database post don't know how add attachment , post meta.

how can that? can me? confused , have spent few days trying solve this.

to add attachment, use wp_insert_attachment():

http://codex.wordpress.org/function_reference/wp_insert_attachment

example:

<?php   $wp_filetype = wp_check_filetype(basename($filename), null );   $attachment = array(      'post_mime_type' => $wp_filetype['type'],      'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),      'post_content' => '',      'post_status' => 'inherit'   );   $attach_id = wp_insert_attachment( $attachment, $filename, 37 );   // must first include image.php file   // function wp_generate_attachment_metadata() work   require_once(abspath . "wp-admin" . '/includes/image.php');   $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );   wp_update_attachment_metadata( $attach_id,  $attach_data ); ?> 

to add meta data, use wp_update_attachment_metadata():

http://codex.wordpress.org/function_reference/wp_update_attachment_metadata

<?php wp_update_attachment_metadata( $post_id, $data ) ?>  

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -