How to Create Pages Programmatically and Add Template – WordPress
- Article
- Comment (3)
Creating of custom pages through program is a part of theme building . By the before getting into topic we need some idea to initiate the task. Here i am going to create a custom contact page for my theme and it will be added through programmatically, when the theme is activated.
Now, first we have to create a template page for contact page. Here is the code for contact page.
<?php /** Template name: KV Contact */ // contact form content goes here. ( contact.php) $message = ''; if(empty($_POST['name'])) die('Please ensure name is provided'); else { $isNumeric = preg_match("/\S*\d+\S*/", trim($_POST['name'])) ? true : false; if( $isNumeric == true) die('Please Dont provide a fake one by wasting of time'); else $name= trim($_POST['name']); } if(empty($_POST['phoneno']) ) die(' Please Enter Your Phone Number for Communication!'); else { $number = trim(preg_replace("/\D/","",$_POST['phoneno'])); if( (strlen($number) >= 10) && (strlen($number) <= 14) ) { /* ok */ } else { die(' Enter a valid phone number for communication ! '); } } if(empty($_POST['message'])) die(' Please enter your Message!'); else $message = trim($_POST['message']); // Email Subject if(empty($_POST['subject-mail'])) die(' Please enter a subject!'); else $subject = trim($_POST['subject-mail']); $sender_email = trim($_POST['email']); // Email to send to $to = 'varadharajv@kvcodes.com'; // Construct a header to send who the email is from $header = 'From: ' . $name. '<' . $sender_email . '>' ."\r\n" . 'Phone Number : '.$number. "\r\n" .' Sub : ' .$subject . "\r\n" ; // Try sending the email if(!mail($to, $subject, $message, $header)){ die('Error sending email.'); }else{ die('Email sent!'); mail($sender_email, 'Thank you for Contacting Us!', ' Hi!, Our Marketing Executives Contact You very soon. Thank you for message. <br> <br> With Regards, <br>KV Codes' , 'From : '.$to); } get_header(); ?> <div id="wrapper"> <div id="inner" style="background:#f9f9f9;"> <div class="container"> <div class="section group"> <div class="col span_3_of_4"> <div class="roundedbox"> <div id="greybar" class="pricesheader"> <div class="container"> <h1><?php the_title(); ?></h1> <div class="clear"></div> </div> </div> <div id="result" style="display :none" > </div> <table width="100%" style="border: 0px solid #ddd; " > <form method='post' action='cont.php' id='contact-form'> <tr> <td style="padding-bottom: 15px;"> </td> <td> <div style="font-size:9px; padding-top : 20px; " > <strong> Note : </strong> * fields are necessary to fill it properly! </div> </td> </tr> <tr> <td> Name* : </td> <td style="padding-bottom: 15px;"> <input name="name" id="name" type="text"> </td> </tr> <tr> <td style="padding-bottom: 15px;"> Email: </td> <td> <input name="email" id="email" type="text"> </td> </tr> <tr> <td style="padding-bottom: 15px;"> Phone Number *: (+91) </td> <td> <input name="phoneno" id="phoneno" type="text"> </td> </tr> <tr> <td style="padding-bottom: 15px;"> Website : </td> <td> <input name="website" id="website" type="text"> </td> </tr> <tr> <td style="padding-bottom: 15px;"> Subject : </td> <td> <input name="subject-mail" id="subject" type="text"> </td> </tr> <tr> <td style="padding-bottom: 15px;"> Message * : </td> <td> <textarea name="message" class="msg" id="message" height="60px" ></textarea> </td> </tr> <tr> <td ></td> <td style="padding-bottom: 15px;"> <input type="submit" name="send" value=" Send " class="sidebar-btn" > </td> </tr> </form> </table> </div> </div> <?php get_sidebar('static-sidebar'); ?> </div> </div> </div> </div> <?php get_footer(); ?>
Than write the following function to call the template when the contact page is opened. Open your currently activated theme and open your “functions.php” file the add the following function into it.
<?php function kv_create_contact_page() { $page = get_pages(); $contact_page= array( 'slug' => 'contact', 'title' =>'KV Contact' ); foreach ($pages as $page) { $apage = $page->post_name; switch ( $apage ){ case 'contact' : $contact_found= '1'; break; default: $no_page; } } if($contact_found != '1'){ $page_id = wp_insert_post(array( 'post_title' => $contact_page['title'], 'post_type' =>'page', 'post_name' => $contact_page['slug'], 'post_status' => 'publish', 'post_excerpt' => 'User profile and author page details page ! ' )); add_post_meta( $page_id, '_wp_page_template', 'pages/contact.php' ); } } add_action('admin_init', 'kv_create_contact_page'); ?>
That’s it you have done . Creating of a wordpress page and call the template through programmatically.
If you have any doubts and bugs just ping me a comment.
good afternoon, I tested your code, I’m doing this myself, but it this create multiple pages, the file I’m staying contact.php in /page-templates/contact.php within the folder of the theme.
Yes, it create multiple pages, whenever you reload the wp-admin. when you post slug varied from the existing one. The very basic thing is, you need to provide the slug correctly, Else, your database will get occuiped by the default contact page. Here the code may be some alignment problems, but it worked fine on my localhost, than only i wrote this post.
Varadharaj Good day, your code works perfectly, nothing more in line 4 in kv_create_contact_page () function variable $page would have to be $pages. So in this way the foreach runs correctly. Thanks for all Varadharaj V