Created
July 4, 2011 14:32
-
-
Save franz-josef-kaiser/1063398 to your computer and use it in GitHub Desktop.
add_method() - automagically hook a method from inside a class into a hook named exactly like the function
This file contains hidden or 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 | |
| /** | |
| * Adds a method from inside a class to an action hook. | |
| * The method must be named exactly like the hook it's attached to. | |
| * | |
| * @param (string) $method | The called method/action to hook | |
| * @param (integer) $priority | The priority the action hooks into | |
| * @param (integer) $accepted_args | Number of arguments the hook accepts | |
| * @return (boolean) | true on success, throws exception on failure. | |
| */ | |
| static function add_method( $method, $priority = 10, $accepted_args = 1 ) | |
| { | |
| $backtrace = debug_backtrace(); | |
| $class = $backtrace[ 1 ][ 'class' ]; | |
| if ( ! isset( $class ) ) | |
| return new Exception ( "The class couldn't be backtraced." ); | |
| if ( method_exists( $class, $method ) ) | |
| add_action( $method, array( $class,$method ), $priority, $accepted_args ); | |
| return true; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment