functions.php
๐ฆ WordPress
โจ The Prompt Phrase
functions.php
๐ป Code Preview
๐ฆ All-in-One Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>functions.php - Interactive Tutorial</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700;800&family=Fira+Code:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--bg-primary: #0a0e27;
--bg-secondary: #151a35;
--bg-card: #1e2542;
--accent-purple: #a855f7;
--accent-blue: #3b82f6;
--accent-green: #10b981;
--accent-cyan: #06b6d4;
--accent-pink: #ec4899;
--accent-orange: #f59e0b;
--text-primary: #f0f6fc;
--text-secondary: #c9d1d9;
--text-muted: #8b949e;
--border-color: #30363d;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
body {
font-family: 'Poppins', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
line-height: 1.6;
overflow-x: hidden;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
/* Hero Section */
.hero {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
position: relative;
overflow: hidden;
}
.hero::before {
content: '';
position: absolute;
width: 200%;
height: 200%;
background: repeating-linear-gradient(45deg, transparent, transparent 10px, rgba(255,255,255,0.03) 10px, rgba(255,255,255,0.03) 20px);
animation: moveBackground 20s linear infinite;
}
@keyframes moveBackground {
0% { transform: translate(0, 0); }
100% { transform: translate(50px, 50px); }
}
.hero-content {
text-align: center;
z-index: 1;
animation: fadeInUp 1s ease;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.hero h1 {
font-size: 4rem;
font-weight: 800;
margin-bottom: 20px;
text-shadow: 0 0 40px rgba(0,0,0,0.3);
animation: pulse 2s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.hero .code-highlight {
background: rgba(0,0,0,0.5);
padding: 20px 50px;
border-radius: 20px;
font-family: 'Fira Code', monospace;
display: inline-block;
backdrop-filter: blur(10px);
border: 3px solid rgba(255,255,255,0.3);
font-size: 2.5rem;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
margin: 20px 0;
}
.hero p {
font-size: 1.5rem;
margin-top: 20px;
opacity: 0.95;
font-weight: 300;
}
.scroll-indicator {
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
animation: bounce 2s infinite;
font-size: 2.5rem;
cursor: pointer;
filter: drop-shadow(0 0 10px rgba(255,255,255,0.5));
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% { transform: translateX(-50%) translateY(0); }
40% { transform: translateX(-50%) translateY(-15px); }
60% { transform: translateX(-50%) translateY(-7px); }
}
/* Progress Bar */
.progress-bar {
position: fixed;
top: 0;
left: 0;
height: 5px;
background: linear-gradient(90deg, var(--accent-purple), var(--accent-blue), var(--accent-green));
z-index: 1000;
transition: width 0.3s ease;
box-shadow: 0 0 20px rgba(168, 85, 247, 0.6);
}
/* Section Styles */
section {
padding: 80px 0;
opacity: 0;
transform: translateY(30px);
transition: all 0.6s ease;
}
section.visible {
opacity: 1;
transform: translateY(0);
}
.section-header {
text-align: center;
margin-bottom: 60px;
}
.section-header h2 {
font-size: 3rem;
background: linear-gradient(135deg, var(--accent-purple), var(--accent-cyan));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 15px;
font-weight: 800;
}
.section-header p {
color: var(--text-secondary);
font-size: 1.2rem;
}
/* Card Styles */
.card {
background: var(--bg-card);
border-radius: 20px;
padding: 40px;
margin-bottom: 30px;
border: 1px solid var(--border-color);
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 4px;
background: linear-gradient(90deg, var(--accent-purple), var(--accent-cyan));
transform: scaleX(0);
transition: transform 0.3s ease;
}
.card:hover::before {
transform: scaleX(1);
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
border-color: var(--accent-purple);
}
.card h3 {
font-size: 1.8rem;
margin-bottom: 20px;
color: var(--accent-purple);
font-weight: 700;
}
.card h4 {
font-size: 1.3rem;
margin-bottom: 10px;
color: var(--accent-cyan);
font-weight: 600;
}
.card ul {
margin-left: 20px;
margin-top: 10px;
}
.card li {
margin-bottom: 10px;
line-height: 1.8;
}
/* Code Block */
.code-block {
background: #0d1117;
border-radius: 15px;
padding: 25px;
margin: 20px 0;
position: relative;
font-family: 'Fira Code', monospace;
overflow-x: auto;
border: 1px solid var(--border-color);
box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}
.code-block .copy-btn {
position: absolute;
top: 15px;
right: 15px;
background: var(--accent-purple);
color: white;
border: none;
padding: 8px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 0.9rem;
transition: all 0.3s ease;
font-family: 'Poppins', sans-serif;
font-weight: 600;
}
.code-block .copy-btn:hover {
background: var(--accent-pink);
transform: scale(1.05);
}
.code-block pre {
margin: 0;
color: #e6edf3;
font-size: 0.95rem;
line-height: 1.6;
}
/* Grid Layout */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 30px;
margin: 40px 0;
}
.grid-item {
background: var(--bg-card);
padding: 30px;
border-radius: 15px;
border: 1px solid var(--border-color);
transition: all 0.3s ease;
}
.grid-item:hover {
border-color: var(--accent-purple);
transform: translateY(-5px);
box-shadow: 0 10px 30px rgba(168, 85, 247, 0.3);
}
.grid-item .icon {
font-size: 3rem;
margin-bottom: 15px;
display: block;
}
/* Tabs */
.tabs {
display: flex;
gap: 10px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.tab-btn {
background: var(--bg-secondary);
color: var(--text-secondary);
border: 1px solid var(--border-color);
padding: 12px 24px;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s ease;
font-size: 1rem;
font-family: 'Poppins', sans-serif;
font-weight: 600;
}
.tab-btn:hover {
border-color: var(--accent-purple);
transform: translateY(-2px);
}
.tab-btn.active {
background: linear-gradient(135deg, var(--accent-purple), var(--accent-cyan));
color: white;
border-color: transparent;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
animation: fadeIn 0.5s ease;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* Quiz Styles */
.quiz-question {
background: var(--bg-card);
padding: 30px;
border-radius: 15px;
margin-bottom: 20px;
border: 1px solid var(--border-color);
}
.quiz-options {
display: flex;
flex-direction: column;
gap: 15px;
margin-top: 20px;
}
.quiz-option {
background: var(--bg-secondary);
padding: 15px 20px;
border-radius: 10px;
border: 2px solid var(--border-color);
cursor: pointer;
transition: all 0.3s ease;
}
.quiz-option:hover {
border-color: var(--accent-purple);
transform: translateX(5px);
}
.quiz-option.correct {
border-color: var(--accent-green);
background: rgba(16, 185, 129, 0.1);
}
.quiz-option.incorrect {
border-color: #ef4444;
background: rgba(239, 68, 68, 0.1);
}
.quiz-feedback {
margin-top: 20px;
padding: 15px;
border-radius: 10px;
display: none;
}
.quiz-feedback.show {
display: block;
animation: fadeIn 0.5s ease;
}
.quiz-feedback.correct {
background: rgba(16, 185, 129, 0.1);
border: 1px solid var(--accent-green);
color: var(--accent-green);
}
.quiz-feedback.incorrect {
background: rgba(239, 68, 68, 0.1);
border: 1px solid #ef4444;
color: #ef4444;
}
/* Summary Card */
.summary-card {
background: linear-gradient(135deg, rgba(168, 85, 247, 0.1), rgba(6, 182, 212, 0.1));
border: 2px solid var(--accent-purple);
border-radius: 20px;
padding: 40px;
margin: 40px 0;
}
.summary-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 30px;
}
.summary-item {
background: var(--bg-card);
padding: 20px;
border-radius: 10px;
border-left: 4px solid var(--accent-purple);
}
.summary-item strong {
color: var(--accent-purple);
display: block;
margin-bottom: 10px;
font-weight: 700;
}
/* Footer */
footer {
background: var(--bg-secondary);
padding: 40px 20px;
text-align: center;
border-top: 1px solid var(--border-color);
}
footer p {
color: var(--text-muted);
}
.badge {
display: inline-block;
background: linear-gradient(135deg, var(--accent-purple), var(--accent-cyan));
padding: 8px 16px;
border-radius: 20px;
font-size: 0.9rem;
margin: 5px;
font-weight: 600;
}
/* Warning/Info Boxes */
.warning-box {
background: rgba(239, 68, 68, 0.1);
border-left: 4px solid #ef4444;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.success-box {
background: rgba(16, 185, 129, 0.1);
border-left: 4px solid var(--accent-green);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.info-box {
background: rgba(59, 130, 246, 0.1);
border-left: 4px solid var(--accent-blue);
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
/* Confetti */
.confetti {
position: fixed;
width: 10px;
height: 10px;
animation: confetti-fall 3s linear forwards;
pointer-events: none;
}
@keyframes confetti-fall {
to {
transform: translateY(100vh) rotate(360deg);
opacity: 0;
}
}
/* Demo Box */
.demo-box {
background: var(--bg-secondary);
border: 2px solid var(--accent-purple);
border-radius: 15px;
padding: 30px;
margin: 20px 0;
}
.demo-button {
background: linear-gradient(135deg, var(--accent-purple), var(--accent-cyan));
color: white;
border: none;
padding: 12px 30px;
border-radius: 10px;
cursor: pointer;
font-size: 1rem;
font-weight: 600;
transition: all 0.3s ease;
margin: 10px;
}
.demo-button:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(168, 85, 247, 0.3);
}
.demo-output {
background: #0d1117;
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 20px;
margin-top: 20px;
min-height: 100px;
font-family: 'Fira Code', monospace;
}
#quiz-complete {
text-align: center;
padding: 40px;
background: linear-gradient(135deg, rgba(168, 85, 247, 0.2), rgba(6, 182, 212, 0.2));
border-radius: 20px;
border: 2px solid var(--accent-purple);
margin-top: 30px;
display: none;
}
/* Responsive */
@media (max-width: 768px) {
.hero h1 {
font-size: 2.5rem;
}
.hero .code-highlight {
font-size: 1.5rem;
padding: 15px 30px;
}
.hero p {
font-size: 1.2rem;
}
.section-header h2 {
font-size: 2rem;
}
.grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="progress-bar" id="progressBar"></div>
<!-- Hero Section -->
<section class="hero" id="hero">
<div class="hero-content">
<h1>โ๏ธ WordPress Theme Functions</h1>
<div class="code-highlight">functions.php</div>
<p>Your Theme's Superpower File</p>
</div>
<div class="scroll-indicator" onclick="document.getElementById('what-is-it').scrollIntoView({behavior: 'smooth'})">
โฌ๏ธ
</div>
</section>
<!-- What Is It Section -->
<section id="what-is-it">
<div class="container">
<div class="section-header">
<h2>๐ค What Is functions.php?</h2>
<p>The powerhouse that extends your theme's capabilities</p>
</div>
<div class="card">
<h3>๐ The Simple Definition</h3>
<p><strong>functions.php</strong> is like your theme's control centerโa special PHP file where you add custom code to extend WordPress functionality. It acts like a plugin but specifically for your theme, letting you add features, modify behavior, and customize WordPress to your needs!</p>
<div class="info-box">
<strong>๐ฏ Think of it like this:</strong>
<p>Imagine WordPress is a smartphone. It comes with basic features (calling, texting). But <code>functions.php</code> is like the app store where you install new capabilitiesโcamera filters, games, productivity tools. It's where you add superpowers to your theme without touching WordPress core!</p>
</div>
<div class="grid">
<div class="grid-item">
<span class="icon">โ๏ธ</span>
<h4>What It Does</h4>
<p>Adds custom functionality to your theme</p>
</div>
<div class="grid-item">
<span class="icon">๐</span>
<h4>Acts Like</h4>
<p>A plugin, but theme-specific</p>
</div>
<div class="grid-item">
<span class="icon">๐</span>
<h4>Location</h4>
<p>wp-content/themes/your-theme/</p>
</div>
</div>
</div>
<div class="card">
<h3>๐ฏ Optional But Powerful</h3>
<div class="success-box">
<strong>โ
Key Facts:</strong>
<ul>
<li><strong>Not Required:</strong> Unlike style.css and index.php, it's optional</li>
<li><strong>Highly Recommended:</strong> Almost every theme uses it</li>
<li><strong>Loads Automatically:</strong> WordPress includes it automatically if present</li>
<li><strong>Runs Early:</strong> Loads before any template files</li>
<li><strong>Theme-Specific:</strong> Only active when theme is active</li>
</ul>
</div>
</div>
</div>
</section>
<!-- Why Use It Section -->
<section id="why-use-it">
<div class="container">
<div class="section-header">
<h2>๐ก Why Use functions.php?</h2>
<p>Unlock unlimited possibilities!</p>
</div>
<div class="grid">
<div class="card">
<h3>๐จ Theme Features</h3>
<p>Enable post thumbnails, custom menus, widgets, and other WordPress features for your theme!</p>
</div>
<div class="card">
<h3>๐ง Custom Functions</h3>
<p>Create reusable PHP functions that you can call throughout your theme templates!</p>
</div>
<div class="card">
<h3>๐ฏ Modify Behavior</h3>
<p>Change how WordPress works using hooks (actions and filters) without editing core!</p>
</div>
<div class="card">
<h3>๐ฆ Enqueue Assets</h3>
<p>Properly load CSS and JavaScript files following WordPress best practices!</p>
</div>
<div class="card">
<h3>๐ Plugin-Like Features</h3>
<p>Add functionality without creating a separate pluginโperfect for theme-specific code!</p>
</div>
<div class="card">
<h3>๐ช Custom Post Types</h3>
<p>Register custom post types, taxonomies, and sidebars for advanced content management!</p>
</div>
</div>
</div>
</section>
<!-- How It Works Section -->
<section id="how-it-works">
<div class="container">
<div class="section-header">
<h2>โ๏ธ How Does It Work?</h2>
<p>Understanding the structure and common uses</p>
</div>
<div class="card">
<h3>๐ Basic functions.php Structure</h3>
<div class="code-block">
<button class="copy-btn" onclick="copyCode(this)">๐ Copy</button>
<pre><code><?php
/**
* Theme Functions
*
* @package Your_Theme_Name
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Theme Setup
*/
function mytheme_setup() {
// Add theme support for various features
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'custom-logo' );
add_theme_support( 'html5', array( 'search-form', 'comment-form' ) );
// Register navigation menus
register_nav_menus( array(
'primary' => __( 'Primary Menu', 'mytheme' ),
'footer' => __( 'Footer Menu', 'mytheme' ),
) );
// Set content width
$GLOBALS['content_width'] = 1200;
}
add_action( 'after_setup_theme', 'mytheme_setup' );
/**
* Enqueue Styles and Scripts
*/
function mytheme_enqueue_assets() {
// Enqueue main stylesheet
wp_enqueue_style(
'mytheme-style',
get_stylesheet_uri(),
array(),
'1.0.0'
);
// Enqueue custom JavaScript
wp_enqueue_script(
'mytheme-script',
get_template_directory_uri() . '/js/main.js',
array( 'jquery' ),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_assets' );
/**
* Register Widget Areas
*/
function mytheme_widgets_init() {
register_sidebar( array(
'name' => __( 'Sidebar', 'mytheme' ),
'id' => 'sidebar-1',
'description' => __( 'Main sidebar widget area', 'mytheme' ),
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'mytheme_widgets_init' );
/**
* Custom Excerpt Length
*/
function mytheme_excerpt_length( $length ) {
return 30; // words
}
add_filter( 'excerpt_length', 'mytheme_excerpt_length' );
/**
* Custom Function Example
*/
function mytheme_get_reading_time( $post_id ) {
$content = get_post_field( 'post_content', $post_id );
$word_count = str_word_count( strip_tags( $content ) );
$reading_time = ceil( $word_count / 200 ); // 200 words per minute
return $reading_time . ' min read';
}</code></pre>
</div>
<div class="success-box">
<strong>โ
Key Components:</strong>
<ul>
<li><strong>Theme Setup:</strong> Enable WordPress features</li>
<li><strong>Enqueue Assets:</strong> Load CSS/JS properly</li>
<li><strong>Register Areas:</strong> Sidebars, menus, widgets</li>
<li><strong>Hooks:</strong> Actions and filters to modify behavior</li>
<li><strong>Custom Functions:</strong> Reusable code snippets</li>
</ul>
</div>
</div>
<div class="card">
<h3>๐ฏ Common Use Cases</h3>
<div class="demo-box">
<h4 style="margin-bottom: 20px;">Click to see examples:</h4>
<button class="demo-button" onclick="showUseCase('features')">๐จ Theme Features</button>
<button class="demo-button" onclick="showUseCase('menus')">๐งญ Navigation Menus</button>
<button class="demo-button" onclick="showUseCase('widgets')">๐ฆ Widget Areas</button>
<button class="demo-button" onclick="showUseCase('custom')">โก Custom Functions</button>
<div class="demo-output" id="useCaseOutput">
<p style="color: var(--text-muted);">๐ Click a button to explore!</p>
</div>
</div>
</div>
</div>
</section>
<!-- Live Demo Section -->
<section id="live-demo">
<div class="container">
<div class="section-header">
<h2>๐ฎ Interactive Examples</h2>
<p>Real-world functions.php code!</p>
</div>
<div class="card">
<div class="tabs">
<button class="tab-btn active" onclick="switchTab(event, 'basic')">Basic Setup</button>
<button class="tab-btn" onclick="switchTab(event, 'intermediate')">Intermediate</button>
<button class="tab-btn" onclick="switchTab(event, 'advanced')">Advanced</button>
</div>
<div id="basic" class="tab-content active">
<h4>๐ฏ Basic functions.php (Starter)</h4>
<div class="code-block">
<button class="copy-btn" onclick="copyCode(this)">๐ Copy</button>
<pre><code><?php
// Basic Theme Setup
function mytheme_setup() {
// Enable post thumbnails
add_theme_support( 'post-thumbnails' );
// Enable title tag
add_theme_support( 'title-tag' );
// Register primary menu
register_nav_menu( 'primary', 'Primary Menu' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );
// Enqueue stylesheet
function mytheme_styles() {
wp_enqueue_style( 'main-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'mytheme_styles' );</code></pre>
</div>
<div class="success-box">
<strong>โ
Perfect for:</strong> Simple blogs, learning WordPress theme development
</div>
</div>
<div id="intermediate" class="tab-content">
<h4>๐ Intermediate functions.php (Production)</h4>
<div class="code-block">
<button class="copy-btn" onclick="copyCode(this)">๐ Copy</button>
<pre><code><?php
// Theme Setup
function mytheme_setup() {
add_theme_support( 'post-thumbnails' );
add_theme_support( 'title-tag' );
add_theme_support( 'custom-logo' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'gallery' ) );
// Image sizes
add_image_size( 'hero', 1920, 600, true );
add_image_size( 'thumbnail-large', 800, 600, true );
// Menus
register_nav_menus( array(
'primary' => 'Primary Menu',
'footer' => 'Footer Menu',
) );
}
add_action( 'after_setup_theme', 'mytheme_setup' );
// Enqueue assets
function mytheme_enqueue_assets() {
// Styles
wp_enqueue_style( 'mytheme-style', get_stylesheet_uri(), array(), '1.0.0' );
wp_enqueue_style( 'mytheme-custom', get_template_directory_uri() . '/css/custom.css', array(), '1.0.0' );
// Scripts
wp_enqueue_script( 'mytheme-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_assets' );
// Register sidebar
function mytheme_widgets_init() {
register_sidebar( array(
'name' => 'Sidebar',
'id' => 'sidebar-1',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'mytheme_widgets_init' );
// Custom excerpt length
function mytheme_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'mytheme_excerpt_length' );</code></pre>
</div>
</div>
<div id="advanced" class="tab-content">
<h4>๐ Advanced functions.php (Professional)</h4>
<div class="code-block">
<button class="copy-btn" onclick="copyCode(this)">๐ Copy</button>
<pre><code><?php
// Custom Post Type: Portfolio
function mytheme_register_portfolio_cpt() {
$args = array(
'labels' => array(
'name' => 'Portfolio',
'singular_name' => 'Portfolio Item',
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-portfolio',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'rewrite' => array( 'slug' => 'portfolio' ),
);
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'mytheme_register_portfolio_cpt' );
// Custom Taxonomy: Project Type
function mytheme_register_project_taxonomy() {
$args = array(
'labels' => array(
'name' => 'Project Types',
'singular_name' => 'Project Type',
),
'hierarchical' => true,
'public' => true,
'rewrite' => array( 'slug' => 'project-type' ),
);
register_taxonomy( 'project_type', array( 'portfolio' ), $args );
}
add_action( 'init', 'mytheme_register_project_taxonomy' );
// AJAX Load More Posts
function mytheme_load_more_posts() {
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'template-parts/content', 'excerpt' );
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_load_more_posts', 'mytheme_load_more_posts' );
add_action( 'wp_ajax_nopriv_load_more_posts', 'mytheme_load_more_posts' );
// Custom Admin Columns
function mytheme_custom_post_columns( $columns ) {
$columns['featured_image'] = 'Featured Image';
$columns['word_count'] = 'Word Count';
return $columns;
}
add_filter( 'manage_post_posts_columns', 'mytheme_custom_post_columns' );
function mytheme_custom_post_column_content( $column, $post_id ) {
if ( $column === 'featured_image' ) {
echo get_the_post_thumbnail( $post_id, array( 50, 50 ) );
}
if ( $column === 'word_count' ) {
$content = get_post_field( 'post_content', $post_id );
echo str_word_count( strip_tags( $content ) ) . ' words';
}
}
add_action( 'manage_post_posts_custom_column', 'mytheme_custom_post_column_content', 10, 2 );</code></pre>
</div>
</div>
</div>
</div>
</section>
<!-- Common Mistakes Section -->
<section id="common-mistakes">
<div class="container">
<div class="section-header">
<h2>โ ๏ธ Common Mistakes</h2>
<p>Avoid these functions.php pitfalls!</p>
</div>
<div class="card">
<div class="warning-box">
<h3>โ Mistake #1: Closing PHP Tag</h3>
<p><strong>Never</strong> close the PHP tag at the end of functions.php!</p>
<div class="code-block">
<pre><code>// WRONG โ
<?php
function mytheme_setup() {
// code
}
?> <-- DON'T DO THIS!
// CORRECT โ
<?php
function mytheme_setup() {
// code
}
// No closing tag!</code></pre>
</div>
<p style="margin-top: 10px;"><strong>Why?</strong> Extra whitespace after ?> can cause "headers already sent" errors!</p>
</div>
<div class="warning-box">
<h3>โ Mistake #2: Direct File Loading</h3>
<div class="code-block">
<pre><code>// WRONG โ
<link rel="stylesheet" href="style.css">
// CORRECT โ
function mytheme_enqueue_styles() {
wp_enqueue_style( 'mytheme-style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' );</code></pre>
</div>
<p style="margin-top: 10px;">Always use <code>wp_enqueue_style()</code> and <code>wp_enqueue_script()</code>!</p>
</div>
<div class="warning-box">
<h3>โ Mistake #3: Generic Function Names</h3>
<div class="code-block">
<pre><code>// WRONG โ - Can conflict with plugins
function setup() {
// code
}
// CORRECT โ
- Use unique prefix
function mytheme_setup() {
// code
}</code></pre>
</div>
<p style="margin-top: 10px;">Always prefix your functions with your theme name!</p>
</div>
<div class="warning-box">
<h3>โ Mistake #4: Not Checking if Function Exists</h3>
<div class="code-block">
<pre><code>// BETTER โ
if ( ! function_exists( 'mytheme_setup' ) ) {
function mytheme_setup() {
// code
}
}</code></pre>
</div>
<p style="margin-top: 10px;">This prevents conflicts with child themes!</p>
</div>
<div class="warning-box">
<h3>โ Mistake #5: Syntax Errors</h3>
<p>One syntax error in functions.php breaks your entire site!</p>
<ul>
<li>โ
Always test in a development environment first</li>
<li>โ
Use a code editor with syntax highlighting</li>
<li>โ
Keep a backup before making changes</li>
<li>โ
Use FTP access to fix errors if site breaks</li>
</ul>
</div>
</div>
</div>
</section>
<!-- Pro Tips Section -->
<section id="pro-tips">
<div class="container">
<div class="section-header">
<h2>๐ Pro Tips</h2>
<p>Master functions.php like a pro!</p>
</div>
<div class="grid">
<div class="card">
<h3>๐ก Tip #1: Organize with Comments</h3>
<div class="code-block">
<pre><code>/**
* ================
* THEME SETUP
* ================
*/
// Your code here</code></pre>
</div>
</div>
<div class="card">
<h3>๐จ Tip #2: Use Child Themes</h3>
<p>For customizations, create a child theme with its own functions.php instead of modifying parent theme!</p>
</div>
<div class="card">
<h3>๐ Tip #3: Check Function Existence</h3>
<div class="code-block">
<pre><code>if ( ! function_exists( 'mytheme_setup' ) ) {
function mytheme_setup() {
// code
}
}</code></pre>
</div>
</div>
<div class="card">
<h3>โก Tip #4: Use Hooks Properly</h3>
<p>Always use appropriate hooks (actions/filters) instead of direct execution!</p>
</div>
<div class="card">
<h3>๐ง Tip #5: Separate Large Code</h3>
<p>For big projects, split code into separate files and include them in functions.php!</p>
</div>
<div class="card">
<h3>๐ฑ Tip #6: Test Thoroughly</h3>
<p>Always test changes in a staging environment before deploying to production!</p>
</div>
</div>
</div>
</section>
<!-- Quiz Section -->
<section id="quiz">
<div class="container">
<div class="section-header">
<h2>๐ฏ Test Your Knowledge!</h2>
<p>Time to prove you're a functions.php master!</p>
</div>
<div class="quiz-question">
<h3>Question 1: Is functions.php required in a WordPress theme?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
A) Yes, absolutely required
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 1)">
B) No, but highly recommended
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
C) Only for complex themes
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 1)">
D) Only for child themes
</div>
</div>
<div class="quiz-feedback" id="feedback1"></div>
</div>
<div class="quiz-question">
<h3>Question 2: Should you close the PHP tag (?>) at the end of functions.php?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
A) Yes, always close PHP tags
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 2)">
B) No, leave it open to avoid errors
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
C) It doesn't matter
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 2)">
D) Only close it in child themes
</div>
</div>
<div class="quiz-feedback" id="feedback2"></div>
</div>
<div class="quiz-question">
<h3>Question 3: What's the correct way to add CSS to your theme?</h3>
<div class="quiz-options">
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
A) Add <link> tag in header.php
</div>
<div class="quiz-option" onclick="checkAnswer(this, true, 3)">
B) Use wp_enqueue_style() in functions.php
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
C) Put CSS directly in functions.php
</div>
<div class="quiz-option" onclick="checkAnswer(this, false, 3)">
D) Use @import in style.css
</div>
</div>
<div class="quiz-feedback" id="feedback3"></div>
</div>
<div id="quiz-complete">
<h2 style="font-size: 3rem;">๐ Congratulations!</h2>
<p style="font-size: 1.5rem; margin-top: 20px;">You're now a functions.php expert!</p>
<div class="badge">WordPress Master</div>
<div class="badge">Theme Developer</div>
<div class="badge">PHP Pro</div>
</div>
</div>
</section>
<!-- Summary Section -->
<section id="summary">
<div class="container">
<div class="section-header">
<h2>๐ Quick Reference</h2>
<p>Your functions.php cheat sheet</p>
</div>
<div class="summary-card">
<h3>๐ฏ functions.php Essentials</h3>
<div class="summary-grid">
<div class="summary-item">
<strong>โ
Status</strong>
<p>Optional but highly recommended</p>
</div>
<div class="summary-item">
<strong>โ
Purpose</strong>
<p>Add custom functionality to theme</p>
</div>
<div class="summary-item">
<strong>โ
Location</strong>
<p>Theme root directory</p>
</div>
<div class="summary-item">
<strong>โ
Acts Like</strong>
<p>Theme-specific plugin</p>
</div>
<div class="summary-item">
<strong>โ
Common Uses</strong>
<p>Theme setup, enqueue assets, hooks</p>
</div>
<div class="summary-item">
<strong>โ
Best Practice</strong>
<p>Prefix all functions with theme name</p>
</div>
<div class="summary-item">
<strong>โ
Important Rule</strong>
<p>Never close PHP tag at end</p>
</div>
<div class="summary-item">
<strong>โ
Loading</strong>
<p>Loads automatically before templates</p>
</div>
</div>
<div class="success-box" style="margin-top: 30px;">
<h4>๐ Key Takeaways:</h4>
<ul>
<li>โจ Optional file but used in almost every theme</li>
<li>๐ฏ Acts like a plugin for your theme</li>
<li>๐จ Enable theme features & add functionality</li>
<li>โก Use hooks (actions/filters) properly</li>
<li>๐ง Never close PHP tag at end</li>
<li>๐ Prefix functions to avoid conflicts</li>
</ul>
</div>
</div>
</div>
</section>
<!-- Footer -->
<footer>
<div class="container">
<h3 style="margin-bottom: 20px;">๐ Mission Accomplished!</h3>
<p>You've mastered functions.php and are ready to supercharge your WordPress themes!</p>
<p style="margin-top: 20px; color: var(--text-muted); font-size: 0.9rem;">
Generated by <strong>AI Prompt Dictionary</strong> ๐ค<br>
Making learning interactive, fun, and effective!
</p>
<div style="margin-top: 30px;">
<span class="badge">Interactive Learning</span>
<span class="badge">WordPress Master</span>
<span class="badge">Theme Development</span>
</div>
</div>
</footer>
<script>
// Progress Bar
window.addEventListener('scroll', () => {
const windowHeight = window.innerHeight;
const documentHeight = document.documentElement.scrollHeight - windowHeight;
const scrolled = window.scrollY;
const progress = (scrolled / documentHeight) * 100;
document.getElementById('progressBar').style.width = progress + '%';
});
// Intersection Observer
const sections = document.querySelectorAll('section');
const observerOptions = {
threshold: 0.3,
rootMargin: '0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, observerOptions);
sections.forEach(section => observer.observe(section));
// Tab Switching
function switchTab(event, tabName) {
const tabContents = document.querySelectorAll('.tab-content');
const tabBtns = document.querySelectorAll('.tab-btn');
tabContents.forEach(content => content.classList.remove('active'));
tabBtns.forEach(btn => btn.classList.remove('active'));
document.getElementById(tabName).classList.add('active');
event.target.classList.add('active');
}
// Copy Code
function copyCode(button) {
const codeBlock = button.nextElementSibling;
const code = codeBlock.textContent;
navigator.clipboard.writeText(code).then(() => {
button.textContent = 'โ Copied!';
button.style.background = 'var(--accent-green)';
setTimeout(() => {
button.textContent = '๐ Copy';
button.style.background = 'var(--accent-purple)';
}, 2000);
});
}
// Interactive Use Case Demo
function showUseCase(useCase) {
const output = document.getElementById('useCaseOutput');
const demos = {
features: `<span style="color: var(--accent-green);">โ
Theme Features</span>
<span style="color: var(--text-secondary);">// Enable WordPress features
add_theme_support( 'post-thumbnails' );
add_theme_support( 'title-tag' );
add_theme_support( 'custom-logo' );
add_theme_support( 'html5' );
// Unlocks: Featured images, automatic title tags,
// custom logo uploader, modern HTML5 markup</span>`,
menus: `<span style="color: var(--accent-cyan);">โ
Navigation Menus</span>
<span style="color: var(--text-secondary);">// Register menu locations
register_nav_menus( array(
'primary' => 'Primary Menu',
'footer' => 'Footer Menu',
) );
// Creates: Menu locations in admin
// Users can assign menus to these locations</span>`,
widgets: `<span style="color: var(--accent-pink);">โ
Widget Areas</span>
<span style="color: var(--text-secondary);">// Register sidebar
register_sidebar( array(
'name' => 'Sidebar',
'id' => 'sidebar-1',
) );
// Creates: Widget area in admin
// Users can drag widgets into this area</span>`,
custom: `<span style="color: var(--accent-orange);">โ
Custom Functions</span>
<span style="color: var(--text-secondary);">// Create reusable function
function mytheme_reading_time() {
$content = get_the_content();
$words = str_word_count( strip_tags( $content ) );
return ceil( $words / 200 ) . ' min read';
}
// Use anywhere: echo mytheme_reading_time();</span>`
};
output.innerHTML = `<pre style="margin: 0;">${demos[useCase]}</pre>`;
}
// Quiz Logic
let correctAnswers = 0;
let answeredQuestions = new Set();
function checkAnswer(element, isCorrect, questionNum) {
if (answeredQuestions.has(questionNum)) return;
answeredQuestions.add(questionNum);
const feedback = document.getElementById('feedback' + questionNum);
const options = element.parentElement.querySelectorAll('.quiz-option');
options.forEach(opt => opt.style.pointerEvents = 'none');
if (isCorrect) {
element.classList.add('correct');
feedback.className = 'quiz-feedback correct show';
feedback.innerHTML = '๐ Perfect! You nailed it!';
correctAnswers++;
createConfetti();
} else {
element.classList.add('incorrect');
feedback.className = 'quiz-feedback incorrect show';
feedback.innerHTML = 'โ Not quite! Review the section above.';
}
if (answeredQuestions.size === 3 && correctAnswers === 3) {
setTimeout(() => {
document.getElementById('quiz-complete').style.display = 'block';
createMegaConfetti();
}, 1000);
}
}
// Confetti Effects
function createConfetti() {
const colors = ['#a855f7', '#3b82f6', '#10b981', '#ec4899'];
for (let i = 0; i < 30; i++) {
const confetti = document.createElement('div');
confetti.className = 'confetti';
confetti.style.left = Math.random() * 100 + '%';
confetti.style.background = colors[Math.floor(Math.random() * colors.length)];
confetti.style.animationDelay = Math.random() * 0.5 + 's';
document.body.appendChild(confetti);
setTimeout(() => confetti.remove(), 3000);
}
}
function createMegaConfetti() {
const colors = ['#a855f7', '#3b82f6', '#10b981', '#ec4899', '#f59e0b'];
for (let i = 0; i < 100; i++) {
const confetti = document.createElement('div');
confetti.className = 'confetti';
confetti.style.left = Math.random() * 100 + '%';
confetti.style.background = colors[Math.floor(Math.random() * colors.length)];
confetti.style.animationDelay = (Math.random() * 2) + 's';
confetti.style.width = (Math.random() * 10 + 5) + 'px';
confetti.style.height = confetti.style.width;
document.body.appendChild(confetti);
setTimeout(() => confetti.remove(), 5000);
}
}
</script>
</body>
</html>
Live Preview