Encapsulation
Abstraction
Inheritance
Polymorphism

OOP

Object-Oriented Programming

Four Principles · Infinite Possibilities

Inheritance in WordPress

Extend core base classes — reuse behavior, override only what changes.

WordPress is built on inheritance: widgets extend WP_Widget, REST endpoints extend WP_REST_Controller, customizers extend WP_Customize_Control. Child classes inherit boilerplate from core and focus on your plugin’s unique output — caching, markup, or query logic.

Core concept

A child class gets methods and properties from its parent. Override hooks like widget() or render_block() while shared utilities — caching, asset loading, sanitization — live in a base class inside your plugin suite.

WordPress core examples

  • WP_Widget — sidebar widgets: form, update, and front-end output.
  • Walker_Nav_Menu — customize menu HTML without rewriting walkers from scratch.
  • WP_Customize_Control — Customizer fields with shared enqueue and JSON logic.
  • WP_Block — block type registration inherits render callbacks from block.json.

Base widget with shared cache

Php
class Base_Widget extends WP_Widget {
    protected function cache( string $output ): void {
        set_transient( $this->id, $output, HOUR_IN_SECONDS );
    }
}

class Popular_Posts extends Base_Widget {
    public function widget( $args, $instance ) {
        $posts = get_posts( [ 'orderby' => 'comment_count', 'numberposts' => 5 ] );
        $html  = $this->render_posts( $posts );
        $this->cache( $html );
        echo $html; // phpcs:ignore WordPress.Security.EscapeOutput
    }
}

The 4WP plugin suite uses the same pattern: a shared base in 4wp-bundle-core or module base classes so each addon only implements its specific feature.

Explore other OOP pillars

Encapsulation
Deep dive →

Abstraction
Deep dive →

Polymorphism
Deep dive →