-
-
Save markjaquith/3b79a70d9bf8fa3603f5 to your computer and use it in GitHub Desktop.
Detects WordPress hooks in your class via method name convention
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 | |
private function add_hooks() { | |
// Create public methods like: action__init, filter__the_title_20, etc | |
// Replace dashes and dots in hook names with _DASH_ and _DOT_, respectively | |
$methods = get_class_methods( $this ); | |
foreach ( $methods as $method ) { | |
if ( preg_match( '#(?P<type>action|filter)__(?P<method>.+?)(?:_(?P<priority>-?[0-9]+))?$#', $method, $match ) ) { | |
$match['priority'] = ( isset( $match['priority'] ) ) ? intval( $match['priority'] ) : 10; | |
$match['hook'] = str_replace( array( '_DOT_', '_DASH_' ), array( '.', '-' ), $match['method'] ); | |
$func = 'add_' . $match['type']; | |
$func( $match['hook'], array( $this, $match[0] ), $match['priority'], 99 ); // 99 = unlimited args, effectively | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment