LearnBeginner’s Guide to PHP for WordPress – Part 1 of 3

Zac Gordon
writes on June 1, 2015

Share with your friends










Submit

WordPress allows us to do so much in terms of building websites without knowing any code. With themes and plugins and a powerful admin area there is a lot we can do without typing any HTML, CSS, JS or PHP. However, at a certain point you need to get into the code to really customize things and get a site to look or behave exactly how you want.

When you first get into customizing WordPress sites, you quickly realize that a lot of WordPress relies on PHP, the server side programming language. In this article we are going to go over some important things to know when you start trying to customize PHP files and PHP code in WordPress. This article is an accompaniment to our course, PHP for WordPress, which you may want to check out as well.

An Introduction to PHP

What is PHP?

PHP is one of the primary server-side programming languages for the web and is what powers a lot of WordPress. PHP allows you to do some important things:

  • Save content to a database
  • Read content from a database
  • Write loops and conditional statements
  • Pull in WordPress specific information
  • Pull in files like images, media
  • Can pull in files like images, media and other PHP, CSS and JS files

Learning how to do everything you may dream up in PHP can take a while to learn, but luckily, when you’re first getting started with PHP in WordPress, you’re usually just looking through already written PHP in order to customize little bits and pieces, and you don’t need to know how to do everything from scratch.

Before we get into some examples of PHP you’ll see and may need to write, let’s address how to edit PHP files.

Working with PHP Files

A PHP file is simply a file that ends with .php. Some PHP files contain only PHP code and some contain a combination of PHP and HTML code. WordPress has a few different types of PHP files:

  • Core files – usually contain only PHP code and are not files that you should edit since that may break your WordPress install.
  • Theme files – contain a combination of PHP and HTML and are the most common types of files you will find yourself editing.
  • Plugin files – contain primarily PHP code although may have some HTML, CSS and JavaScript mixed in with them. You do not generally edit plugin files unless you are building a plugin yourself or making changes to someone else’s plugin.

When it comes to editing PHP files you are going to want to use a code editor, something like Sublime Text. You don’t want to use word processing software like Word since it may add additional characters behind the scenes that will break the code.

PHP Blocks

Within all of the different types of PHP files, you will find what’s called a PHP block. When you write PHP you need to write it inside of PHP blocks otherwise it will display as plain text and not be processed as a programming language by the server.

A PHP block starts with a “<?php” and ends with a “?>”. Blocks can span many lines or sometimes appear inline along with other code.

Here is an example of a PHP block with a PHP comment inside of it.

<?php

// Example of a PHP Comment

?>

When you are working inside of template files you will often see short PHP blocks that appear within HTML tags like the one below. This is an example of how you would display the title of a post inside of an h1 tag.

<h1><?php the_title(); ?></h1>

Many of the WordPress core files don’t actually ever close their PHP blocks because the server will close them automatically. However, in general, you should close your PHP blocks.

Troubleshooting PHP Errors

It’s very helpful to have an idea of what to do when you see an error related to PHP. There are a few ways that errors can occur and a number of effects they may have.
An error may simply cause a warning, or it may break your entire site and display an error message. It may also break the site and display something called the “White Screen of Death”.

When it comes to troubleshooting PHP problems in WordPress it is helpful to have PHP Debug Mode enabled in your wp-config.php file, which is in the root directory of your WordPress install. Once you open you can search for the line

define( 'WP_DEBUG', false );

And set it to

define( 'WP_DEBUG', true );

This will make sure that error messages are being displayed on your site. Remember though, once you are done troubleshooting to set the value back to false since there is some information that error messages display that you do not want displaying to the general public.

The reason for this is that some error messages contain information about what folders your WordPress site is installed in and these folders can give away information that hackers can use to access your site.

Breaking Down an Error Message

Let’s look at an example of an error that would come from a missing semicolon at the end of a line. PHP statements end with semicolons and if you’re missing one from your code, then it will likely display an error like this:

Parse error: syntax error, unexpected end of file, expecting ',' or ';' in /home/wptreehs/public_html/wp-content/themes/twentyfifteen-child/functions.php on line 19

We can tell three basic things from an error message like this:

  1. The error itself
  2. The file where it occurs
  3. The line number where the error happens

The error itself is the first segment of this message and stops right before the word “in”.

Parse error: syntax error, unexpected end of file, expecting ',' or ';'

Take the time to read the error message and see if you can make sense of it. In this case, it is telling us that there is a “syntax” error and it is expecting to have a comma or a semicolon somewhere. If you’re ever not sure what an error message means, you can copy this portion of the error message into Google for an explanation.

The next segment of the error message tells us the file where the error message occurs. If you are ever posting for help into a forum, you don’t want to paste this whole message because it is giving away the absolute location of your file and part of this location path can include the username for your account. In the example above, “wptreehs” is the root directory folder and could also be the username for this account. If you are posting up error messages, you are only really concerned with the last segment or two, which will likely tell you the folder and file name.

For example, in this error, we are primarily concerned with the “twentyfifteen-child/functions.php” since that it telling us that the error is in our functions.php file inside of the “twentyfifteen-child” folder. If you are posting up error messages to a folder you may want to share the last folder name and file name to help others help pinpoint the problem.

The last segment is the line number, which tells us where in the file the problem occurs. One important thing to know though about line number errors is that sometimes the error will occur on the line before where the error says it occurs. The reason for this is that the PHP processor does not know an error has occurred until it gets to the next line. So when you go back to your code to look for the error, sometimes you will want to check the line before where the error message says the error occurs.

Where to Go for Help with Error Messages

If you need help with WordPress PHP error messages, the first good place to try is Google. If that doesn’t lead to an answer you may try the Treehouse forums (especially if it relates to a Treehouse WordPress course), the WordPress Stack Exchange Forum, or the WordPress.org forums.

If the error message occurs for a specific plugin then you should try contacting the plugin developer directly.  All plugins in the WordPress.org repository have a Support tab where you can submit issues you are having and most premium plugins have a website or marketplace page where you can contact the developers.

Next in the Series

In the next two parts to this series, PHP for WordPress, we will get more into some basics about coding PHP, as well as look at the ever important Loop.
In the meantime, please check out the PHP for WordPress course in our library.

5 Responses to “Beginner’s Guide to PHP for WordPress – Part 1 of 3”

  1. I just could not go away your website before suggesting tthat
    I really enjoyyed the usual info a person supply in your guests?
    Is going to be again steadily iin orfder to check out new posts

  2. This is an enormously helpful guide, thank you so much!

  3. This is a really nice post for Beginner’s who want to learn PHP. Thanks for your sharing,,, Please check this http://www.freeewpthemes.com

  4. jasveer singh on June 3, 2015 at 4:58 am said:

    this is good

Leave a Reply

Want to learn more about PHP?

Learn how to create dynamic websites using the back-end programming language, PHP.

Learn more