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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments