MODULE 03 · FUNCTIONALITY & CUSTOMIZATION

Introduction to PHP for WordPress

You've added functions to your child theme's functions.php without fully knowing what the syntax means. Today you'll learn just enough PHP to read and safely edit the code that powers WordPress underneath.

Day 22 of 30 Intermediate ~20 min

Why WordPress Runs on PHP

WordPress is, at its core, a large PHP application. Every theme, every plugin, every admin screen is PHP code running on the server, building the HTML page that gets sent to a visitor's browser. You don't need to become a PHP developer to run a WordPress site — but knowing the basics turns "mysterious code I'm scared to touch" into something you can read and edit with confidence.

PHP files are mostly regular text, with special tags marking where the PHP code starts and stops. Everything outside those tags is treated as plain HTML.

The Building Blocks

A handful of concepts cover almost everything you'll encounter in WordPress theme and plugin files.

CONCEPT WHAT IT MEANS
Variable

A named container for a value, always starting with a dollar sign, like $title.

Function

A named, reusable block of code that runs when called, like get_the_title().

Conditional

An if statement that runs code only when a condition is true.

Loop

Code that repeats — WordPress's own "The Loop" runs once per post being displayed.

THINK OF IT AS

Variables hold data, functions act on that data, conditionals decide what to show, and loops repeat the process for each item.

A Function From functions.php

functions.php
<?php
function my_custom_greeting() {
    $name = 'Reader';

    if ( is_user_logged_in() ) {
        $name = wp_get_current_user()->display_name;
    }

    echo 'Welcome, ' . $name;
}

A variable holds the default name, a conditional checks if someone is logged in, and the function prints the result.

Editing functions.php Safely

  • Open your child theme's functions.php through the file manager or a code editor — never the parent theme.
  • Scroll to the bottom of the file, after any existing code.
  • Write a new function starting with function, a name, and parentheses.
  • Wrap the logic in curly braces { } and end each line with a semicolon.
  • Hook the function into WordPress using add_action() or add_filter().
  • Save and reload the site — a blank white screen usually means a syntax error to fix.
  • Where Should You Edit PHP?

    THE BUILT-IN THEME EDITOR Editing functions.php inside Appearance → Theme File Editor has no undo — one typo can take the entire site down instantly.
    A CODE EDITOR + SFTP Editing locally in a proper code editor, then uploading via SFTP, gives you version history and a safety net before changes go live.

    Common PHP Mistakes

    WATCH FOR THESE
    MISSING SEMICOLON Forgetting a semicolon at the end of a line is the single most common cause of a fatal error.
    MISMATCHED BRACES Every opening curly brace needs a matching closing one, or the file won't parse correctly.
    DUPLICATE FUNCTION NAMES Two functions sharing the same name anywhere on the site will crash it with a fatal error.

    Breaking Down What You Just Learned

    1

    WordPress is PHP underneathEvery theme, plugin, and admin screen is built from PHP code.

    2

    Four core building blocksVariables, functions, conditionals, and loops cover most WordPress PHP.

    3

    Functions get hooked inadd_action() and add_filter() connect custom functions to WordPress.

    4

    A code editor beats editing liveSFTP with a local editor avoids the built-in editor's lack of undo.

    5

    Small mistakes cause fatal errorsMissing semicolons and mismatched braces are the most common culprits.

    Try It Yourself

    EXERCISE

    Add a function to your child theme's functions.php that changes the site's default excerpt length. Search for the excerpt_length filter and hook a simple function into it.

    Write Your First Custom Function

    HANDS-ON EXERCISE

    Getting comfortable inside functions.php

    By the end of today, your child theme should include one working custom function you wrote yourself.

    1. Set up a local code editor and connect to your site via SFTP.
    2. Open your child theme's functions.php file.
    3. Write a simple function using a variable and a conditional.
    4. Hook it into WordPress with add_action() or add_filter().
    5. Save, upload, and reload the site to confirm nothing broke.

    Recap

    VARIABLE

    A named container for a value, starting with a dollar sign.

    FUNCTION

    A reusable, named block of code that runs when called.

    add_action()

    Hooks a custom function into a specific point in WordPress.

    FATAL ERROR

    A syntax mistake, like a missing semicolon, that can break the whole site.

    Key Takeaways

    • WordPress, its themes, and its plugins are all built from PHP code.
    • Variables, functions, conditionals, and loops cover most of what you'll encounter.
    • Custom functions connect to WordPress through add_action() and add_filter().
    • Editing locally with SFTP is safer than the built-in theme editor, which has no undo.
    • Missing semicolons and mismatched braces are the most common causes of a broken site.
    Course Overview