Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Last active May 12, 2021 12:53
Show Gist options
  • Save obiPlabon/224ec62815fb941784decccf655e10bc to your computer and use it in GitHub Desktop.
Save obiPlabon/224ec62815fb941784decccf655e10bc to your computer and use it in GitHub Desktop.
Register ajax action hook.
<?php
/**
* Register ajax action hook.
*
* When you have lots of ajax actions in your theme or plugin then
* this utility function is going to be quite handy!
* By default all actions are for logged in users.
*
* Usage:
* add_ajax( 'get_infinity_posts', 'prefix_get_infinity_posts' ); // for logged in only
* add_ajax( 'get_infinity_posts', 'prefix_get_infinity_posts', true ); // for both
* add_ajax( 'get_infinity_posts', 'prefix_get_infinity_posts', true, false ); // for logged out only
*
* @param string $action
* @param callable $callback
* @param bool $nopriv
* @param bool $priv
*
* @return void
*/
function add_ajax( $action, callable $callback, $nopriv = false, $priv = true ) {
$prefix = '_action_prefix_'; // usualy this is going to be your plugin slug
if ( $priv ) {
add_action( 'wp_ajax' . $prefix . $action, $callback );
}
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv' . $prefix . $action, $callback );
}
}
<?php
/**
* Register ajax action hook.
*
* When you have lots of ajax actions in your theme or plugin then
* this utility function is going to be quite handy!
*
* @param string $action
* @param callable $callback
* @param bool $nopriv
*
* @return void
*/
function prefix_add_ajax( $action, $callback, $nopriv = false ) {
$prefix = '_action_prefix_'; // usualy this is going to be your plugin slug
add_action( 'wp_ajax' . $prefix . $action, $callback );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv' . $prefix . $action, $callback );
}
}
<?php
/**
* Register ajax action hook.
*
* When you have lots of ajax actions in your theme or plugin then
* this utility function is going to be quite handy!
*
* @param string $action
* @param callable $callback
* @param bool $nopriv
*
* @return void
*/
if ( ! function_exists( 'add_ajax' ) {
function add_ajax( $action, $callback, $nopriv = false ) {
$prefix = '_action_prefix_'; // usualy this is going to be your plugin slug
add_action( 'wp_ajax' . $prefix . $action, $callback );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv' . $prefix . $action, $callback );
}
}
}
<?php
/**
* Register ajax action hook.
*
* When you have lots of ajax actions in your theme or plugin then
* this utility function is going to be quite handy!
*
* @param string $action
* @param callable $callback
* @param bool $nopriv
*
* @return void
*/
function add_ajax( $action, $callback, $nopriv = false ) {
$prefix = '_action_prefix_'; // usualy this is going to be your plugin slug
add_action( 'wp_ajax' . $prefix . $action, $callback );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv' . $prefix . $action, $callback );
}
}
<?php
function prefix_get_infinity_posts() {
// process and return the response
}
add_ajax( 'get_infinity_posts', 'prefix_get_infinity_posts', true );
// and this will do the following
// add_action( 'wp_ajax_prefix_get_infinity_posts', 'prefix_get_infinity_posts' );
// add_action( 'wp_ajax_nopriv_prefix_get_infinity_posts', 'prefix_get_infinity_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment