How to Create multiple pages programmatically in wordpress

Today we will see the simple and easiest way to create multiple pages programmatically. In this article I have divided the goal into small and easy steps so that not only its easy to understand but also its very easy to implement.

My Goal for this article is to create unique page for each alphabet dynamically (programmatically) as simple as like just running a cms page.

Image: Create pages in wordpress

Step1: Create a template file under your theme.

Go to theme directory for example:

C:\xampp\htdocs\wordpress\wp-content\themes\twentyfourteen

Create any template file there like testtemplate.php

Make sure you have proper comment on the top of the page. Under the comment must add the comment for ‘Template Name’.


/**

 * Template Name: Testtemplate

 * Description: A full-width template with no sidebar

 *

 * @package WordPress

 * @subpackage twentyfourteen

 */

<?php

alphabatic_custom_page_creation(); //Note this function will be defined under function.php

echo “All of your pages got created successfully! Delete me now from admin section”;

Step 2: Write a custom function as below in your function.php that is under your theme folder e.g., C:\xampp\htdocs\wordpress\wp-content\themes\twentyfourteen

Note: Here we are trying to create 26 page with a function. Please make sure you have updated _wp_page_template field value.


function alphabatic_custom_page_creation()

{

$ar_char = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

foreach ($ar_char as $key=>$value)

{

$new_slug = "website-start-with-".$value;

$keyword = "website names with ".$value."";

$desc = "List of website that started with ".$value." ";

$title = "Websites started with keyword ".$value."";

$new_post = array(

'post_title' => $new_slug,

'post_content' => 'special page contents..',

'post_status' => 'publish',

'post_date' => date('Y-m-d H:i:s'),

'post_author' => $userid,

'post_type' => 'page',

'_wp_page_template'=>pagetemplate.php',

'post_name' => $new_slug,

'post_category' => array(0)

);

$is_post_exists = post_name_if_exists($new_slug);

//if post is not already exists

if($is_post_exists)

{

$post_id = wp_insert_post($new_post);

if (!$post_id) {

        wp_die('Error creating template page');

    } else {

        update_post_meta($post_id, '_wp_page_template', pagetemplate.php');

    }

}

}

Step 3: Create a new page from admin section. And select newly created template

*. Login to Admin section

*. Create any page . As soon as we will open this page in browser then all of your pages gets created atutomatically.

*. Select template dropdown. You will see ‘Testtemplate’ would be available.

 

 

Step 4: Run this page from browser (that you have created on above step). As soon as you see the message from your browser, You are done. If you want that no one should again run the url then simply either make the page unpublished or delete it.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...