Getting Started with WordPress Theme Development: Creating Your First Custom Theme from Scratch

3-minute read
2026-03-17
2026-06-03
2,545
I earn commissions when you shop through the links below, at no additional cost to you.

When you start learning WordPress development, creating your own theme is an exciting milestone. It not only allows you to take complete control over the appearance and functionality of your website but also serves as an excellent way to gain a deeper understanding of the core architecture of WordPress. This article will guide you through the entire process, from setting up your development environment to creating your first basic theme, helping you take the first step towards becoming a WordPress developer.

Preparatory work before development

Before starting to write any code, it is crucial to set up an efficient local development environment. This allows you to test and debug your code without affecting the live website.

Build a local development environment

First of all, we need to install local server software. You can choose tools such as XAMPP, MAMP, or Local by Flywheel. These software packages include Apache server, MySQL database, and PHP, which are essential for running WordPress. Taking XAMPP as an example, after installation, start the Apache and MySQL services, and your computer will become a local server.

Recommended Reading Introduction to WordPress Theme Development: Building Your First Custom Theme from Scratch

Next, download the latest version of WordPress and extract it to the root directory of your local server’s website (for example, the directory where your XAMPP installation is located).htdocsThen, by accessing the folder…http://localhost/your-wordpress-folderTo complete the famous “five-minute installation,” you will need to create a database and set the administrator username and password for the website during the installation process.

UltaHost WordPress Hosting
30-day refund guarantee, unlimited bandwidth and database usage, free DDoS protection; purchase for 3 years and get a discount of 50%.

Prepare the code editor and tools.

An excellent code editor can greatly improve development efficiency. Visual Studio Code, PhpStorm, or Sublime Text are all great options. Especially VS Code, which comes with a wealth of extensions for WordPress and PHP, such as PHP Intelephense, which offers intelligent code completion and syntax checking.

In addition, it is recommended to install a browser developer tool plugin, such as the official “Theme Check” plugin for WordPress, to check whether the theme meets the standards at later stages of development. You can also use the “Inspect Elements” feature of the browser to debug HTML and CSS in real-time.

Understanding the basic structure of a WordPress theme

The simplest WordPress theme only requires two files. However, a fully functional and standard-compliant WordPress theme structure includes multiple specific files, each with its own distinct role and function.

The core style sheet file

Every WordPress theme must have one.style.cssThis file is not only a CSS file that defines the theme’s style, but the comments in the file’s header section also serve as the “identity card” of the theme, containing all the meta-information necessary for WordPress to recognize the theme. A basic header comment is shown below:

Recommended Reading WordPress Theme Development: A Complete Guide to Building a Custom Theme from Scratch

/*
Theme Name: 我的第一个主题
Theme URI: https://example.com/my-first-theme/
Author: 你的名字
Author URI: https://example.com/
Description: 这是一个用于学习的简单自定义WordPress主题。
Version: 1.0
License: GNU General Public License v2 or later
Text Domain: my-first-theme
*/

Among them,Text DomainUsed for internationalization; it is for subsequent calls to the translation function.__()Or_e()The identifier used at that time.

Basic template files

Another required file isindex.phpIt is the default template for the theme. When WordPress cannot find any more specific template files (such as…)single.phpOrpage.phpWhen you call the render method, it will be used to render the page.

With just these two files, your theme can already appear in the “Appearance” -> “Themes” list in the WordPress administration panel and can be activated. Of course, it’s currently just a blank framework. To make the theme actually functional, we still need to create several key template files.

hosting.com Shared Hosting
High performance with AMD EPYC CPUs, NVMe SSD storage and LiteSpeed, 24/7, 24x7 expert in-house support, advanced security measures including SSL, brute force, malware and DDoS protection, savings of up to 73%

First, create it.header.phpIt contains the header of the HTML document, such as…<doctype>Statement,<head>Region (where it should be used)wp_head()The function includes the introduction of WordPress core scripts and styles, as well as the markup for the website’s header.

Secondly, create…footer.phpIt contains the footer content of the website and is called before the end of the process.wp_footer()This is a function; it represents a hook that is essential for many plugins to add code at the bottom of the page.

Finally, create it.functions.phpThis file is not mandatory, but it serves as the “core” of the theme. You can use it to add features that support the theme’s functionality, a registration menu and sidebar, as well as to include style and script files. Additionally, you can define various custom functions within this file.

Recommended Reading How to develop a custom WordPress theme from scratch

The core features of building a theme include:

Once we have the basic files in place, we can start bringing the theme to life and giving it the essential functions of a website.

Implement a navigation menu

Modern websites usually have a navigation menu. First of all, you need to…functions.phpUse it in Chineseregister_nav_menus()A function is used to register the location of the dish unit.

InterServer Shared Hosting
Shared hosting $2.50 USD per month , first month $0.1 USD promo code tryinterserver, 461 cloud apps scripts, one click install.
function my_first_theme_setup() {
    register_nav_menus( array(
        'primary' => __( '主导航菜单', 'my-first-theme' ),
        'footer'  => __( '页脚菜单', 'my-first-theme' ),
    ) );
}
add_action( 'after_setup_theme', 'my_first_theme_setup' );

