Skip to main content

Course Progress

Loading...

Theme Testing and Validation

Duration: 45 minutes
Module 5: Session 1

Learning Objectives

  • Master WordPress theme testing methodologies
  • Use Theme Check plugin to validate your theme
  • Implement debugging techniques for theme development
  • Test themes across different browsers and devices
  • Validate theme compliance with WordPress standards
  • Create a comprehensive testing checklist

Introduction

Testing your WordPress theme is like quality control in a factory - it ensures your product works perfectly before it reaches users. A well-tested theme prevents embarrassing bugs, security vulnerabilities, and poor user experiences.

🔍
Testing Philosophy
Good testing catches bugs before users do. Great testing prevents bugs from existing in the first place. Think of testing as an investment in your theme's reputation!

Types of Theme Testing

Professional theme development requires multiple layers of testing:

flowchart TB
    A[Theme Testing] --> B[Functionality Testing]
    A --> C[Visual Testing]
    A --> D[Performance Testing]
    A --> E[Security Testing]
    A --> F[Compatibility Testing]
    
    B --> B1[PHP Errors]
    B --> B2[JavaScript Errors]
    B --> B3[Feature Testing]
    
    C --> C1[Cross-browser]
    C --> C2[Responsive Design]
    C --> C3[Print Styles]
    
    D --> D1[Page Load Speed]
    D --> D2[Database Queries]
    D --> D3[Asset Optimization]
    
    E --> E1[Data Validation]
    E --> E2[Escaping Output]
    E --> E3[Nonce Verification]
    
    F --> F1[WordPress Versions]
    F --> F2[PHP Versions]
    F --> F3[Plugin Conflicts]
    
    style A fill:#e8f5e9,stroke:#4caf50,stroke-width:3px
    style B fill:#e3f2fd,stroke:#2196f3,stroke-width:2px
    style C fill:#fce4ec,stroke:#e91e63,stroke-width:2px
    style D fill:#fff3e0,stroke:#ff9800,stroke-width:2px
    style E fill:#ffebee,stroke:#f44336,stroke-width:2px
    style F fill:#f3e5f5,stroke:#9c27b0,stroke-width:2px
                                    

Using Theme Check Plugin

Theme Check is the official WordPress plugin that tests your theme against WordPress standards. Think of it as your theme's report card!

# Install Theme Check via WP-CLI
wp plugin install theme-check --activate

# Or install through WordPress admin:
# 1. Go to Plugins > Add New
# 2. Search for "Theme Check"
# 3. Install and Activate
✓ PASSED: Theme contains valid screenshot.png ✓ PASSED: Theme contains proper style.css header ✓ PASSED: No PHP errors detected ⚠ WARNING: Text domain not found in style.css ⚠ WARNING: No content width set ✗ REQUIRED: wp_enqueue_script() not found ✗ REQUIRED: add_theme_support( 'automatic-feed-links' ) not found ✗ REQUIRED: comments_template() not found Test Results: 3 Passed | 2 Warnings | 3 Errors

Fixing Common Theme Check Errors

<?php
// functions.php - Fix common Theme Check issues

// REQUIRED: Set content width
if ( ! isset( $content_width ) ) {
    $content_width = 1200;
}

// REQUIRED: Add theme support features
function my_theme_setup() {
    // Automatic feed links
    add_theme_support( 'automatic-feed-links' );
    
    // Title tag support
    add_theme_support( 'title-tag' );
    
    // Post thumbnails
    add_theme_support( 'post-thumbnails' );
    
    // HTML5 support
    add_theme_support( 'html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script',
    ) );
    
    // Custom logo
    add_theme_support( 'custom-logo', array(
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
    ) );
}
add_action( 'after_setup_theme', 'my_theme_setup' );

// REQUIRED: Properly enqueue scripts and styles
function my_theme_scripts() {
    // Enqueue style.css
    wp_enqueue_style( 
        'my-theme-style', 
        get_stylesheet_uri(),
        array(),
        wp_get_theme()->get( 'Version' )
    );
    
    // Enqueue JavaScript
    wp_enqueue_script(
        'my-theme-script',
        get_template_directory_uri() . '/assets/js/main.js',
        array( 'jquery' ),
        wp_get_theme()->get( 'Version' ),
        true
    );
    
    // Comment reply script
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );

Manual Testing Checklist

Automated testing catches code issues, but manual testing catches user experience issues:

📋 Essential Theme Testing Checklist

📱
Responsive Design Testing

Test on mobile (320px), tablet (768px), and desktop (1920px)

📝
Content Testing

Test with no content, minimal content, and lots of content

🖼️
Media Testing

Test images, galleries, videos, and audio embeds

🔗
Navigation Testing

Test menus with 0, 1, 5, and 20+ items

💬
Comments Testing

