How to create a new wordpress page programmatically

I am creating a plugin in wordpress.
I am unable to find a way to create a new wordpress page using plugin.
I want to make a new page on the front end of the wordpress when the user activates the plugin.

Related posts

4 comments

  1. With the help of above code, you can create dynamic page.
    Firstly we need to check if the post we are going to create is available or not.
    If present you dont need to create another one you can edit the content of the
    page. But if you changed the title of the page new page will be created.
    Here I have created a page with the help of title of the page.

    $check_page_exist = get_page_by_title('title_of_the_page', 'OBJECT', 'page');
    // Check if the page already exists
    if(empty($check_page_exist)) {
        $page_id = wp_insert_post(
            array(
            'comment_status' => 'close',
            'ping_status'    => 'close',
            'post_author'    => 1,
            'post_title'     => ucwords('title_of_the_page'),
            'post_name'      => strtolower(str_replace(' ', '-', trim('title_of_the_page'))),
            'post_status'    => 'publish',
            'post_content'   => 'Content of the page',
            'post_type'      => 'page',
            'post_parent'    => 'id_of_the_parent_page_if_it_available'
            )
        );
    }
    
  2. Something like this

    function some_function()
    {
       $post_details = array(
      'post_title'    => 'Page title',
      'post_content'  => 'Content of your page',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type' => 'page'
       );
       wp_insert_post( $post_details );
    }
    
    register_activation_hook(__FILE__, 'some_function');
    

    See here for other possible params.

  3. A convenient helper function to create a bunch of pages:

    function create_page($title_of_the_page,$content,$parent_id = NULL ) 
    {
        $objPage = get_page_by_title($title_of_the_page, 'OBJECT', 'page');
        if( ! empty( $objPage ) )
        {
            echo "Page already exists:" . $title_of_the_page . "<br/>";
            return $objPage->ID;
        }
        
        $page_id = wp_insert_post(
                array(
                'comment_status' => 'close',
                'ping_status'    => 'close',
                'post_author'    => 1,
                'post_title'     => ucwords($title_of_the_page),
                'post_name'      => strtolower(str_replace(' ', '-', trim($title_of_the_page))),
                'post_status'    => 'publish',
                'post_content'   => $content,
                'post_type'      => 'page',
                'post_parent'    =>  $parent_id //'id_of_the_parent_page_if_it_available'
                )
            );
        echo "Created page_id=". $page_id." for page '".$title_of_the_page. "'<br/>";
        return $page_id;
    }
    
    create_page( 'How it works', 'This is how it works');
    create_page( 'Contact Us', 'The contact us page');
    create_page( 'About Us', 'The about us page');
    create_page( 'Team', 'The team page');
    $pid = create_page( 'Sample Page', 'This is sample page');
    create_page( 'Sample SubPage 1', 'This is sample SubPage 1',$pid);
    create_page( 'Sample SubPage 2', 'This is sample SubPage 2',$pid);
    
    

Comments are closed.