Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new wordpress page programmatically

Tags:

php

wordpress

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.

like image 610
Sppidy Avatar asked Aug 31 '15 15:08

Sppidy


4 Answers

This way you can add page

// Create post object
 $my_post = array(
   'post_type'     => 'page',
   'post_title'    => 'My post',
   'post_content'  => 'This is my post.',
   'post_status'   => 'publish',
   'post_author'   => 1
 );

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

For detail see this https://codex.wordpress.org/Function_Reference/wp_insert_post

like image 58
Ghulam Ali Avatar answered Oct 29 '22 01:10

Ghulam Ali


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);

like image 37
J W Avatar answered Oct 29 '22 01:10

J W


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'
        )
    );
}
like image 31
sonu pokade Avatar answered Oct 29 '22 01:10

sonu pokade


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.

like image 20
Igor Yavych Avatar answered Oct 29 '22 02:10

Igor Yavych