Skip to content

Instantly share code, notes, and snippets.

@theking2
Created May 11, 2024 15:12
Show Gist options
  • Save theking2/106601cf9e58388a473c06f07f55dd3f to your computer and use it in GitHub Desktop.
Save theking2/106601cf9e58388a473c06f07f55dd3f to your computer and use it in GitHub Desktop.
WordPress Bootstrap function to load actions and filters
<?php declare(strict_types=1);
abstract class PluginName
{
public function __construct()
{
$this->bootstrap();
}
private function bootstrap()
{
foreach( get_class_methods( $this ) as $method ) {
$re = "/(?'type'action|filter)_(?'hook'.*)_(?'priority'[0-9]*)_(?'args'[0-9]*)/";
if( preg_match( $re, $method, $matches ) ) {
$type = $matches['type'];
$hook = $matches['hook'];
$priority = $matches['priority'];
$args = $matches['args'];
$function = "add_$type";
$function( strtolower( $hook ), [ $this, $method ], $priority, $args );
}
}
}
/**
* sample add_action('init',...)
*/
abstract public function action_init_10_1();
}
@theking2
Copy link
Author

Tell me if this is weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment