Register Widgets with a Static Method

Registering widgets in WordPress is annoying.

All of the functionality is bundled up nicely in the WP_Widget class, but you can’t use an object method for the registration. Most PHP classes I see used in the WordPress space are pseudo namespace singletons. They have a helper for registering methods to hooks, and then the object is instantiated and the helper called. The various class methods are hooked in with something like add_action( 'init', array( $this, 'method_name' ) );. There is nothing inherently wrong with this approach. It provides a nice pseudo namespace for code that is PHP 5.2 compatible. When it comes to widget registration, though, that pattern just doesn’t work.

More often than not I see the class declaration for a widget followed immediately by a standalone function to register it. In some instances, the standalone function will register a number of widget classes. This is fine, but it lacks a certain elegance that I prefer in my code.

To deal with this I’ve seen various different approaches. To maintain PHP 5.2 compatibility I often see the create_function( '', 'return register_widget("My_Widget");' ) approach. This takes a text string and turns it into a function which is then invoked by WordPress core registering the widget class.

I’ve also seen developers give up the 5.2 compatibility and use an anonymous function. If you’ve spent any time in Javascript, you’ve probably seen this construct. function(){ register_widget( 'My_Widget' ); }. The function keyword is used without an identifier. This function is then compiled right before it’s needed and invoked by core to register the widget. A little more elegant perhaps, but it requires at least PHP 5.3.

There is another way!

Static methods can be a bit confusing, but using one in this context gives us both PHP 5.2 compatibility, and a much more elegant, readable piece of code.

/**
 * Registers the widget with the WordPress Widget API.
 * 
 * @return void.
 */
public static function register() {
    register_widget( __CLASS__ );
}

Placing this static method at the top of your widget declaration will not interfere with the behavior of the widget in any way and gives you a built in helper that you can use to register the widget by name with the widgets API. Registration looks like this now:

add_action( 'widgets_init', array( 'Amazing_Widget', 'register' ) );

That is even more readable than the anonymous function technique. Here is an outline for what this might look like in context. Next time you create a widget, give it a try.

/**
 * A widget that does amazing things!
 */
class Amazing_Widget extends WP_Widget {
    /**
     * Registers the widget with the WordPress Widget API.
     * 
     * @return void.
     */
    public static function register() {
        register_widget( __CLASS__ );
    }
    /**
     * Sets up the widget in the system.
     *
     * @return Amazing_Widget An instance of this widget.
     */
    public function __construct() {
        parent::__construct( 'amazing_widget', 'Amazing Widget' );
    }
    /**
     * Outputs the widget admin form.
     *
     * @return void.
     */
    public function form( $instance ) {
        // Output the widget admin form here
    }
    /**
     * Validates and sanitizes the widget form input.
     *
     * @return array The sanitized and updated values.
     */
    public function update( $new_instance, $old_instance ) {
        // Validate and sanitize updates here
    }
    /**
     * Outputs the widget front end markup and data.
     *
     * @return void.
     */
    public function widget( $args, $instance ) {
        // Output the widget front end markup here
    }
}
add_action( 'widgets_init', array( 'Amazing_Widget', 'register' ) );

2 Comments

  1. It`s a great solution, thanks!

  2. Thanks!

Leave a Reply

Your email address will not be published.

*

© 2024 Luke on Everything

Theme by Anders NorénUp ↑