Test comment display, threading, and pagination

🔍
Search Testing

Test with results, no results, and special characters

Browser Compatibility Testing

Your theme needs to work across different browsers. Each browser is like a different brand of TV - they all show the same content but might display it slightly differently:

🌐
Chrome

90%+ users

🦊
Firefox

Developer favorite

🧭
Safari

Mac/iOS users

🌊
Edge

Windows default

🌍
Opera

Optional

/* Browser-specific CSS fixes */

/* Safari flexbox fix */
.flex-container {
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
}

/* IE11 grid support */
@supports not (display: grid) {
    .grid-container {
        display: flex;
        flex-wrap: wrap;
    }
}

/* Firefox form styling */
@-moz-document url-prefix() {
    input[type="number"] {
        -moz-appearance: textfield;
    }
}

/* Chrome autofill background */
input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 30px white inset;
}

Debugging WordPress Themes

Debugging is detective work - you're finding clues to solve the mystery of why something isn't working:

🔴 Common PHP Error

Fatal error: Uncaught Error: Call to undefined function 
get_theme_mod() in /themes/my-theme/header.php:25

Solution: Check if WordPress functions are available and spelled correctly

⚠️ Common Warning

Warning: Division by zero in 
/themes/my-theme/functions.php on line 142

Solution: Always check for zero before division operations

✅ Debug Helper Functions

<?php
// Debugging helper functions

// Pretty print variable
function debug_print( $var, $label = '' ) {
    if ( WP_DEBUG ) {
        echo '<pre style="background: #f0f0f0; padding: 10px; margin: 10px 0;">';
        if ( $label ) {
            echo '<strong>' . esc_html( $label ) . ':</strong> ';
        }
        print_r( $var );
        echo '</pre>';
    }
}

// Log to debug.log file
function debug_log( $message ) {
    if ( WP_DEBUG_LOG ) {
        if ( is_array( $message ) || is_object( $message ) ) {
            error_log( print_r( $message, true ) );
        } else {
            error_log( $message );
        }
    }
}

// Check which template is being used
function debug_template() {
    if ( WP_DEBUG ) {
        global $template;
        echo '<!-- Template: ' . basename( $template ) . ' -->';
    }
}

// Usage examples:
debug_print( get_theme_mods(), 'Theme Mods' );
debug_log( 'User accessed page: ' . get_the_title() );
debug_template();

Performance Testing

A beautiful theme that loads slowly is like a sports car stuck in traffic - it doesn't matter how good it looks if it can't perform!

Page Speed

Target: Under 3 seconds

  • Optimize images
  • Minify CSS/JS
  • Enable caching
💾

Database Queries

Target: Under 50 queries

  • Use transients
  • Cache results
  • Optimize loops
📦

File Size

Target: Under 2MB total

  • Compress images
  • Remove unused code
  • Use CDN for libraries
<?php
// Performance optimization in functions.php

// Remove unnecessary WordPress features
function optimize_theme_performance() {
    // Remove emoji scripts
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    
    // Remove WordPress version
    remove_action( 'wp_head', 'wp_generator' );
    
    // Remove RSD link
    remove_action( 'wp_head', 'rsd_link' );
    
    // Remove WLW manifest
    remove_action( 'wp_head', 'wlwmanifest_link' );
    
    // Disable XML-RPC
    add_filter( 'xmlrpc_enabled', '__return_false' );
    
    // Limit post revisions
    if ( ! defined( 'WP_POST_REVISIONS' ) ) {
        define( 'WP_POST_REVISIONS', 3 );
    }
}
add_action( 'init', 'optimize_theme_performance' );

// Lazy load images
function add_lazy_loading( $content ) {
    $content = preg_replace( 
        '/<img(.*?)src=/i', 
        '<img$1loading="lazy" src=', 
        $content 
    );
    return $content;
}
add_filter( 'the_content', 'add_lazy_loading' );

Security Validation

Security testing protects your users from attacks. It's like installing locks, alarms, and cameras in your theme:

flowchart LR
    A[User Input] --> B{Validate}
    B -->|Valid| C[Sanitize]
    B -->|Invalid| D[Reject]
    C --> E[Escape Output]
    E --> F[Display to User]
    D --> G[Error Message]
    
    style A fill:#ffebee,stroke:#f44336,stroke-width:2px
    style B fill:#fff3e0,stroke:#ff9800,stroke-width:2px
    style C fill:#e8f5e9,stroke:#4caf50,stroke-width:2px
    style E fill:#e3f2fd,stroke:#2196f3,stroke-width:2px
    style F fill:#f0f4c3,stroke:#827717,stroke-width:2px
                                    
<?php
// Security best practices

// 1. Always escape output
echo esc_html( $user_input );
echo esc_attr( $attribute );
echo esc_url( $url );
echo wp_kses_post( $content_with_html );

