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:wwwwordpresswp-includespost.php on line 144
Please help.
I finally got the answer to the problem.
To make this code work:
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 http://www.yourdomain.com/wpinstalldir/autoposts.php
Now when you will execute this script at http://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.Hope the answer on this link is useful.
https://stackoverflow.com/questions/3947979/fatal-error-call-to-undefined-function-add-action/3952058#3952058
Make sure the common functions are accessible in your
Z:wwwwordpresswp-includespost.php
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: