Beginners Guide To PHP in WordPress

WordPress uses different types of code to run the software.

PHP, CSS, HTML and Javascript are 4 different code languages you’ll find in the core WordPress files.

PhP is the most powerful code that runs the core WordPress files, any plugins you install and your active theme.

There’s a lot more to PHP than custom functions however custom functions are widely used when customizing & designing WordPress sites.

Where To Insert PHP Code

You can place your php code directly into your child themes template files or in your themes functions.php file.

Another solution is to install a hook plugin like Genesis Simple Hooks which comes in handy if you only want to add a few small lines of HTML, Text or PHP code in a specific location your theme uses hooks.

When adding PHP code to a hook location using a plugin, make sure you wrap the PHP in opening and closing PHP tags. Example:

<?php
echo "Output Text Using PHP in a Themes Hook Locations";
?>

To avoid bloating your template files with code and to keep all your code in one place, its best practice to create a child theme and add a functions file for all your custom code.

This way all your custom coding won’t be lost when you update your parent theme framework or the WordPress core files.

Opening & Closing PHP Tags

PHP code always starts with <?php and ends with ?>

If you’re placing PHP code in your template files, you’ll always need to add these opening and closing PHP tags.

Considering adding PHP directly to your files is Not best practice, you don’t need to add these tags to every block of code you write in your functions file. The reason for this is, your functions file will already include an opening PHP tag at the beginning and also sometimes at the end of the file depending on how your theme is coded.

You simply add the PHP code after the opening PHP tag.

Where To Add Your PHP Code

Example of parent theme functions file:

php functions file

The above screenshot shows you a section the a Woo theme functions file which you can use to add custom php code for custom functions. Woo themes include the Woo framework in every theme whereas StudioPress child themes are built based on a separate framework named Genesis.

Child themes like what StudioPress offer, include a functions.php file for custom coding. This way you don’t need to add or edit the PHP code in the parent theme or theme framework which in this case is Genesis.

You can also use the built in file editor located under your Appearance tab in your Dashboard however this is not best practice.

Editing PHP Code

Choose your favorite text/code editor and always backup the files you are editing in case you make a mistake.

Displaying Text Using PHP

If you simply want to output text using php code, you write code like this:

<?php
echo "Howdy Folks!";
?>

echo is used when you want to output text and this code will output the text Howdy Folks!

But where will the code output the text?

If you place this code in your themes functions.php, it will output the text at the top of every page on your site.

If you place the code in a specific location in a specific template file like your footer.php, it will output the text in your footer. Not only that, it will always output this text every time the page loads.

A better way to determine when the code should be executed to output the text is to use a function.

And a better way to determine the location your text is displayed, is to use WordPress hooks and even better still, your themes hooks.

Considering the fact adding PHP code to your template files isn’t an efficient way to use PHP code for customization, you’ll need to add this code to a function and include a hook to determine the exact location your content displays in your theme.

Let’s first look at using basic custom functions and then look at which hooks to use to determine the location your text is displayed.

Functions

We’ll write a very basic custom function to execute the code that outputs the text when the page loads in the browser, only when it is called using the function.

The first step is to create your custom function and give it a name which describes what it will do.

function your_custom_function_name()
{
echo "Howdy Folks!";
}

Function name

function display_welcome_text()
{
echo "Howdy Folks!";
}

Now that we’ve named the custom function, we want to choose a location to display (output) the welcome text in our theme.

You can execute the PHP code anywhere in your theme using hooks.

Firstly, we’ll add a WordPress hook that works in any theme to display our welcome text in the footer.

The hook WordPress uses for the footer is wp_hook so all we need to do now is add it to the code.

To add the hook to the code, we use add_action.

add_action( 'wp_footer', 'display_welcome_text' );
function display_welcome_text() {
echo "Howdy Folks!";
}

This code will work in any theme because it uses a WordPress hook. But what if your theme uses it own hooks like Genesis or Woo?

What if you don’t want the text to output in the footer?

What if you want it to output the text before or after your posts content?

Then you need to use the appropriate hooks your theme offers.

This code displays the welcome text before the posts content when using a Woo theme

add_action( 'woo_post_before', 'display_welcome_text' );
function display_welcome_text() {
echo "Howdy Folks!";
}

This code displays the welcome text after the content when using a Genesis child theme from StudioPress.

function display_welcome_text() {
echo "Howdy Folks!";
}
add_action( 'genesis_after_content', 'display_welcome_text' );

Ok, but the text also displays on archive pages like the home page or blog page so how do we filter that out?

We add a conditional tag.

You can view code examples using conditional tags for Woo Theme users and Genesis child theme users.

Here’s one example:

add_action( 'genesis_after_content', 'display_welcome_text' );
function display_welcome_text() {
if (is_single() && is_active_sidebar( 'after-post' ) ) {
echo "Howdy Folks!";
}}

This code will display your plain text message on all single posts after the post content.

Why do we use 2 closing curly brackets?

Because we need to end both the custom function and the conditional statement.

Local Code Development

Install WordPress locally using instantwp for Windows or MAMP for Mac users.

This way you can make as many mistakes as it takes to learn how to write PHP code until it works.

Displaying PHP Code in WordPress

Based on my experience, the best way to display and store PHP code snippets is to use a free online service like Github Gists.

Read more about displaying PHP code in WordPress.

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.