How to Create Page In WordPress Programmatically

WordPress Create Page Programmatically

If you want to create a WordPress page programmatically, you need to begin by understanding that a WordPress page is a post type just like the post or custom post types.

In this quick post, I will show you how to create a page In WordPress programmatically using a quick code snippet added to your plugin or theme files.

Create Page In WordPress With PHP Programmatically

This is the basis of the code snippet I will share shortly on how to create a WordPress page programmatically. Essentially the code will contain the data of the various post elements like the title, content, or post meta as well as the status of the post.

When you have this data in an array, WordPress has a core function that is specifically designed to help with creating and inserting the page or post into the WordPress database.

This data is passed to the wp_insert_post() function that does the job of creating and inserting the page in the database as you will see shortly.

Step by Step Create Page Programmatically WordPress

Create Page In WordPress With PHP Programmatically

For a start, I have the blank WordPress installation without any page and we will demonstrate how to create pages In WordPress programmatically. Now we want to get this done in a step-by-step so that is easy for you to follow.

  1. In the first step, you need to have a place to add this code which can be a plugin or it can be a theme in the functions.php file.
  2. In the second step, you need to fill in the page data in the array below to replace the placeholder data I have added to the array. You can give your post the title, content, and status that can either be published or pending.
    $wordpress_page = array(
    'post_title' => 'Page title',
    'post_content' => 'Page Content',
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type' => 'page'
    );
    wp_insert_post( $wordpress_page );

     

  3. The post type should be page since we are creating a WordPress page. If it was a custom post type we would replace this value with the name of the custom post type while if it was a post we would have it as 'post'.
  4. Now we have the data the next step is to pass the data to the wp_insert_post() function that will automatically create and add the page in the database.

This simple code is what will help you to create a WordPress page programmatically and you can add it to the functions.php or your plugin code and will get this done.

Create Page Programmatically WordPress Example

For illustration, I have added the code to the functions.php file and I have wrapped the code in an action hook that is hooked at ínit and this code will look as follows :

add_action('init', 'njengah_create_wordpress_page_programmatically');

function njengah_create_wordpress_page_programmatically(){

$wordpress_page = array(
'post_title' => 'Page title',
'post_content' => 'Page Content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $wordpress_page );

}

 

The result of this code is the creation of the WordPress page programmatically every time the init event runs like if we reload the page we will create a WordPress page programmatically.

How to Create Page In WordPress Programmatically

You can change the hook to work with any other logic in your WordPress theme or plugin. When you check in the back end you will see the WordPress pages are created automatically

create page programmatically wordpress

Conclusion

In this post, we have highlighted how to create a Page in WordPress programmatically using the wp_insert_post() core WordPress function that helps with creating and inserting the page into the database.

I hope this tutorial has helped you to learn how to create pages in WordPress automatically and you can implement it in your project.

If you would like more help on this topic, I can answer your questions and possibly help with your code. For any more help that requires a WordPress developer, feel free to get in touch.

Similar Articles