Skip to content

Latest commit

 

History

History
179 lines (157 loc) · 11.8 KB

File metadata and controls

179 lines (157 loc) · 11.8 KB

Live Coding WordPress Theme from Scratch | 50 min | 2024

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction and Tools for Theme Development

Robert introduces himself as a 13-year WordPress and WooCommerce developer, sharing his approach to building themes from scratch. He covers tools like Local for quick local WordPress setups, Visual Studio Code with GitHub Copilot for AI-assisted coding, and emphasizes structuring themes efficiently.

  • Key Takeaway/Example: Start with a fresh WordPress install in Local, create a theme folder in wp-content/themes, and use VS Code for editing.
  • Link for More Details: Ask AI: WordPress Theme Development Tools

Basic Theme Files and Setup

Every WordPress theme needs core files like index.php for the main content (including get_header() and get_footer()), functions.php for hooks and functions, and style.css for theme metadata like name, version, and description. WordPress reads style.css to display theme info in the admin.

  • Key Takeaway/Example: In style.css, add comments like /* Theme Name: Tutorial */. In index.php, echo simple content between header and footer calls.
<?php get_header(); ?>
<?php echo 'Hello World'; ?>
<?php get_footer(); ?>

Organizing with Includes and Constants

Use an includes folder to separate functionalities into individual files for better structure, avoiding a bloated functions.php. Define constants like THEME_URI (using get_template_directory_uri()) and THEME_DIR (using get_template_directory()) for easy paths to assets.

  • Key Takeaway/Example: In functions.php, add:
define('THEME_URI', get_template_directory_uri());
define('THEME_DIR', get_template_directory());

Then include files like includes/categories.php as needed.

Implementing a Singleton Class for Theme

Create a singleton class in functions.php to manage theme initialization safely, ensuring it's instantiated only once. Use it to run the constructor for hooks like enqueuing styles.

  • Key Takeaway/Example: Example class setup:
class Tutorial_Theme {
    private static $instance = null;
    public static function get_instance() {
        if (self::$instance == null) {
            self::$instance = new Tutorial_Theme();
        }
        return self::$instance;
    }
    private function __construct() {
        // Add hooks here
    }
}
Tutorial_Theme::get_instance();

Header and Footer Templates

Header.php handles the doctype, html, head (with wp_head() for hooks), and opening body. Footer.php includes wp_footer() and closing tags. These override defaults when present.

  • Key Takeaway/Example: In header.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(); ?>>

Integrating Tailwind CSS

Tailwind CSS provides utility classes for efficient styling. Set it up with npm, an input.css file for directives, and scripts in package.json to build/watch styles into style.css, scanning PHP files for classes.

  • Key Takeaway/Example: Run npm install -D tailwindcss, then add to package.json:
"scripts": {
    "build": "tailwindcss -i ./input.css -o ./style.css",
    "watch": "tailwindcss -i ./input.css -o ./style.css --watch"
}

In input.css: @tailwind base; @tailwind components; @tailwind utilities;

Enqueuing Styles and Scripts

Use add_action('wp_enqueue_scripts') in the theme class to load styles via wp_enqueue_style(), referencing THEME_URI for paths.

  • Key Takeaway/Example: In the constructor:
add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
public function enqueue_styles() {
    wp_enqueue_style('tutorial-style', get_stylesheet_uri());
}

Template Hierarchy and Custom Templates

WordPress uses a hierarchy: index.php as fallback, single.php for posts, page.php for pages, archive.php for archives. Customize for post types like archive-product.php.

  • Key Takeaway/Example: In single.php:
<?php get_header(); ?>
<div class="p-20">
    <h1 class="text-2xl font-bold"><?php the_title(); ?></h1>
    <div class="mb-10"><?php the_content(); ?></div>
</div>
<?php get_footer(); ?>

Using Composer for Autoloading Classes

Set up a src folder for classes, use composer.json with PSR-4 autoloading, and require vendor/autoload.php in functions.php to auto-load classes without manual includes.

  • Key Takeaway/Example: In composer.json:
{
    "autoload": {
        "psr-4": {
            "Robbert\\TutorialTheme\\": "src/"
        }
    }
}

Run composer dump-autoload.

Working with Actions and Filters

Actions add functionality (e.g., via add_action()), filters modify values (e.g., add_filter('the_title')). Use in classes or prefixed functions to avoid conflicts.

  • Key Takeaway/Example: Filter example:
add_filter('the_title', array($this, 'change_title'));
public function change_title($title) {
    return $title . ' (Modified)';
}

Best Practices for Code Organization

Prefer classes over global functions to encapsulate code and avoid name conflicts. Prefix functions if not using classes. Structure keeps themes scalable.


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: