15

How can I automatically create a WordPress page (for example, when plugin is activated)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phil
  • 3,628
  • 8
  • 34
  • 36

2 Answers2

25

Use wp_insert_post(), which can insert pages as well: http://codex.wordpress.org/Function_Reference/wp_insert_post

See post_type below.

$post = array(
  'ID' => [ <post id> ] //Are you updating an existing post?
  'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs.
  'page_template' => [ <template file> ] //Sets the template for the page.
  'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
  'ping_status' => [ ? ] //Ping status?
  'pinged' => [ ? ] //?
  'post_author' => [ <user ID> ] //The user ID number of the author.
  'post_category' => [ array(<category id>, <...>) ] //Add some categories.
  'post_content' => [ <the text of the post> ] //The full text of the post.
  'post_date' => [ Y-m-d H:i:s ] //The time post was made.
  'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT.
  'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
  'post_name' => [ <the name> ] // The name (slug) for your post
  'post_parent' => [ <post ID> ] //Sets the parent of the new post.
  'post_password' => [ ? ] //password for post?
  'post_status' => [ 'draft' | 'publish' | 'pending' ] //Set the status of the new post.
  'post_title' => [ <the title> ] //The title of your post.
  'post_type' => [ 'post' | 'page' ] //Sometimes you want to post a page.
  'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
  'to_ping' => [ ? ] //?
);  

// Insert the post into the database
wp_insert_post( $post );
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
  • 1
    Because Pages are simply Posts that are flagged as Pages. – Tyler Carter Jul 27 '09 at 01:12
  • Thanks. Easier that I thought :) – Phil Jul 27 '09 at 13:29
  • Also, newbie plugin dev question... will this make page when i activate plugin or i need to add some code to specify that i want plugin to make that page at the moment it's activated? – Phil Jul 27 '09 at 13:31
  • That's up to you. When you make the plugin, there's space for you to do things when the plugin is activated as well as a multitude of hooks and actions you can hook into and do things only when a certain event occurs. Read up on WP plugin development online. – Artem Russakovskii Jul 27 '09 at 14:59
  • This code goes to infinite loop when I add it to my functions.php – Vaibhav Bhanushali Dec 20 '12 at 19:06
  • The only problem I see with this method is that these new pages are then susceptible to accidentally being deleted by a wp-admin prior to deactivating the plugin that created them. https://wordpress.stackexchange.com/questions/142884/hide-certain-pages-posts-on-wp-admin-show-custom-filter and https://stackoverflow.com/questions/12650079/how-to-prevent-a-wordpress-page-from-being-deleted-by-a-user are two posts that offer some possible solutions to that problem. – Damian Green Aug 05 '17 at 16:31
  • is there an updated version of this answer for TYPO3 in version TYPO3 9.5.5? – SL5net Apr 01 '19 at 19:02
-4

Wordpress provides the wp->query API method for database abstraction. You can create the appropriate query to make a Page when needed.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • 7
    That's a pretty bad suggestion in general. You should use query only if you can't achieve the same with an API function. The main reason being future table changes may break your query while functions are hopefully upkept. – Artem Russakovskii Jul 27 '09 at 01:15