how to create wordpress pages by script

how to create wordpress pages by script

For plugin or custom wordpress theme we need create wordpress pages by script. In this article we given code to how to create wordpress pages by script.

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.purabtech.in/
Description: Create wordpress pages by script
Version: 1.0
Author: wordpressapi
Author URI: http://images.purabtech.in/
*/
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');
how to create wordpress pages by script
how to create wordpress pages by script

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.