Learn PHP for WordPress in 30 minutes

Need to learn PHP ASAP? This is a rapid fire class to help you learn PHP in 30 minutes. Obviously we won’t be covering every damned thing, but we will get you up and running with PHP inside of WordPress so that you can get to work writing awesome PHP functionality.

You’ll need a working WordPress install. You’ll need a code editor. You’ll need your WP site to be running either locally or on a server, and if it’s on a server you’ll need FTP to be able to drop files into your WP site. If any of those steps are not done already go research and handle that and then come back.

First thing you’re going to do is setup a page to run PHP in WordPress. The fastest way to do this is to create a custom page template at the root of your active theme. At this point for practice, we don’t care if the theme is a starter theme from WP, a 3rd party theme or a child theme. Just add the following file to the root, page-test.php. Then go into the WP Admin and make a page with the title “Test” and the permalink should be ‘test’. Save your page, then visit it. You should now see your page-test.php, currently empty, loading in the front-end.

Now add an opening tag plus a print statement to do the hello world as shown below. This is saved to your page-test.php:


print "Hello wonderful world";

After refreshing the page you should now see “Hello wonderful world” printed to the page. You now have a working PHP file to practice in. You’ve also learnt that we start PHP blocks with the <?php open tag. Now in a full PHP file, we DO NOT close the PHP tag, we leave it open. However if we need to break out of PHP, usually to write HTML in a mixed PHP/HTML template, then we will use the closing tag ?> as shown below:


$test = 'Test 123';
print $test;


<h1>Now we're out of PHP and into regular HTML</h1>

This capability to move in and out of PHP has made PHP very effective in rendering content in HTML tags. Now let’s learn variables. In PHP the variables as in the code block above start with a dollar sign ($) so $vars and $args are both variables. We use the equals sign (=) to set the value of the variable as shown below:

$test = 'Test 123456';
var_dump( $test );

A handy utility function in PHP is var_dump() which will spit out the value of the variable, array or object that you pass to it. This is very useful in debugging and just seeing what is going on. With a simple variable we could just print the variable to see it’s value, but using var_dump() is more useful when you have an array or object or when you’re just not sure what a variable is, it could for instance be a boolean value like false.

When variables are set to strings we use either single or double quotes around the string, whereas with numbers we can often omit this:

$number = 5;
print $number;
var_dump( $number );

Introducing PHP Arrays

There are 2 syntax options for creating a new array in PHP:

// named array() function
$cats = array('tabitha', 'gerald', 'max');

// short bracket notation []
$cats = ['tabitha', 'gerald', 'max'];

Each array defined above are identical, so it is personal preference in terms of how you setup your arrays. We prefer using short brackets, and when you use most editors you’ll find that this enables you to open the arrow with the open bracket [ and have the close bracket ] placed automatically.

PHP supports multidimensional arrays in addition to simple arrays like those shown above. You can therefore nest arrays inside of arrays, another common usage is to create an array that contains objects or other mixed data such as objects, arrays and/or strings.

$user = [
  'id' => 59,
  'username' => 'timsmith59',
  'profile_image' => [
    'url' => 'https://cdn.amazons3.com/293848392',
    'size' => 'medium',
    'width' => 400,
    'height' => 300

In the example above we have an array of user data. The first two elements are strings and number data, the ID and the username. Then for the profile_image we have a nested array of data. Notice the use of the equals sign followed by the right array => to set the value of array parameters. You’ll trigger an error if you were to incorrectly use only an = sign the way you’re used to when setting up variables.

Learn PHP Functions

As in most programming languages functions are used to organize code into modular pieces. Most of the code you write in PHP will be organized into functions, and within these functions you will likely call other functions. We’ll talk later about object-oriented programming where functions are organized into classes and also where they become known by the OO (Object-Oriented) terminology “methods”. For now just remember that when somebody talks about methods, they mean functions contained within a class structure.

Functions are defined starting with the keyword “function” shown below:

function do_stuff() {
  // this is where stuff is done inside the curly braces {} of the function. 

// time to call our function do_stuff 

Now let’s set a variable and pass it to the function as an argument.

$user_first_name = "Katrina";
show_user_name( $user_first_name );

function show_user_name( $name ) {
  print $name;

You should now see “Katrina” printed to the screen when you run this code. Notice that we named our variable $user_first_name and set the value, then passed it into the show_user_name() function, but inside that function the variable is $name. This is because inside the function you use whatever the argument name is, in this case $name. The function does not care whether a string was passed in or a variable holding a string, and the name of that variable is not relevant to it. The only thing that matters is the function requires 1 argument. If you were to call show_user_name() without the argument, it would create an error for the missing argument.

Next were going to show how functions can have optional arguments.

$user_first_name = "Katrina";
show_user_name( $user_first_name );

function show_user_name( $name_first, $name_last = false ) {
  print $name_first;
  if( $name_last ) {
    print $name_last;


In this example we add $name_last argument as a second and optional argument to the show_user_name function. It has the default set to false, and the way we call it in the example we only pass the first name. This will work fine, and inside the function we use an IF statement (which you’ll learn about more in the next section) to check if $name_last is set. If it is set we’ll print the last name.

Try now calling the argument with both arguments like this:

show_user_name( "Tiger", "Woods" );

You should now see both names printed as “TigerWoods”. In practice we’d want to also create a space or separate the names when we’re printing multiple strings like this.

About the Instructor

Casey Milne

Senior WordPress Developer, Eat/Build/Play

Casey started programming PHP in 2003. His early work involved PHP application development and custom sites built from PHP and HTML. He adopted Drupal in the early days of CMS, and then switched later to WordPress in 2012. For the past 8-years he has developed WordPress plugins and large-scale WP sites. Casey's main forte is handling API integrations and building data-driven functionality in WordPress.