$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Why, if a function accepts arguments, it fails on ajax calls?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Why, if a function accepts arguments, it fails on ajax calls?

matteradmin8PV0评论

I am trying to write a WP function to work with both ajax and direct calls, something like this:

PHP

function some_function($var){
    $var = !empty($var) ? $var : $_POST['var'];
    echo $var;
    
    //or even

    $var = null;
    echo 'something';

    if(!empty($_POST['action'])) wp_die();
}

AJAX CALL

let ajaxurl = '███';
let data = {'action': 'somefunction','var':'foo')};
$.post(ajaxurl, data, function(response) {console.log(response);});

WP use

add_action( 'wp_ajax_somefunction', 'some_function',10,1);
add_action( 'wp_ajax_nopriv_somefunction', 'some_function',10,1);

Another WP use

some_function('bar');

However, any time I place $var as an accepted function argument, some_function($var), my ajax calls start returning a 500 error. So, something like this

function some_function(){
    $var = !empty($var) ? $var : $_POST['var'];
    echo $var;
}

works for ajax.

I tried looking up wp ajax & arguments, but the search results are always about the variables we pass through ajax, not the callback function arguments. The only thing I learned is that we have to add a number of accepted arguments into add_action()

What am I doing wrong?

Thank you.

...P.S. I found a funny workaround:

function some_function_ajax(){
    $var = $_POST['var'];
    some_function($var);
}
function some_function($var){
    echo $var;
} // =)

but still, what is the right way?


Jackob Peattie, thanks for the hint! So, the working code looks like this, then:

//JS (jQuery):
let data = {
   'action' : 'func',
   'var' : 'something',
   'nonce' : nonce
};
$.post(ajaxurl, data);

//add_action:
add_action( 'wp_ajax_func', 'func_ajax');
add_action( 'wp_ajax_nopriv_func', 'func_ajax');

//PHP:
function func_ajax(){
   //verify nonce
   if(empty($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'myajax-nonce' ) ) wp_die();

    $var = $_POST['var'];
    func($var);

   //end ajax call
   wp_die();
}

function func($var){
    echo $var;
}

I am trying to write a WP function to work with both ajax and direct calls, something like this:

PHP

function some_function($var){
    $var = !empty($var) ? $var : $_POST['var'];
    echo $var;
    
    //or even

    $var = null;
    echo 'something';

    if(!empty($_POST['action'])) wp_die();
}

AJAX CALL

let ajaxurl = '███';
let data = {'action': 'somefunction','var':'foo')};
$.post(ajaxurl, data, function(response) {console.log(response);});

WP use

add_action( 'wp_ajax_somefunction', 'some_function',10,1);
add_action( 'wp_ajax_nopriv_somefunction', 'some_function',10,1);

Another WP use

some_function('bar');

However, any time I place $var as an accepted function argument, some_function($var), my ajax calls start returning a 500 error. So, something like this

function some_function(){
    $var = !empty($var) ? $var : $_POST['var'];
    echo $var;
}

works for ajax.

I tried looking up wp ajax & arguments, but the search results are always about the variables we pass through ajax, not the callback function arguments. The only thing I learned is that we have to add a number of accepted arguments into add_action()

What am I doing wrong?

Thank you.

...P.S. I found a funny workaround:

function some_function_ajax(){
    $var = $_POST['var'];
    some_function($var);
}
function some_function($var){
    echo $var;
} // =)

but still, what is the right way?


Jackob Peattie, thanks for the hint! So, the working code looks like this, then:

//JS (jQuery):
let data = {
   'action' : 'func',
   'var' : 'something',
   'nonce' : nonce
};
$.post(ajaxurl, data);

//add_action:
add_action( 'wp_ajax_func', 'func_ajax');
add_action( 'wp_ajax_nopriv_func', 'func_ajax');

//PHP:
function func_ajax(){
   //verify nonce
   if(empty($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'myajax-nonce' ) ) wp_die();

    $var = $_POST['var'];
    func($var);

   //end ajax call
   wp_die();
}

function func($var){
    echo $var;
}
Share Improve this question edited Jun 26, 2020 at 5:54 Artem asked Jun 26, 2020 at 4:37 ArtemArtem 3152 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

No arguments are passed to the AJAX callback function. Variables passed with the request are available in $_GET or $_POST. So if your function expects an argument, it can’t be used as the hooked callback directly.

Your workaround is the correct way to use a function that accepts arguments in an AJAX request.

Post a comment

comment list (0)

  1. No comments so far