Back to Articles
25 min read

Mastering WordPress Architecture: Real-World Projects for Themes & Plugins

Theory isn't enough when mastering the WordPress ecosystem. In this deep dive, we architect complete solutions—ranging from a modern block-based theme to a complex functionality plugin. We analyze the code structure, explore the Hook system, and implement best practices for security and performance.

Complete Plugin Example

<?php /** * Plugin Name: My Custom Plugin * Description: Demonstrates WordPress Hook System * Version: 1.0.0 */ // Prevent direct access if ( ! defined( 'ABSPATH' ) ) { exit; } class My_Custom_Plugin { private static $instance = null; public static function get_instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } private function __construct() { // Activation/Deactivation hooks register_activation_hook( __FILE__, array( $this, 'activate' ) ); register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) ); // Action hooks add_action( 'init', array( $this, 'init' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'wp_footer', array( $this, 'footer_content' ) ); // Filter hooks add_filter( 'the_content', array( $this, 'modify_content' ) ); add_filter( 'the_title', array( $this, 'modify_title' ), 10, 2 ); // Custom hooks add_action( 'my_plugin_loaded', array( $this, 'plugin_loaded' ) ); } public function activate() { // Create database tables, set options, etc. add_option( 'my_plugin_version', '1.0.0' ); flush_rewrite_rules(); } public function deactivate() { // Cleanup tasks flush_rewrite_rules(); } public function init() { // Register custom post type register_post_type( 'my_custom_post', array( 'labels' => array( 'name' => 'Custom Posts', 'singular_name' => 'Custom Post', ), 'public' => true, 'has_archive' => true, )); // Fire custom action do_action( 'my_plugin_loaded' ); } public function enqueue_assets() { wp_enqueue_style( 'my-plugin-style', plugin_dir_url( __FILE__ ) . 'css/style.css', array(), '1.0.0' ); wp_enqueue_script( 'my-plugin-script', plugin_dir_url( __FILE__ ) . 'js/script.js', array( 'jquery' ), '1.0.0', true ); } public function add_admin_menu() { add_menu_page( 'My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array( $this, 'admin_page' ), 'dashicons-admin-plugins', 100 ); } public function admin_page() { echo '<div class="wrap">'; echo '<h1>My Plugin Settings</h1>'; // Allow others to add content do_action( 'my_plugin_admin_page' ); echo '</div>'; } public function modify_content( $content ) { if ( is_single() ) { $additional = apply_filters( 'my_plugin_additional_content', '' ); $content .= $additional; } return $content; } public function modify_title( $title, $post_id ) { if ( get_post_type( $post_id ) === 'my_custom_post' ) { $title = '⭐ ' . $title; } return $title; } public function footer_content() { echo '<!-- My Plugin Active -->'; } public function plugin_loaded() { // Plugin is fully loaded error_log( 'My Plugin Loaded!' ); } } // Initialize plugin My_Custom_Plugin::get_instance();

Complete Theme Example (functions.php)

<?php /** * Theme Functions - Demonstrating Hook System */ // Theme Setup add_action( 'after_setup_theme', 'my_theme_setup' ); function my_theme_setup() { // Add theme support add_theme_support( 'title-tag' ); add_theme_support( 'post-thumbnails' ); add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', )); // Register navigation menus register_nav_menus( array( 'primary' => 'Primary Menu', 'footer' => 'Footer Menu', )); } // Enqueue styles and scripts add_action( 'wp_enqueue_scripts', 'my_theme_assets' ); function my_theme_assets() { // Styles wp_enqueue_style( 'my-theme-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) ); // Scripts wp_enqueue_script( 'my-theme-script', get_template_directory_uri() . '/js/main.js', array( 'jquery' ), '1.0.0', true ); // Localize script wp_localize_script( 'my-theme-script', 'myTheme', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'my_theme_nonce' ), )); } // Register widget areas add_action( 'widgets_init', 'my_theme_widgets' ); function my_theme_widgets() { register_sidebar( array( 'name' => 'Main Sidebar', 'id' => 'sidebar-main', 'before_widget' => '<div class="widget %2$s">', 'after_widget' => '</div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>', )); } // Custom excerpt length add_filter( 'excerpt_length', 'my_theme_excerpt_length' ); function my_theme_excerpt_length( $length ) { return 25; // 25 words } // Custom excerpt more text add_filter( 'excerpt_more', 'my_theme_excerpt_more' ); function my_theme_excerpt_more( $more ) { return '... <a href="' . get_permalink() . '">Read More</a>'; } // Add custom body classes add_filter( 'body_class', 'my_theme_body_classes' ); function my_theme_body_classes( $classes ) { // Add class for logged-in users if ( is_user_logged_in() ) { $classes[] = 'user-logged-in'; } // Add class for specific page templates if ( is_page_template( 'template-fullwidth.php' ) ) { $classes[] = 'full-width-layout'; } return $classes; } // Modify main query add_action( 'pre_get_posts', 'my_theme_modify_query' ); function my_theme_modify_query( $query ) { // Only modify main query on frontend if ( ! is_admin() && $query->is_main_query() ) { // Custom posts per page for archive if ( is_archive() ) { $query->set( 'posts_per_page', 12 ); } // Exclude category from homepage if ( is_home() ) { $query->set( 'cat', '-5' ); // Exclude category ID 5 } } } // Add custom meta boxes add_action( 'add_meta_boxes', 'my_theme_meta_boxes' ); function my_theme_meta_boxes() { add_meta_box( 'my_theme_options', 'Page Options', 'my_theme_meta_box_callback', 'page', 'side', 'default' ); } function my_theme_meta_box_callback( $post ) { wp_nonce_field( 'my_theme_meta_box', 'my_theme_meta_box_nonce' ); $value = get_post_meta( $post->ID, '_hide_title', true ); echo '<label>'; echo '<input type="checkbox" name="hide_title" value="1" ' . checked( $value, '1', false ) . '>'; echo ' Hide page title'; echo '</label>'; } // Save meta box data add_action( 'save_post', 'my_theme_save_meta_box' ); function my_theme_save_meta_box( $post_id ) { // Verify nonce if ( ! isset( $_POST['my_theme_meta_box_nonce'] ) ) { return; } if ( ! wp_verify_nonce( $_POST['my_theme_meta_box_nonce'], 'my_theme_meta_box' ) ) { return; } // Check autosave if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check permissions if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Save data $hide_title = isset( $_POST['hide_title'] ) ? '1' : '0'; update_post_meta( $post_id, '_hide_title', $hide_title ); } // AJAX handler add_action( 'wp_ajax_my_theme_action', 'my_theme_ajax_handler' ); add_action( 'wp_ajax_nopriv_my_theme_action', 'my_theme_ajax_handler' ); function my_theme_ajax_handler() { check_ajax_referer( 'my_theme_nonce', 'nonce' ); $data = sanitize_text_field( $_POST['data'] ); // Process data $response = array( 'success' => true, 'message' => 'Data processed: ' . $data, ); wp_send_json( $response ); } // Custom login logo add_action( 'login_head', 'my_theme_login_logo' ); function my_theme_login_logo() { echo '<style> .login h1 a { background-image: url(' . get_template_directory_uri() . '/images/logo.png); background-size: contain; width: 200px; } </style>'; }