Prefixing plugin hooks (actions/filters) with a wrapper class or functions

I'm developing a rather extensive plugin which has a lot of hooks that are prefixed with the plugin namespace. I have created a wrapper class like the one below but because this plugin is geared towards developers I want it to adhere to WordPress standards (coding and documentation) as best as possible.

Are the below examples recommended? If not, is there a more WordPress way of doing this or should I just stick to hardcoding the prefix in every hook?

Example of basic usage for reference:


add_filter( 'namespace/value', function( $value, $context ) { ... }, 10, 2 );

$value = apply_filters( 'namespace/value', $value, $context );

add_action( 'namespace/example', function( $context ) { ... } );

do_action( 'namespace/example', $context );

Example 1: Class:

?php

namespace MyPlugin\Wrappers;

class Hooks {

    private static function prefix( string $value ) {
        return 'namespace/' . $value;
    }

    public static function add_filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
        add_filter( self::prefix( $tag ), $callback, $priority, $accepted_args );
    }

    public static function apply_filters( string $tag, $value, ...$args ) {
        return apply_filters( self::prefix( $tag ), $value, ...$args );
    }

    public static function add_action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
        add_action( self::prefix( $tag ), $callback, $priority, $accepted_args );
    }

    public static function do_action( string $tag, ...$args ) {
        do_action( self::prefix( $tag ), ...$args );
    }

}

Usage:

use MyPlugin\Wrappers\Hooks;

Hooks::add_filter( 'value', function( $value, $context ) { ... }, 10, 2 );

$value = Hooks::apply_filters( 'value', $value, $context );

Hooks::add_action( 'example', function( $context ) { ... } );

Hooks::do_action( 'example', $context );

Example 2: A more WordPress procedural approach...


function myplugin_prefix( $value ) {
    return 'namespace/' . $value;
}

function myplugin_add_filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
    add_filter( myplugin_prefix( $tag ), $callback, $priority, $accepted_args );
}

function myplugin_apply_filters( string $tag, $value, ...$args ) {
    return apply_filters( myplugin_prefix( $tag ), $value, ...$args );
}

function myplugin_add_action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
    add_action( myplugin_prefix( $tag ), $callback, $priority, $accepted_args );
}

function myplugin_do_action( string $tag, ...$args ) {
    do_action( myplugin_prefix( $tag ), ...$args );
}

Usage:

myplugin_add_filter( 'value', function( $value, $context ) { ... }, 10, 2 );

$value = myplugin_apply_filters( 'value', $value, $context );

myplugin_add_action( 'example', function( $context ) { ... } );

myplugin_do_action( 'example', $context );

Topic coding-standards actions filters plugin-development hooks Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.