Skip to content

Instantly share code, notes, and snippets.

@Cifro
Last active December 15, 2015 13:38
Show Gist options
  • Save Cifro/5268376 to your computer and use it in GitHub Desktop.
Save Cifro/5268376 to your computer and use it in GitHub Desktop.
Meta programming in WordPress here we go... Automatic registration of methods as wp_ajax_* hook callbacks via @wpajax annotation. No more manual listing hooks and callbacks in the old WP way
<?php
/**
* Registers all ajax hooks
* The method which suppose to be wp_ajax_* callback must have @WpAjax annotation
*/
public static function registerAjaxHooks()
{
$methods = get_class_methods(__CLASS__);
$r = new NClassReflection(__CLASS__);
foreach($methods as $method){
if($r->getMethod($method)->getAnnotation('WpAjax'))
add_action("wp_ajax_{$method}", array(__CLASS__, $method));
}
}
/**
* Saves Theme Options
* Will be called as do_action("wp_ajax_saveThemeOptions")
*
* @WpAjax
*/
public static function saveThemeOptions()
{
// code...
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment