Home FAQ

21 questions answered

SOLID

Answers pulled from across 4wp.dev — plugins, architectures, development, jobs, and audience guides. Powered by the 4WP FAQ registry.

SOLID

Q: Can I use OCP without object-oriented programming in WordPress?

A: Absolutely! WordPress hooks (add_actionadd_filter) are a perfect OCP implementation using procedural code. Your functions stay closed (working code), others extend via hooks (adding functionality). OOP adds more tools, but hooks alone achieve OCP beautifully.

Q: Do I need a full DI framework?

A: Not at all. A simple array-based container handles 95% of cases. For larger plugins, consider PHP-DI or Pimple. But starting with constructor injection and interfaces is enough.

Q: Does LSP mean I can never override parent methods?

A: No! Overriding is fine as long as the contract is preserved. You can make behavior more specific, optimize performance, or add features — but you cannot break the promises the parent made (same return types, no new exceptions, same preconditions).

Q: Does SRP mean one function per file in WordPress?

A: No, SRP is about responsibility, not file count. You can have multiple related functions in one file as long as they serve the same high-level responsibility. For example, all post validation functions can live in post-validation.php, while email functions go in email-notifications.php.

Q: Does WordPress core follow DIP?

A: Partially. The Hooks API is an excellent abstraction layer — plugins depend on the hook system, not on core internals. But older WordPress code uses global functions and static calls heavily.

Q: Doesn’t OCP make code more complex with all these abstractions?

A: Initially yes, but it pays off as the project grows. Start simple – a few hooks are enough for small plugins. Add interfaces and abstract classes when you have 3+ similar implementations. The complexity is justified when it prevents breaking existing functionality while adding features.

Q: How do I apply SRP to WordPress hooks that naturally do multiple things?

A: Instead of one callback doing everything, create multiple focused callbacks. For example, on save_post, use separate functions for validation, notification, caching, and analytics — each attached to the same hook but handling one responsibility.

Q: How do I know if my subclass violates LSP?

A: Ask yourself: “Can I replace every use of the parent class with this child class, without the tests failing or behavior changing?” If yes — you’re good. If any test breaks or you need to add instanceof checks — LSP is violated.

Q: How does LSP apply to WP_Widget extensions?

A: When you extend WP_Widget, your widget must implement widget()form(), and update() correctly. If you override update() to return false always, you break WordPress’s expectation — an LSP violation.

Q: How does LSP relate to WordPress hooks and filters?

A: Filters are a great example of LSP — any callback passed to apply_filters() must accept and return the same type. If a filter expects a string and you return an array, you’ve violated the contract — exactly like LSP.

Q: How does OCP relate to WordPress plugin updates?

A: OCP is crucial for plugin compatibility. If your plugin follows OCP (provides hooks), users can extend it safely. When you update, their extensions keep working because they’re not modifying your code. This is why editing vendor plugin files is dangerous – updates erase custom changes.

Q: How does SRP relate to WordPress Plugin API and action/filter hooks?

A: WordPress hooks are perfect for SRP! Each hook callback should handle one specific task. Use do_action() to notify other parts of your code about events, allowing different responsibilities to respond independently. This creates loose coupling and high cohesion.

Q: How many hooks should I add to my WordPress plugin?

A: Add hooks at logical extension points – before/after major operations, when data changes, or when rendering output. Follow WordPress core’s approach: do_action('before_save_')apply_filters('the_content'), etc. Don’t overdo it – too many hooks make code hard to follow.

Q: Is it always wrong to use inheritance for code reuse only?

A: Yes, using inheritance purely for code reuse (without a real “is-a” relationship) is a warning sign. It often leads to LSP violations. Use traits or composition instead for code reuse, and reserve inheritance for true behavioral subtypes.

Q: Is it overkill for small WordPress plugins?

A: For tiny plugins (under 200 lines), it can be overkill. But once you have classes with external dependencies (email, cache, DB, APIs), DIP pays off immediately through testability and flexibility.

Q: Should every function have a filter in WordPress development?

A: No, only functions that return data users might want to modify. Use filters for content transformation (apply_filters('post_title', $title)), use actions for event notifications (do_action('post_saved', $id)). Internal helper functions don’t need hooks.

Q: Should I refactor all my existing WordPress plugins to follow SRP?

A: Apply the Boy Scout Rule — “leave code better than you found it.” When you need to modify existing code, refactor that section to follow SRP. Don’t attempt massive rewrites, but gradually improve code as you work with it. Focus on new features being SRP-compliant from the start.

Q: What about small WordPress sites — is SRP overkill?

A: Even small sites benefit from SRP, but the implementation scale differs. For a simple blog, separating validation from email sending in your contact form is enough. You don’t need complex class hierarchies — simple, focused functions work great and make your theme easier to maintain as the site grows.

Q: What if I need to fix a bug – does that violate OCP?

A: No, OCP is about adding features, not fixing bugs. Bug fixes are legitimate modifications. The principle means “don’t modify working code to add new features” – fixing broken code is different. After the fix, that code should be closed for new features again.

Q: What’s the difference between DIP and Dependency Injection?

A: DIP is the principle — depend on abstractions, not concretions. Dependency Injection (DI) is a technique to achieve it — passing dependencies into a class rather than creating them inside.

Q: Won’t splitting code into many small functions make WordPress slower?

A: No, modern PHP handles function calls efficiently. The real performance killer is poorly written monolithic code that does unnecessary work. Well-organized code following SRP is easier to optimize because you can identify and improve bottlenecks without touching unrelated functionality.

Still have questions?

Reach out via contacts or explore the plugin docs if you are building with 4WP FAQ yourself.