This code registers two menu locations: the “Main Navigation Menu” and the “Footer Menu”. Then, in the template areas where you wish to display the menus (for example...header.php(In the middle), usewp_nav_menu()The function calls it.

wp_nav_menu( array(
    'theme_location' => 'primary',
    'menu_class'     => 'primary-menu',
) );

Users can now create menus in the “Appearance” -> “Menus” section of the WordPress administration panel, and assign them to the “Main Navigation Menu” location.

Enable featured article images and a sidebar.

Many themes support article thumbnails (feature images) and a sidebar with widgets. The same applies to...functions.phpThemy_first_theme_setupIn the function, add the following code to enable these features:

add_theme_support( 'post-thumbnails' ); // 启用文章特色图像
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) ); // 启用HTML5标记支持

To register a sidebar widget area, useregister_sidebar()Function:

function my_first_theme_widgets_init() {
    register_sidebar( array(
        'name'          =&gt; __( 'Main Sidebar', 'my-first-theme' ),
        'id'            =&gt; 'sidebar-1',
        'description'   =&gt; __( 'Add widgets here.', 'my-first-theme' ),
        'before_widget' =&gt; ' &lt;&#039;<section id="%1$s" class="widget %2$s">',
        'after_widget'  =&gt; '</section>',
        'before_title'  =&gt; '<h2 class="widget-title">',
        'after_title'   =&gt; '</h2>',
    ) );
}
add_action( 'widgets_init', 'my_first_theme_widgets_init' );

After that, in the template file (such assidebar.phpUsed in (…)dynamic_sidebar( 'sidebar-1' )To display this area.

Integrating templates and adding styles

Now, we will connect all the different template parts together and add some basic styling to them, so that the website takes on a visible, tangible form.

Introducing functions using templates

WordPress provides several core functions for including other template files, which ensures the modularity and maintainability of the code. In your…index.phpThe structure should be as follows:

<?php get_header(); ?>

<main id="main" class="site-main">
    <?php
    if ( have_posts() ) :
        while ( have_posts() ) : the_post();
            // 循环输出文章内容
            get_template_part( 'template-parts/content', get_post_type() );
        endwhile;
        the_posts_navigation(); // 文章导航
    else :
        get_template_part( 'template-parts/content', 'none' ); // 没有找到内容时的模板
    endif;
    ?>
</main>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

get_header(), get_sidebar(), get_footer()The functions each import the corresponding template files.get_template_part()It is a more flexible function that encourages you to separate sections of content that can be reused frequently (such as article summaries) into separate components.template-partsIn the individual files within the folder, for example…content.php

Writing basic styles and responsive design

Now, open it.style.cssLet's start adding styles to your theme. First, set some global styles, such as the box model, fonts, and colors.

* {
    box-sizing: border-box;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    line-height: 1.6;
    color: #333;
    margin: 0;
}
.container {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

To achieve responsive design, use media queries to adjust the layout for different screen sizes. For example, make the sidebar move below the main content on smaller screens:

@media (max-width: 768px) {
    .site-main {
        width: 100%;
    }
    #sidebar {
        width: 100%;
        margin-top: 2rem;
    }
}

Don't forget to…header.phpThe<head>Some viewport meta tags have been added to ensure correct scaling on mobile devices:<meta name="viewport" content="width=device-width, initial-scale=1">

summarize

From creating a document that includesstyle.cssandindex.phpStarting from a basic folder structure, you progressed to creating a complete WordPress theme that includes navigation, a sidebar, and a responsive layout. This process covered the core concepts of WordPress theme development. You learned how to…functions.phpExpanding the theme functionality: How to use a template hierarchy system to organize code, and how to convert the design into front-end HTML and CSS. This is just the beginning; you can further explore more advanced template files (such as…).single.php, page.php, archive.phpCustom article types, theme customizers via APIs, and even the use of Sass and JavaScript frameworks to create more complex and modern themes are all available. Continuous practice, along with referring to the official developer documentation, are the best ways to improve your skills.

FAQ Frequently Asked Questions

Do development topics have to use subtopics?

Not necessarily. It’s completely possible to create an independent theme from scratch, and it’s also the best way to learn the core principles. Using sub-threads is generally the best practice when you need to customize and modify an existing parent theme (such as a theme from a popular framework), as it ensures that your custom changes won’t be overwritten when the parent theme is updated. For brand-new development projects, it’s more common to build an independent theme from the beginning.

How can I make my theme support multiple languages?

Making a topic support multiple languages (internationalization) mainly involves two steps. First, make sure that…style.cssThe header and…functions.phpAll strings that need to be translated in this text are wrapped using WordPress’s translation functions, for example:__( ‘文本’, ‘my-first-theme’ )Or_e( ‘文本’, ‘my-first-theme’ )Secondly, you need to use a tool like Poedit to create it..potTranslate the template file, and then generate the corresponding files for each language..poand.moThe file should be placed in the same directory as the theme./languagesUnder the directory.

How do I publish the theme after it has been developed?

Before releasing your theme, be sure to thoroughly test it using the “Theme Check” plugin to ensure that it meets all the coding standards and security requirements of the WordPress theme directory. Remove any debugging code and comments from the theme. You can then choose to submit the theme to the official WordPress.org theme directory (which requires review) or package it in a ZIP file for direct distribution to customers or on your own website. If you plan to distribute the theme yourself, make sure to provide clear installation instructions and documentation.

Why do my custom styles disappear after they have been updated in the backend?

This is likely because you directly modified the files of the parent theme that you are using. When the parent theme is updated through the WordPress administration panel, all core files are replaced with the new version, causing your changes to be lost. This is why it is highly recommended to use a child theme for any custom development. The styles and functions of a child theme are loaded independently from those of the parent theme; therefore, even if the parent theme is updated, your child theme files will retain all your customizations intact.