How WordPress uses OOP internally

WordPress, despite being originally built in a procedural style, has incorporated Object-Oriented Programming (OOP) concepts extensively over the years. Internally, WordPress uses OOP in several key areas to manage its core functionalities efficiently. Here’s a breakdown of how it applies OOP principles:
1. Class-Based Structure
WordPress defines many of its core features using PHP classes. Some examples include:
-
WP_Query – Handles database queries for retrieving posts.
-
WP_Post – Represents a single post object.
-
WP_User – Manages user-related operations.
-
WP_Widget – Provides a base class for creating widgets.
-
WP_Error – Standardized error-handling class.
These classes encapsulate data and behavior, making WordPress extensible and modular.
2. Encapsulation and Abstraction
WordPress uses encapsulation to restrict direct access to certain properties and methods. For example:
-
Many core classes have private or protected properties to prevent direct modification.
-
Some classes provide getter and setter methods to access internal properties safely.
Example:
$user = new WP_User(1);
echo $user->user_email; // Accessing a public property
However, some older parts of WordPress still rely on global variables, breaking strict encapsulation.
3. Inheritance
WordPress uses inheritance to extend functionality in multiple areas. Some examples:
-
WP_Widget – A base class for all widgets.
-
Developers can create custom widgets by extending this class.
-
Example:
class My_Custom_Widget extends WP_Widget {
public function __construct() {
parent::__construct('my_widget', 'My Custom Widget');
}
}
-
WP_Customize_Control – The base class for custom controls in the WordPress Customizer.
This allows developers to create specialized versions of these components without modifying core code.
4. Polymorphism (Hooks & Filters)
WordPress achieves polymorphism through its Hooks API (Actions and Filters), allowing developers to modify functionality without directly modifying core files.
Example:
add_filter('the_content', 'modify_content');
function modify_content($content) {
return $content . "<p>Custom text added.</p>";
}
This provides a flexible way to extend and override functionality dynamically.
5. Dependency Injection & Design Patterns
While WordPress does not strictly follow modern design patterns like Dependency Injection (DI) throughout, it does use patterns like:
-
Singleton Pattern in classes like
WP_Session_Tokens
to ensure only one instance exists. -
Factory Pattern in classes like
WP_Filesystem
which provides an abstraction over different file operations. -
Strategy Pattern in
WP_Filesystem
to handle different filesystem implementations (FTP, Direct, SSH).
Example of Singleton:
class SingletonExample {
private static $instance = null;
private function __construct() {
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
}
6. Autoloading (Limited Usage)
WordPress does not fully leverage Composer’s PSR-4 autoloading, but it does use a custom autoloader in some areas (like REST API classes). However, plugins and themes often rely on Composer for autoloading.

- Web Development
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Games
- Gardening
- Health
- Home
- Literature
- Music
- Networking
- Other
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness