Skip to content

Instantly share code, notes, and snippets.

@mmarj
Forked from obiPlabon/add-ajax-extended.php
Created May 11, 2021 18:46
Show Gist options
  • Save mmarj/4e0d34c1763d1518e6ca213ddf3c1c2b to your computer and use it in GitHub Desktop.
Save mmarj/4e0d34c1763d1518e6ca213ddf3c1c2b 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