I really liked this post and played around with changing my construction process around it. Ultimately I didn't want to invest the time to properly figure out how to tackle filters or large $arg functions (I'm sure it's possible), but holy hell this option is elegant as hell declaring wp_ajax_ handlers. Just throw the name of the handler function in and you're set:
private function __construct()
{
$this->addAjaxHandler('feature_post' )
->addAjaxHandler('unfeature_post');
}
private function addAjaxHandler( $function_name ){
I'm a software developer who writes about Laravel, JavaScript, Rails, Linux, Docker, WordPress and the tech industry. Follow me on Twitter @tylerlwsmith
Yeah it works in this case since wp_ajax follows a standard naming convention with your function and PHP will natively try to execute a variable as a function if you put the () parens after, so declaring the function name pulls double duty here.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I really liked this post and played around with changing my construction process around it. Ultimately I didn't want to invest the time to properly figure out how to tackle filters or large $arg functions (I'm sure it's possible), but holy hell this option is elegant as hell declaring wp_ajax_ handlers. Just throw the name of the handler function in and you're set:
private function __construct()
{
$this->addAjaxHandler('feature_post' )
->addAjaxHandler('unfeature_post');
}
private function addAjaxHandler( $function_name ){
\add_action( 'wp_ajax_' . $function_name, function() use( $function_name){
self::$function_name();
} );
return $this;
}
I'm glad you got some value out of the post! I hadn't even considered using this for ajax, but I'll have to try it out at some point.
Yeah it works in this case since wp_ajax follows a standard naming convention with your function and PHP will natively try to execute a variable as a function if you put the () parens after, so declaring the function name pulls double duty here.