Mr.Turtle
Mr.Turtle

Reputation: 3090

Wordpress - Add a page upon activation of plugin

I am currently making a Wordpress plugin, but I couldn't find an answer to this.

How do you add a page upon activation of the plugin?

I have added posts upon activation earlier with the wp_insert_post function, but I can't find a way to insert a page.

Upvotes: 0

Views: 97

Answers (2)

Momondo
Momondo

Reputation: 326

You should use the activation hook for plugins in order to make any actions upon the plugin activation.

register_activation_hook( __FILE__, 'activation_hook_callback');

function activation_hook_callback()
  {
   //add the post type and other options in the array for the query
    $page = array(

          'post_status' => 'publish' ,
          'post_title' => 'Page name',
          'post_type' => 'page',
    );  
    //add the page and ID will be saved.
    $the_page_itself = wp_insert_post( $page );

  }

This should work.

Upvotes: 2

Mr.Turtle
Mr.Turtle

Reputation: 3090

I solved it. Wordpress has several post-types:

Post (Post Type: 'post')
Page (Post Type: 'page')
Attachment (Post Type: 'attachment')
Revision (Post Type: 'revision')
Navigation menu (Post Type: 'nav_menu_item')

To add a post upon activation of my plugin:

function add_page_upon_activation() {
    $arr = array(
        'post_title' => 'title',
        'post_name' => 'slug',
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_content' => 'yes, a nice page',
    );
    wp_insert_post($arr);
}
add_action( 'activated_plugin', 'add_page_upon_activation' );

Upvotes: 0

Related Questions