Why learn PHP as a WordPress administrator?

You don’t need to know how to code proficiently in PHP to utilize PHP in WordPress. But, understanding basic PHP syntax will help you become familiar with the WordPress Loop, understand WordPress Functions, and give you the skills to add code snippets to your site.

What is PHP?

PHP is the core language of WordPress. Nearly all WordPress themes and plugins are primarily written in PHP.

Some Things PHP Can Do:

  • PHP can generate dynamic page content.
  • PHP can add, delete, and modify data in your database.
  • PHP can be used to control user-access (logins).
  • PHP can collect form data.
  • PHP works completely behind the scenes.

How PHP is used in WordPress

PHP is mostly used for getting and putting content to/from the WordPress database. The information stored in the database includes such things as posts, pages, comments and users. PHP functions can be written to fetch and display content from the database (such as a blog post).

Did you know? If you use the search box at the top of a WordPress web page, PHP is being used to give you the search results.

WordPress has a lot of  PHP files. A PHP file ends with the extension .php.

Some PHP files contain only PHP code and some contain a combination of PHP and HTML code.

  • WordPress theme files contain a combination of PHP and HTML.
  • WordPress plugin files contain primarily PHP code. They may have some HTML, CSS and JavaScript too.

You can learn more about PHP by studying the files in a simple WordPress theme. You will become more familiar with the syntax.

WordPress Theme with PHP

NAKED WORDPRESS is a straightforward and well-commented theme. You can download it to your computer and look at the files. In it, you’ll find a functions.php file. We will discuss that in a minute.

How to recognize PHP (syntax)

  • A PHP script starts with <?php and ends with ?>
  • PHP code goes inside PHP tags
  • A PHP script can be placed anywhere in a web page.
  • A PHP variable starts with the $ sign followed by the name of the variable (variable names are case-sensitive).
echo “This is my page.”;

All PHP code goes inside the PHP tags.

<?php // PHP code is placed between the tags. ?>

PHP Comments

PHP Script

// This is a single line comment. It’s for a brief comment.

The multi-line PHP comment begins with  /*  and ends with  */ . It can be used to write multiple line comments.

PHP Script Example

Copyright <?php echo date(‘Y’); ?> Crimson Designs.

This is a simple way to show the dynamic copyright date in your website. Put this code in a PHP file to show the current year. (NOTE: The file you put it in must end in .php)

You can place your PHP code directly into your child theme’s template files (but not in a cascading style sheet file. That is strictly for CSS).

Code Snippets

You may come across code snippets online that do handy things for your WordPress website. You’ll be told to put the code snippet into your functions.php file. (You’ll want to create that file in a child theme and add the code snippet there.)

echo “I’m learning php.”;

If you place the above PHP code in your child theme’s functions.php, it will output the text in every page of your site. All your website visitors will discover that you’re learning PHP. You can be more specific about when and where (on which pages) you want your announcement to be displayed by using PHP functions and hooks. (We’ll discuss the functions.php file in a minute.)

simple PHP script

See: Beginners Guide To PHP in WordPress

PHP Files/Pages

PHP files are similar to HTML files, but PHP files can include both HTML and PHP. A user visiting a web page made in PHP cannot view the PHP code, only the output generated by the code.

When an online visitor accesses a PHP page, the Web browser only gets sent the HTML code. The Web server has already processed the PHP code in the background. In a PHP file, the .php extension is important. It tells the Web server that the page may include PHP code.

PHP compared to HTML

  • PHP is a scripting language; HTML is a markup language.
  • HTML cannot perform computations like 1 + 1 = 2.
  • HTML is very easy and forgiving of mistakes while PHP isn’t.
  • HTML files are static. They are the same every time they are viewed. PHP files are dynamic. The output might not always be the same.

Learn more about HTML and CSS: WordPress under the Hood: Beginner HTML & CSS.

PHP Include Files

PHP allows you to include the contents of one file into another. (This makes designing web pages much easier and more efficient.) PHP functions such as include() or the WordPress function get_template_part() make it possible to include page templates in a web page.

PHP Include File

A full web page on your WordPress site contains code for the header, the footer, the sidebar etc.. It’s like putting the pieces of a puzzle together. These files will be included in the page.

  • the header will be in a file called header.php
  • the sidebar(s) is normally in sidebar.php
  • the footer is normally in footer.php

These are template files.

WordPress template files are like puzzle pieces

What is a function?

A function is simply a set of program statements which perform a specific task. A PHP function can be “called” or executed. You can define functions you wish to re-use in your theme’s template files.

Where exactly is the functions.php file?

A theme’s functions.php file is located in your theme’s folder  in the /wp-content/ folder. (NOTE: It is NOT  in the /wp-includes/ folder. Do not even look in that folder. It should not be touched.)

You can download WordPress to your computer in a ZIP file, then open it to get a look at all the files. Look for the wp-content folder. Open it. Then find the the folder called “themes.” Inside that folder, you will see the folders of some default themes. Each one has a functions.php file inside with functions in it.

PHP Function Example:

function twentyseventeen_setup() {

/* Each function may contain a lot of code. Each function will end with a curly brace. This is a multi-line comment in a function that doesn’t really do anything right now. */


Inside your functions.php file

If you look at a functions.php file, you will notice that it starts with:


The closing PHP tag ?> is optional at the end of the file. So if you don’t see the closing PHP tag at the bottom of the file, that’s okay. If you’re creating a child functions.php page, you will need to type: <?php at the very top before you add any functions or code snippets.

Your functions.php file is not the place to put custom CSS code. This file is for PHP code snippets and functions.

WordPress functions.php file

NOTE: If you’re comfortable modifying your functions.php file, you are capable of creating a simple plugin. The code you paste in your functions.php can most likely be used in a plugin.

Here are some PHP functions that can be added to a functions.php page.

WordPress functions.php Template with 15 Essential Custom Functions

Here’s a tutorial for writing a simple WordPress plugin:

Writing a Simple WordPress Plugin, Beginner Tutorial

You can copy and paste WordPress PHP functions, at first, once you figure out where to put them. With a little effort, you’ll start to recognize them. Then you’ll begin to understand them, and maybe someday write your own.

  • Make sure you wait until a function is ‘over’ before you insert your code. Remember, a closing curly brace } designates the end of a function.
  • Don’t mess with existing functions in a parent theme. Put functions in a child theme.
  • Be prepared. Even a tiny mistake in your functions.php page can break your WordPress site and make it inaccessible.

Adding Custom Code Snippets in WordPress

The Code Snippets plugin is a great way to add custom code to a WordPress site. It gives you a safer way to add chunks of code to the functions.php file. Your code snippets can all be managed from the Manage screen of the plugin. You can easily  activate and deactivate them.

NOTE: ALWAYS back up your WordPress site before making any changes to the code.

Next up: Beginner JavaScript

WordPress Tutorial: Beginner PHP and JavaScript