// 2. Sanitize input
$clean_email = sanitize_email( $_POST['email'] );
$clean_text = sanitize_text_field( $_POST['name'] );
$clean_textarea = sanitize_textarea_field( $_POST['message'] );

// 3. Validate data
if ( ! is_email( $email ) ) {
    wp_die( 'Invalid email address' );
}

// 4. Use nonces for forms
// In form:
wp_nonce_field( 'my_action', 'my_nonce' );

// In processing:
if ( ! isset( $_POST['my_nonce'] ) || 
     ! wp_verify_nonce( $_POST['my_nonce'], 'my_action' ) ) {
    wp_die( 'Security check failed' );
}

// 5. Check user capabilities
if ( ! current_user_can( 'edit_posts' ) ) {
    wp_die( 'You do not have permission to do this' );
}

Testing with Theme Unit Test Data

WordPress provides test data that includes edge cases and unusual content. It's like a stress test for your theme:

# Download WordPress Theme Unit Test Data
curl -O https://raw.githubusercontent.com/WPTT/theme-unit-test/master/themeunittestdata.wordpress.xml

# Import via WordPress Admin:
# 1. Tools > Import
# 2. Install WordPress Importer
# 3. Upload the XML file
# 4. Import users and attachments

What Theme Unit Test Data Tests

  • Edge Cases: Very long titles, no title, special characters
  • Content Types: Posts, pages, attachments, custom post types
  • Formatting: Lists, quotes, code blocks, alignments
  • Media: Images with various alignments and captions
  • Comments: Nested comments, pingbacks, trackbacks
  • Users: Multiple authors with different roles
  • Categories/Tags: Hierarchical categories, many tags
  • Menus: Complex navigation structures

Accessibility Testing

Accessibility ensures everyone can use your theme, regardless of their abilities:

<!-- Accessibility best practices -->

<!-- 1. Semantic HTML -->
<nav role="navigation" aria-label="Main navigation">
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a></li>
    </ul>
</nav>

<!-- 2. Skip links -->
<a class="screen-reader-text" href="#content">Skip to content</a>

<!-- 3. ARIA labels -->
<button aria-label="Open menu" aria-expanded="false">
    <span class="hamburger"></span>
</button>

<!-- 4. Form labels -->
<label for="search">Search:</label>
<input type="search" id="search" name="s">

<!-- 5. Alt text for images -->
<img src="logo.png" alt="Company Name Logo">
Accessibility Tools
  • WAVE: Web accessibility evaluation tool
  • axe DevTools: Browser extension for testing
  • Screen readers: NVDA (Windows) or VoiceOver (Mac)
  • Keyboard navigation: Test using only Tab, Enter, and Arrow keys

Practice Exercise

Let's validate and test your theme:

💻
Testing Challenge
  1. Install and run Theme Check on your theme
  2. Fix at least 3 errors reported by Theme Check
  3. Test your theme with WordPress Debug mode enabled
  4. Check your theme in 3 different browsers
  5. Test responsive design at 320px, 768px, and 1920px
  6. Import Theme Unit Test data and check all pages
  7. Run your site through Google PageSpeed Insights
  8. Validate your HTML and CSS using W3C validators
<?php
// Add this test function to your theme for debugging
function theme_debug_info() {
    if ( ! WP_DEBUG ) {
        return;
    }
    
    echo '<div style="background: #f0f0f0; padding: 20px; margin: 20px 0;">';
    echo '<h3>Theme Debug Information</h3>';
    echo '<p>Theme Name: ' . wp_get_theme()->get( 'Name' ) . '</p>';
    echo '<p>Theme Version: ' . wp_get_theme()->get( 'Version' ) . '</p>';
    echo '<p>WordPress Version: ' . get_bloginfo( 'version' ) . '</p>';
    echo '<p>PHP Version: ' . phpversion() . '</p>';
    echo '<p>Active Plugins: ' . count( get_option( 'active_plugins' ) ) . '</p>';
    echo '<p>Current Template: ' . get_page_template_slug() . '</p>';
    
    global $wp_query;
    echo '<p>Query Vars: </p>';
    echo '<pre>' . print_r( $wp_query->query_vars, true ) . '</pre>';
    echo '</div>';
}
// Add to footer during development
add_action( 'wp_footer', 'theme_debug_info' );

Practice Assignment

Create a comprehensive testing report for your theme:

  • Run Theme Check and document all errors, warnings, and recommendations
  • Create a spreadsheet with test results for 5 different browsers
  • Test your theme with 0, 1, 10, and 100 posts
  • Document load time before and after optimization
  • Test all WordPress default widgets in your sidebar
  • Validate accessibility using WAVE or axe DevTools
  • Create a bug report for any issues found
  • Write a test plan for future theme updates

Additional Resources