Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created July 4, 2011 14:32
Show Gist options
  • Select an option

  • Save franz-josef-kaiser/1063398 to your computer and use it in GitHub Desktop.

Select an option

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
<?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