1. Go to this page and download the library: Download boxuk/wp-hook-attributes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
boxuk / wp-hook-attributes example snippets
use Cache\Adapter\Memcache\MemcacheCachePool;
use Psr\Cache\CacheItemPoolInterface;
if ( wp_get_environment_type() === 'production' ) {
add_filter(
'wp_hook_attributes_cache_adapter',
function ( CacheItemPoolInterface $cache_adapter ): CacheItemPoolInterface {
global $wp_object_cache;
if ( $wp_object_cache->get_mc( 'default' ) instanceof \Memcache ) {
$client = $wp_object_cache->get_mc( 'default' );
return new MemcacheCachePool( $client );
}
return $cache_adapter;
}
);
}
use BoxUk\WpHookAttributes\Hook\Attributes\Action;
use BoxUk\WpHookAttributes\Hook\Attributes\Filter;
// Example of using an action hook
#[Action('init')]
function basic_action(): string {
return 'something...';
}
// Example of using a filter hook
#[Filter('the_content')]
function basic_filter(): string {
return 'something...';
}
// You can also attach a priority and args
#[Action('init', priority: 20, args: 4)]
function advanced_action( string $arg1, int $arg2, bool $arg3, array $arg4 ): string
return 'something...';
}
use BoxUk\WpHookAttributes\Hook\Annotations\Action;
use BoxUk\WpHookAttributes\Hook\Annotations\Filter;
// Example of using an action hook
/**
* @Action("init")
*/
function basic_action(): string {
return 'something...';
}
// Example of using a filter hook
/**
* @Filter("the_content")
*/
function basic_filter(): string {
return 'something...';
}
// You can also attach a priority and args
/**
* @Action("init", priority="20", args="4")
*/
function advanced_action( string $arg1, int $arg2, bool $arg3, array $arg4 ): string
return 'something...';
}
/**
* @ActionAnnotation("muplugins_loaded")
*/
#[Action('muplugins_loaded')]
function muplugins_loaded_action(): void
{
echo 'on muplugins_loaded action';
}
// Some place earlier than `muplugins_loaded`, maybe a `000-my-project.php` or something within `mu-plugins`.
( new WordPressHookAttributes() )();
// This will not work.
add_action( 'wp_loaded', function() {