saurav.rox
saurav.rox

Reputation: 365

Creating pages when activating plugin WordPress

Anyone Please help! I have a plugin which creates post_type pages in the backend. The plugin is creating the desired pages but the problem is whenever i try to see the page list, it shows "No pages found" message. Screenshot here: http://prnt.sc/azalub

My code for creating the required pages here:

$new_page = array('post_title'    => $title,
                  'post_content'  => '['.$shortcode.']',
                  'post_status'   => 'publish',
                  'post_type'     => 'page'
                );
$post_id = wp_insert_post( $new_page );

Upvotes: 5

Views: 16680

Answers (3)

Rushabh Gohel
Rushabh Gohel

Reputation: 1

if you are using class try pass array($this, 'method_name') instead of function name.

Upvotes: 0

Mansukh Khandhar
Mansukh Khandhar

Reputation: 2582

For this purpose, you need to register with plugin activation hook.
See the code example below:

function add_my_custom_page() {
    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( 'My Custom Page' ),
      'post_content'  => 'My custom page content',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     => 'page',
    );

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

register_activation_hook(__FILE__, 'add_my_custom_page');

enter image description here

Upvotes: 16

saurav.rox
saurav.rox

Reputation: 365

While creating a custom post type, i had set 'query_var' to 'true' on one of the custom post type in my plugin. Setting it 'false' just made everything fine.

Upvotes: 1

Related Questions