DEV Community

Manuel Canga
Manuel Canga

Posted on

Write Modular, Extensible Python Code Using WordPress-Style Hooks (Introducing wphooks)

If you've ever touched WordPress as a developer, you already know its secret superpower: hooks.

Actions and filters are the backbone of WordPress extensibility and the reason plugins can modify almost anything without altering core files.

Now imagine bringing that same extensibility to Python — in a clean, lightweight, framework-agnostic way.

That’s exactly what wphooks does.

PyPI: pip install wphooks

GitHub: https://github.com/manuelcanga/wphooks/


Why Should Python Developers Care About Hooks?

Python already has signals (Django), observers, event emitters, and libraries like blinker.

They’re great — but none of them fully recreate the plugin-style extensibility that WordPress provides:

  • “Do something at this specific moment”
  • “Modify this value before using it”
  • “Add new behavior without touching core code”
  • “Make features live in separate modules, not inside giant legacy functions”

This is where wphooks shines.

It brings the same concepts that power 40% of the web directly into Python:

  • Actions → fire events
  • Filters → modify data through chained transformations
  • Minimal APIadd_action(), do_action(), add_filter(), apply_filters()

No frameworks. No heavy abstractions. Just plug-and-extend.


When Hooks Save You From Legacy Hell

Picture this classic scenario:

You inherit a 1,000-line function, like process_order().

It's business-critical. Nobody dares touch it.

Now product wants “Send Slack notification when order completes”.

Option A: Edit the massive function and pray nothing breaks.

Option B: Add one hook call and keep new logic in a clean new file.

With wphooks:

Inside the legacy function:

def process_order(order):
    # existing logic...
    do_action('order_processed', order)
Enter fullscreen mode Exit fullscreen mode

In a separate file/module:

def send_slack_notification(order):
    slack.send(f"Order {order.id} received!")

add_action('order_processed', send_slack_notification)
Enter fullscreen mode Exit fullscreen mode

This is the Open/Closed Principle in practice:
Your legacy function stays closed for modification but open for extension.


Actions: “Do Something When This Happens”

Actions let you attach any number of functions to a named event and run them at the right moment.

from wphooks import add_action, do_action

def send_welcome_email(user_id):
    print(f"Sending welcome email to {user_id}")

add_action('user_registered', send_welcome_email)

do_action('user_registered', 123)
Enter fullscreen mode Exit fullscreen mode

Perfect for:

  • sending emails
  • logging
  • analytics
  • third-party integrations
  • background tasks

Filters: “Modify a Value Before Using It”

Filters are the real differentiator of wphooks compared to Django Signals or blinker.
They allow chained transformation of data, just like WordPress.

from wphooks import add_filter, apply_filters

def uppercase_title(title):
    return title.upper()

add_filter('format_title', uppercase_title)

title = apply_filters('format_title', "hello world")
print(title)   # HELLO WORLD
Enter fullscreen mode Exit fullscreen mode

Use filters for:

  • data formatting
  • sanitization
  • price adjustments
  • pre-processing values
  • plugin overrides
  • configuration mutation

Multiple filters can modify the value in sequence.


Installation

Install from PyPI:

pip install wphooks
Enter fullscreen mode Exit fullscreen mode

Or install from source:

git clone https://github.com/manuelcanga/wphooks.git
cd wphooks
pip install .
Enter fullscreen mode Exit fullscreen mode

Import:

from wphooks import add_action, do_action
from wphooks import add_filter, apply_filters
Enter fullscreen mode Exit fullscreen mode

How wphooks Fits into the Python Landscape

Compared to Django Signals

Signals are powerful, but:

  • they don’t provide filters
  • they're tied to Django’s app infrastructure
  • they're less suitable for plugin systems

wphooks is framework-agnostic and intentionally minimal.

Compared to blinker

Blinker is an event emitter, not an extensibility layer.
It handles broadcasting well, but doesn't model the “WordPress-style hook pipeline”.

Compared to custom observer patterns

You can build your own, sure — but wphooks gives you:

  • a predictable API
  • simple naming conventions
  • an instantly recognizable mental model

Who Should Use wphooks?

Use wphooks if you want:

  • WordPress-style extensibility in Python
  • real data-modifying filters
  • a lightweight plugin architecture
  • a safe way to modernize legacy code
  • extensibility without frameworks

Final Thoughts

WordPress proved long ago that a simple hook system can power a massive ecosystem of plugins, themes, and extensions.
wphooks brings this tried-and-true architecture to Python, enabling developers to build clean, modular, and extensible applications with almost zero overhead.

If you’ve ever wished Python had a direct equivalent to WordPress hooks, now it does.

Install and try it:

pip install wphooks
Enter fullscreen mode Exit fullscreen mode

Repository and documentation:
https://github.com/manuelcanga/wphooks/

Top comments (0)