Created
May 11, 2024 15:12
-
-
Save theking2/106601cf9e58388a473c06f07f55dd3f to your computer and use it in GitHub Desktop.
WordPress Bootstrap function to load actions and filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tell me if this is weird