Random chars: 1893ea0ac4bf02fd

II.Create a customized WordPress Template

Prerequisite: chapter I.

Making a template is required if you need to embed PHP code into a WordPress page. There are other methods of course, but this is the proper way to do it.

As an example, let’s look at top part of this page. There’s this simple random characters generator which takes user’s input to define characters length as parameter to generate characters.

tmpl-rchars.php

This file is our customized template file and is saved under /wp-content/themes/twentynineteen-child folder. WordPress will detect its presence and load as a template on Dashboard. See following screenshot:

<?php
 /**
 Template Name: Random Chars Form
 module:    /wp-content/themes/twentyseventeen-child/tmpl-rchars.php
 author:    oldhendra
 date:    sep 14 2019, 10:43pm
 descr.:    a global page template for including a random characters custom form content within page
 */
 get_header();
 ?>
 <section id="primary" class="content-area">
     <main id="main" class="site-main"> 
 <!-- start of specific content from random chars template-->
 <?php $rchars_len = isset($_GET['rchars_len']) ? $_GET['rchars_len'] : 16; ?>
             <style>
                 #rchars label,
                 #rchars button{
                     vertical-align: middle;
                 }
             </style>
         <div class="entry">             <form id="rchars" class="entry-content" action="#rchars" method="get">                 <label>                     <span>Chars length:</span>                     <input name="rchars_len" type="text" value="<?php echo $rchars_len;?>" autocomplete="off">                 </label>                 <button type="submit">Submit</button>             </form>             <p class="entry-content">Random chars: <?php echo substr(md5(rand()), 0, $rchars_len); ?></p>         </div>
 <!-- end of specific content from random chars template-->
 <?php
     while(have_posts()):
         the_post();
     get_template_part( 'template-parts/content/content', 'page' );     // If comments are open or we have at least one comment, load up the comment template.     if(comments_open() || get_comments_number()){         comments_template();     } endwhile;//end while
 ?>
     </main><!-- #main --> </section><!-- #primary -->
 <?php
     get_footer();

Reserved Terms

When working with PHP embed please avoid using variables / constants already defined / used by WordPress. Find them in following pages:

Done 🙂

Leave a comment

Your email address will not be published. Required fields are marked *