how to create wordpress pages by script

You can create the pages by using script. You can execute any script using the “$wpdb->insert” or

“$wpdb->query”.

For creating wordpress post or page you can use the wp_insert_post function. Before calling wp_insert_post() it is necessary to create an object (typically an array) to pass the necessary elements that make up a post. The wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.

You can use following code for creating the page.  This code will execute when only you are logged in to wordpress.

// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
  $my_post['post_type'] = 'page',
  $my_post['post_category'] = array(8,39);

// Insert the post into the database
  wp_insert_post( $my_post );

You can create wordpress plugin and use this code. Use following code for wordpress plugin. Copy the code and create the create_wordpress_pages.php file and put in plugin folder.

/*
Plugin Name: Create WordPress Pages
Plugin URI: http://images.wordpressapi.com/
Description: Create wordpress pages by script
Version: 1.0
Author: wordpressapi
Author URI: http://images.wordpressapi.com/
*/
function create_wordpress_pages(){
// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
  $my_post['post_type'] = 'page',
  $my_post['post_category'] = array(8,39);

// Insert the post into the database
  wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'create_wordpress_pages');

You may like following Articles!

21 thoughts on “how to create wordpress pages by script

  1. Exactly what I needed! Thank you. One question though: If I wanted to execute this as a script outside of the administration area. How would I do that? For example, imagine a script that, once a day, adds a new article from a pull of articles I have defined to it and this way I do not have to manually post future items…

    I am assuming you'd have to include some admin functions in the header as well as wp-connect, etc. But am not sure which exactly. Any help would be appreciated.

  2. Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. I’ll tell you about that later…

  3. Would it be possible to do this through an URL? What I want is to display SMS messages that are send by our teammembers on the road. The messages are for our followers. It’s like a messageboard.
    I’ve found a SMS serviceprovider where the messages come in and they call an url like this:

    http://www.yoursite.nl/sms/incoming.php?shortcode=1008&keyword=EXAMPLE&message=Example& originator=31612345678&operator=02F440& receive_datetime=20110921142314&mid=9000123

    The keyword is a word that only we know (sort of password if you like) and the message is the thing we want to display in the posting that should be generated from this (or added to an existing page, that would be great too).
    Receive_datatime my also be usefull.

    Does anybody know of a way to archieve this?

  4. Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. I’ll tell you about that later…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>