Title: Insert post from PHP script
Last modified: August 31, 2016

---

# Insert post from PHP script

 *  Resolved [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/)
 * Hello,
 * How can i insert a post from a PHP file.
    Please give me a working “wp_insert_post”
   example.
 * Thanks
    bye

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/insert-post-from-php-script/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/insert-post-from-php-script/page/2/?output_format=md)

 *  [beirdostudio](https://wordpress.org/support/users/beirdostudio/)
 * (@beirdostudio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203746)
 *     ```
       // Create post object
       $my_post = array(
         'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
         'post_content'  => $_POST['post_content'],
         'post_status'   => 'publish',
         'post_author'   => 1,
         'post_category' => array( 8,39 )
       );
   
       // Insert the post into the database
       wp_insert_post( $my_post );
       ```
   
 * [from wp_insert_post](https://developer.wordpress.org/reference/functions/wp_insert_post/)
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203749)
 * but first need some include, right?
    if yes what kind of files need to include?
 *  [beirdostudio](https://wordpress.org/support/users/beirdostudio/)
 * (@beirdostudio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203756)
 * I don’t see what includes you are talking about and I would need much more info
   to understand your problem.
    what are you going to use the insert for? do you
   have a lot of posts that you are trying to bulk insert once or is it some kind
   of functionality that fires after a certain event? I need to understand your 
   need to be able to answer you accordinglly.
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203760)
 * Okay, i want to insert post to wordpress from PHP file.
    When i open the “mydomain.
   com/testinsert.php” that script will post the content to db and the wordpress
   will show it…
 *  [beirdostudio](https://wordpress.org/support/users/beirdostudio/)
 * (@beirdostudio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203764)
 * ok, try adding this to your php file:
 * `require("wp-includes/post.php");`
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203772)
 * <?php
 * ini_set(‘display_errors’, 1);
    error_reporting(E_ALL);
 * require(“wp-includes/post.php”);
 * // Create post object
    $my_post = array( ‘post_title’ => “qwe”, ‘post_content’
   => wp_strip_all_tags($content), ‘post_status’ => ‘publish’, ‘post_author’ => ‘
   1’, ‘post_category’ => array( 1,2 ) );
 * wp_insert_post( $my_post );
 * ?>
 *  [beirdostudio](https://wordpress.org/support/users/beirdostudio/)
 * (@beirdostudio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203800)
 * If your question has been answered, we would love if you would mark this topic
   as resolved in the right hand sidebar. This helps our volunteers find the topics
   that still need attention and more people will get helped, possibly like you 
   did.
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203801)
 * still not answered, i dont know still how can i insert post to my wordpress 🙁
 *  [beirdostudio](https://wordpress.org/support/users/beirdostudio/)
 * (@beirdostudio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203807)
 * well you just copied the code and put it here so I assumed you found the solution.
   
   did you try this code? what happened if you did?
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203853)
 * yes i tried, but the error is:
    “Fatal error: Call to undefined function wp_strip_all_tags()
   in /home/penzker/public_html/test.php on line 11”
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203854)
 * Wait, you’re trying to use WordPress outside of WordPress?
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203858)
 * okay, start again, my question is how can i insert post to my wordpress from 
   PHP file?
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203859)
 * What do you mean by “post”?
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203860)
 * We have no knowledge of your system that you’re running outside of WordPress.
   If your external system creates posts then you need to tell us how it does it.
 *  Thread Starter [szunyi](https://wordpress.org/support/users/szunyi/)
 * (@szunyi)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/#post-7203862)
 * ok thx

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/insert-post-from-php-script/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/insert-post-from-php-script/page/2/?output_format=md)

The topic ‘Insert post from PHP script’ is closed to new replies.

## Tags

 * [insert post](https://wordpress.org/support/topic-tag/insert-post/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [wp_insert_post](https://wordpress.org/support/topic-tag/wp_insert_post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 4 participants
 * Last reply from: [jpvitulli](https://wordpress.org/support/users/jpvitulli/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/insert-post-from-php-script/page/2/#post-8492728)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
