Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have to insert posts programatically in Wordpress. I want that I should be able to publish posts via a url. Something like www.mypage.com/insertnewpost.php?title=blah&content=blahblahblah&category=1,2,3

The following code works only if I use it inside the functions.php file of the themes.

include '../../../wp-includes/post.php';
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

However when I try to create a new page like insertnewposts.php and use the above code there I get errors like Fatal error: Call to undefined function add_action() in Z:\www\wordpress\wp-includes\post.php on line 144

Please help.

share|improve this question

migrated from stackoverflow.com Jun 21 '11 at 12:45

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 2 down vote accepted

I finally got the answer to the problem.

To make this code work:

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

we need to make sure that wordpress' bootstrap has been started... Wordpress bootstrap ensures that all wordpress configuration has been loaded into the memory. This includes all the core functions etc.

Coming back to the original problem of "inserting posts programatically", we need to call wp_insert_post() at the appropriate place after starting the wp bootstrap.

For this create a new php file like www.yourdomain.com/wpinstalldir/autoposts.php

<?php
/**
 * Writes new posts into wordpress programatically
 *
 * @package WordPress
 */

/** Make sure that the WordPress bootstrap has run before continuing. */
require(dirname(__FILE__) . '/wp-load.php');

global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
?>

Now when you will execute this script at www.yourdomain.com/wpinstalldir/autoposts.php your post will be created. Easy and simple!

Just adding the line require(dirname(__FILE__) . '/wp-load.php'); made all the difference.

share|improve this answer

Hope the answer on this link is useful.

Fatal error: Call to undefined function add_action()

Make sure the common functions are accessible in your Z:\www\wordpress\wp-includes\post.php

share|improve this answer
    
Thanks for the reply dude! I feel I am doing it the wrong way... I am trying to directly access wordpress' core file. Whereas I think I should somehow call it in a more elegant way by routing the function call through wordpress. I just dont know how to do it... –  hiprakhar Jun 20 '11 at 7:49

Just adding the line require(dirname(__FILE__) . '/wp-load.php'); made all the difference.

So when you want write programming post to WordPress do like this:

error_reporting(E_ALL);
ini_set("display_errors", 1);
set_time_limit(0);
require(dirname(__FILE__) . '/wp-load.php'); 

// Create post object
$my_post = array(
  'post_title'    => wp_strip_all_tags( 'test' ),
  'post_content'  => 'hi boy',
  'post_status'   => 'publish',
  'post_author'   => 1
);

$post_id = wp_insert_post( $my_post, $wp_error );

if ($wp_error==false)
 echo $post_id ;
else
  echo 'Not set';
echo 'Ended';
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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