Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Your own theme

A theme is a folder of templates in wp-content/themes. The simplest theme needs just two files. Writing one from scratch is the best way to see how WordPress assembles a page.

Two required files

Create the wp-content/themes/mytheme folder and in it a style.css file with a header comment:

/*
Theme Name: My Theme
Author: Anna Smith
Description: A learning theme.
Version: 1.0.0
Text Domain: mytheme
*/

WordPress learns about the theme from this comment. The second file is index.php:

<?php
// index.php
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
    <h1><?php bloginfo('name'); ?></h1>
 
    <?php wp_footer(); ?>
</body>
</html>

The theme appears under "Appearance → Themes". Activate it — the site shows its title.

These two calls are required. Through them WordPress and plugins insert styles, scripts, the window title and service tags into the page. Without them neither the admin bar at the top of the site nor most plugins work.

Template functions

Site data is printed with ready-made functions:

FunctionWhat it prints
bloginfo('name')The site title
bloginfo('description')The tagline
home_url('/')The home page address (returns rather than prints)
language_attributes()The lang attribute for the html tag
body_class()The classes of the body tag for the current page

The functions.php file

functions.php runs on every load of the site. In it a theme declares its features and adds styles:

// functions.php
 
add_action('after_setup_theme', function () {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', ['search-form', 'gallery', 'caption', 'style', 'script']);
});
 
add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('mytheme-style', get_stylesheet_uri(), [], '1.0.0');
});

add_action() ties a function to a WordPress event — hooks get a lesson of their own.

Theme features

FeatureWhat it gives
title-tagWordPress prints the title tag itself
post-thumbnailsA featured image for a post
custom-logoUploading a logo in the settings
html5Modern markup for forms and galleries
menusMenus configurable in the dashboard

Block themes

Modern WordPress themes can be block themes: instead of PHP templates they have HTML files made of blocks and a theme.json settings file, and they can be edited right in the dashboard. Classic PHP themes still work and are easier to understand — this course is built on them.

Common mistake

The <title> tag and the stylesheet link are not written into a template by hand. A <link rel="stylesheet" href="style.css"> line in index.php breaks on inner pages and gets in the way of plugins. The window title is enabled through title-tag, and styles and scripts are added with wp_enqueue_style() and wp_enqueue_script().

Thanks for reading!

Donate to the site — we work hard for you.