Get Started
Guides April 26, 2026 5 min read

PHP Object Injection in WordPress: What It Is and How Plugins Can Prevent It

PHP object injection WordPress: What Is PHP Object Injection?

PHP Object Injection is a vulnerability that occurs when an application unserializes untrusted data. Serialization converts PHP objects into a storable string format. Unserialization reverses this process. When attackers control the serialized data, they can inject arbitrary PHP objects. These malicious objects can execute code, read files, or modify the database. WordPress and its plugins often use PHP serialization for caching and session storage. This widespread usage makes object injection a serious threat.

PHP object injection WordPress: How PHP Object Injection Works

PHP object injection exploits PHP’s unserialize() function. When your code calls unserialize() on attacker-controlled input, PHP reconstructs the original object. This process triggers special methods like __wakeup() or __destruct(). These magic methods run automatically during unserialization. Attackers craft serialized payloads that trigger dangerous functionality. For example, a __destruct() method might delete files or execute SQL queries. The attacker never needs to call these methods directly. PHP calls them automatically.

Common gadget chains use existing WordPress or plugin classes. Attackers find classes with useful magic methods. They chain these methods together to achieve code execution. The POP chain concept is central to modern object injection attacks. A POP (Property Oriented Programming) chain links multiple gadgets to perform a specific action. Finding these chains requires understanding the codebase. Automated tools now exist to discover gadget chains in popular plugins.

Real-World Impact on WordPress Sites

PHP Object Injection vulnerabilities have affected many popular plugins. In 2023 and 2024, researchers found critical vulnerabilities in WooCommerce, Advanced Custom Fields, and Contact Form 7 extensions. These flaws allowed attackers to achieve remote code execution. Sites running vulnerable plugins could lose complete control to attackers. The impact includes data theft, malware injection, and site defacement. Some vulnerabilities require authentication, but many work on unauthenticated endpoints.

Why Serialization Is Common in WordPress

WordPress uses PHP serialization extensively. The options table stores serialized data for plugins and themes. Transients use serialization for caching. Session data in custom plugins relies on serialization. Many caching plugins serialize complex data structures. The php-serialization library is lightweight and fast. However, it lacks built-in security features. Developers must implement safety checks manually. This creates many opportunities for vulnerabilities.

WordPress core provides functions like maybe_unserialize() that check data before unserializing. But many plugins call unserialize() directly on user input. This bypasses WordPress safety measures. Developers often assume serialized data comes from trusted sources. User-supplied data like cookies, form fields, and API parameters can contain malicious serialized payloads.

How Plugins Can Prevent Object Injection

Use JSON Instead of Serialization

JSON is a safer alternative to PHP serialization. JSON does not support PHP objects natively. It cannot trigger magic methods during parsing. Use json_encode() and json_decode() instead of serialize() and unserialize(). JSON handles arrays, strings, numbers, and nested structures well. Most modern applications prefer JSON for data exchange. This change eliminates the object injection attack surface entirely.

Validate Serialized Data Sources

Never unserialize data from untrusted sources. Trusted sources include your own database, cache server, or signed cookies. Validate that serialized data originates from a known, secure location. Use cryptographic signatures to verify data integrity. Sucuri Scanner includes checks for common serialization attacks. Trusti Security’s Known Vulnerabilities Scanner checks your installed plugins against a CVE database to identify plugins known to be vulnerable to object injection.

Implement Input Sanitization

Sanitize all input before processing it. Filter out serialized payloads in form submissions, URL parameters, and API requests. Use WordPress sanitization functions consistently. Wordfence provides firewall rules that detect serialized attack payloads. Trusti Security’s Admin Activity Log tracks plugin and theme changes, helping you spot if an attacker has installed malicious code after a successful object injection attack. Combine input validation with output escaping for defense in depth.

Use Allowed Classes Whitelist

PHP 7.0 and later support the allowed_classes parameter in unserialize(). This parameter accepts an array of class names. PHP only reconstructs objects from the listed classes. All other objects get converted to __PHP_Incomplete_Class objects. This prevents attackers from instantiating arbitrary gadget classes. Maintain a strict whitelist of classes you expect to unserialize. Update this list only when you add new functionality.

Detecting Object Injection Attempts

Detection is crucial for catching attacks before they succeed. Monitor for serialized payloads in unexpected locations. Watch for unserialize() calls in PHP error logs. Look for PHP warnings about incomplete class definitions. These warnings indicate deserialization of unknown object types. Trusti Security monitors your site for suspicious admin activity. If an attacker gains access after an object injection compromise, the activity log captures the incident. You can review blocked attempts and identify attack trends.

Best Practices for Plugin Developers

  • Avoid unserialize() on any data that originates from user input, HTTP requests, or external APIs.
  • Use JSON serialization as the default data format for new features.
  • When unserializing legacy data, use the allowed_classes parameter with a strict whitelist.
  • Validate HMAC signatures for serialized data stored in cookies or user metadata.
  • Run automated security scans on your codebase to detect unserialize() calls.
  • Keep PHP updated to benefit from security improvements in serialization handling.
  • Review plugin dependencies for known object injection vulnerabilities.

Tools for Scanning Plugins

Several tools help identify unserialize() vulnerabilities in WordPress plugins. The WPScan tool checks for known vulnerabilities in installed plugins. PHPStan with security rules detects unsafe unserialize() calls. RIPS and other static analysis tools find object injection vulnerabilities in PHP code. WPScan checks for known vulnerabilities in installed plugins. Trusti Security runs core integrity checks and vulnerability scans as part of its all-in-one approach. Its known vulnerabilities scanner catches plugins with published CVEs related to object injection. Run these scans regularly on your development and production environments.

Conclusion

PHP Object Injection is a serious but preventable vulnerability. It exploits PHP’s unserialize() function to execute arbitrary code. Understanding how these attacks work helps you protect your site. Use JSON instead of PHP serialization whenever possible. Validate data sources and sanitize inputs consistently. Keep your plugins updated to receive security patches. A comprehensive security solution like Trusti Security provides layered protection including admin activity logging, core integrity scanning, and vulnerability detection. Combining best practices with the right tools reduces your risk significantly. These steps will keep your WordPress site safe from one of the most dangerous vulnerability classes.

Related Articles