Running PHP in WordPress

By the end of this tutorial you will have learned how to create a child theme on your WordPress installation. You will also have learned how to run your own custom PHP inside of WordPress.

Create a child theme

You’re probably going to dismiss this instruction because you can get away with not doing this: Make sure you create a child theme first.

There are a lot of blog posts on how to create child themes in WordPress. Most of them are out of date.

The best tutorial on creating child themes in WordPress was written by Nick Roach. Follow the instructions on his blog to get this done before moving on.

Create a new page in WordPress

WordPress puts restrictions on where it will allow PHP to be run. You will need to create a template page. Please follow the instructions below to create a new page:

  1. Log into WordPress.
  2. Go to the Dashboard.
  3. Create a new page.
  4. Save the new page.

There, you’ve finished setting up a new page. Next we will go to the file browser and create the template file that your new page will point to.

Open your file browser and find the location of your wordpress installation. Mine is: /home/david/Documents/website

Navigate to the folder with your current theme. This should be the child theme you created in the first part of this blog post. Mine is: /home/david/Documents/website/wp-content/themes/hemingway-child

I copied the file page.php from the parent theme into this directory for my child theme. Let’s rename page.php to templatePage.php.

Remove the page’s content

The only parts we need of this page are the scaffolding. Let’s remove the following code from templatePage.php:

<div class="post-content">
  <?php the_content(); ?>
  <?php if ( current_user_can( 'manage_options' ) ) : ?>
  <?php edit_post_link( __('Edit', 'hemingway') ); ?>
  <?php endif; ?>

<!-- /post-content -->

Add your own PHP

Now templatePage.php should look like this. I’ve added comments to show where the content of the page should be put in the updated page. Be sure to wrap your PHP code with PHP tags!

<?php get_header(); ?>

<div class="wrapper section-inner bottom-margin">						

<div class="content left">
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div class="posts">

<div class="post">
      <?php if ( has_post_thumbnail() ) : ?>

<div class="featured-media">
          <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
    <?php the_post_thumbnail('post-image'); ?>
    <?php if ( !empty(get_post(get_post_thumbnail_id())->post_excerpt) ) : ?>

<div class="media-caption-container">
        <?php echo get_post(get_post_thumbnail_id())->post_excerpt; ?>

    <?php endif; ?>

<!-- /featured-media -->
<?php endif; ?>								

<div class="post-header">

<h2 class="post-title"><?php the_title(); ?></h2>


<!-- /post-header -->   				       	        		            

<!-- This is where we will put our code. Be sure to wrap your PHP with php tags! -->
<?php echo "Hello World!"; ?>
<!-- END CONTENT -->


<!-- /post -->
<?php if ( comments_open() ) : ?>
<?php comments_template( '', true ); ?>
<?php endif; ?>

<!-- /posts -->
<?php endwhile; else: ?>
<?php _e("We couldn't find any posts that matched your query. Please try again.", "hemingway"); ?>
<?php endif; ?>

<div class="clear"></div>


<!-- /content left -->
<?php get_sidebar(); ?>

<div class="clear"></div>


<!-- /wrapper -->
<?php get_footer(); ?>

Now we can run any PHP code inside of templatePage.php that we want. This page can be used to set up a form, query the database, and process forms. Contact me if you have any specific questions about this part.

Name the template

The next step is to name the template so it is accessible from the WordPress Dashboard. Add the following lines to the top of templatePage.php:

<?php /** Template Name: My Awesome Template */ ?>

Upload the template

Next you need to upload your new template file into the child theme directory. This is how WordPress will see your new template.

Set page with new template

Go back to the page you created at the beginning of this blog. Find the following section on the “Edit page” screen in the Dashboard (it’ll be in the sidebar. Quick note, if you are not self hosting, you will not see this option):


That’s it!

Save the page and reload it in your browser. If everything has been set correctly, you will see a standard looking page with your PHP code running inside of it.

Contact me if you have any questions.

Just submitted my first post to Hacker News

IMG_8933Very happy to have posted my first post to Hacker News. This weekend I hacked together 2 scripts that I have been wanting on my computer for a long time.

Gotta remember next time to post it with “Show HN:” in the future. I have been reading HN for a very long time and it’s nice to have finally posted something up there that I made.

I plan on posting more now that I have an account with my name. I want to get into more discussions on HN and maybe start responding to some questions, or give my feedback on things.

I’m glad that I set it up with my real name. That’ll keep me professional in all of my online comments.


Every single day I start out by creating a new text file with the following headings:





I write for a while in the NOTES section and gather my thoughts freely for about 20 minutes. Once the 20 minutes are up I look through the notes and group the action items into the DOING section. Then I take another 2 minutes to organize this list and prioritize it.

My entire day revolves around this list that I create in the morning. Hackers are lazy right? I wanted a way to automate the creation of this daily text file. So I wrote one up and tested it this weekend.



I was inspired to create this script when I installed ‘fortune’ on my computer. Fortune prints out a cool fortune to the command line. I collect quotes and I wanted to have a quick way to access these quotes.

I created a script that pulls a random quote from a quotes.txt file and prints it to the command line.


Hacker News

There’s a hacker meetup in the new room that I helped to design and create at the University of Hawaii at Manoa. I want to showcase these two scripts today.

I have a student assistant that I am working with. My goal is to have him post something on Hacker News by the end of this semester. We are working on a network map in our office.

Rather than have him post something to Hacker News without having done it myself, I decided to post these 2 utilities to HN to see how the process went. It was very easy. I did learn that HN has a limit on how many articles you can post at a time. I am currently waiting to post a link to Journal.


Ideas for Human Computer Interaction Study

mcc-logoI am taking ICS 464  at the University of Hawaii at Manoa. I need to come up with a project idea and I want to flip it and use it for my work. I want to be able to write up something that will benefit my employer.

There are a few things that I am interested in updating on our website. I am interested in creating ways to gauge if users are interested in certain parts of our website more than others.

I have looked into the Javascript Observer pattern that records mouse movements and can play them back on page. This is beneficial because you can actually play back a session.

Paid services that provide usability metrics


The best things that these services offer are:

  • Heatmaps – Display where users move their mouse to.
  • Scroll distance calculators –  Show how much of the page they have scrolled to.
  • Jump off points – Show where users get frustrated and leave your website.

I am also interested in how to make our interactive brochure of a website more compelling and interesting to listen to on a screen reader. I want to find out if tags are read out loud or not.

“Know thy users for they are not you.” – HCI Proverb

I want to curate the experience of parsing a page on a screen reader. I want to hear how it sounds on a screen reader. Then I want to see how I can update our website to make the content more inviting; and make the process of doing things on the website easier and less frustrating.

I would like to hire blind beta testers for the usability study.

I would also like to make a checklist for creating a good usability study.

I am currently using Google Analytics on our website. It’s easy enough to integrate into a website with the Google Analytics code. //TODO: Insert link to how to article on integrating google analytics into wordpress.

Potential Usability Targets

Workshop Registration




  • More compelling.
  • More engaging.
  • Less tedious.

Career Counsellor Registration




  • Portray current state of sign ups. What times are still available?
  • Suggest other options after sign up is successful: Sign up for a workshop, learn more about our programs and services, ask a question.
  • Less tedious.

Career Fair Registration




  • More compelling.
  • More engaging.
  • Less tedious.

Search functionality




  • More compelling.
  • More engaging.
  • Less tedious.


I am meeting with Professor Suthers today to discuss this project. Once this meeting is finished I am hoping to have refined the area of interest for this project. I also would like to have a clear framework for how to get started.