PHP code example of spenserhale / wp-autohook-library

1. Go to this page and download the library: Download spenserhale/wp-autohook-library 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/ */

    

spenserhale / wp-autohook-library example snippets


#[Hook('cli_init', priority: 9)]
public static function registerCommand(): void {}

#[Hook('rest_api_init')]
public static function registerController(): void {}

#[Hook('wp_init', 99)]
public static function boot(): void {}

#[Hook(priority: 100, tag: 'pre_update_option')]
public static function filterOption(string $value): string {}

// Stacked attributes
#[Hook('wp_insert_blog_meta')]
#[Hook('wp_insert_post_meta')]
public static function logInsert(array $data, array $postarr): array {}

// Comma-separated (same line)
#[Hook('update_post_meta'), Hook('update_option_meta')]
public static function logChange(mixed $old_value, mixed $new_value): void {}

#[Shortcode('example_shortcode')]
public static function shortcode(): string {}

abstract class AbstractAdminPage
{
    public static function registerPage(): void {}
    
    public static function renderPage(): void {}
}

#[Hook('admin_menu', callback: 'registerPage')]
#[Hook('admin_init', callback: 'renderPage')]
class SettingsPage extends AbstractAdminPage
{
    // Inherited methods are hooked via class-level attributes
}

namespace ACME;

#[Hook('facade_example_hook', callback: 'facadeMethod', arguments: 3)]
#[Shortcode('other_shortcode', 'otherShortcode')]
class ExamplesClass
{
    #[Hook('cli_init', priority: 9)]
    public static function registerCommand(): void {}

    #[Hook('rest_api_init')]
    public static function registerController(): void {}

    #[Hook('wp_init', 99)]
    public static function boot(): void {}

    #[Hook(priority: 100, tag: 'pre_update_option')]
    public static function filterOption(string $value): string {}

    #[Hook('wp_insert_blog_meta')]
    #[Hook('wp_insert_post_meta')]
    public static function logInsert(array $data, array $postarr): array {}

    #[Hook('update_post_meta'), Hook('update_option_meta')]
    public static function logChange(mixed $old_value, mixed $new_value): void {}

    #[Shortcode('example_shortcode')]
    public static function shortcode(): string {}
    
    public static function facadeMethod($arg1, $arg2, $arg3): mixed {}
    
    public static function otherShortcode(): string {}
}

//=== Start AutoHooks Generated Section ===
add_action('cli_init', 'ACME\ExamplesClass::registerCommand', 9, 0);
add_filter('facade_example_hook', 'ACME\ExamplesClass::facadeMethod', 10, 3);
add_filter('pre_update_option', 'ACME\ExamplesClass::filterOption', 100, 1);
add_action('rest_api_init', 'ACME\ExamplesClass::registerController', 10, 0);
add_action('update_option_meta', 'ACME\ExamplesClass::logChange', 10, 2);
add_action('update_post_meta', 'ACME\ExamplesClass::logChange', 10, 2);
add_action('wp_init', 'ACME\ExamplesClass::boot', 99, 0);
add_filter('wp_insert_blog_meta', 'ACME\ExamplesClass::logInsert', 10, 2);
add_filter('wp_insert_post_meta', 'ACME\ExamplesClass::logInsert', 10, 2);
add_shortcode('example_shortcode', 'ACME\ExamplesClass::shortcode');
add_shortcode('other_shortcode', 'ACME\ExamplesClass::otherShortcode');
//=== End AutoHooks Generated Section ===

// Get the list of classes
$classes = \SH\AutoHook\ComposerJsonParser::getClasses($composerJsonPath);

// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);

// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);
   

// Get Classloader object
$loader = s = \SH\AutoHook\ComposerClassLoaderParser::getClasses($loader, ['App\\', 'MyNamespace\\']);

// Process classes to string
[$output] = \SH\AutoHook\AttributeResolver::processClassesToString($classes);

// Write to file
(bool) $written = \SH\AutoHook\FileWriter::write($output, $outputPath);