Skip to content

Instantly share code, notes, and snippets.

@markjaquith
Created January 18, 2011 06:49
Show Gist options
  • Save markjaquith/3b79a70d9bf8fa3603f5 to your computer and use it in GitHub Desktop.
Save markjaquith/3b79a70d9bf8fa3603f5 to your computer and use it in GitHub Desktop.
Detects WordPress hooks in your class via method name convention
<?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