Last active
December 15, 2015 13:38
-
-
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
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 | |
/** | |
* 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