$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'); ?>hooks - add_action('wp_ajax_[action name]', myfunction) problem|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)

hooks - add_action('wp_ajax_[action name]', myfunction) problem

matteradmin9PV0评论

I'm trying to integrate ajax in wordpress using the wp codex guidelines. In the PHP I added:

wp_enqueue_script ( 'my_ajax', ADMIN_URL . 'js/ajax.js','jquery','1.0.0' );
wp_localize_script( 'my_ajax', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

The ajax code is:

jQuery.ajax({url: MyAjax.ajaxurl, type: "POST",data: {action: 'myaction',postID : MyAjax.postID}, success: function(response) {
 alert('Got this from the server: ' + response);
}});

and the PHP function that should be called by ajax is:

function ajax_function() {
...do something...
}
add_action('wp_ajax_myaction', 'ajax_function');
add_action('wp_ajax_admin_myaction', 'ajax_function');

The ajax call is successful (the "alert" works), however, the php function "ajax_function" is never called. After doing some debugging I realized that even though the action call add_action('wp_ajax_ sets a new element in the global $wp_filter array, when the corresponding do_action runs inside admin-ajax.php, the $wp_filter array no longer contains that element.

Therefore, "ajax_function" function is ignored. Any idea why the function is not called?

I'm trying to integrate ajax in wordpress using the wp codex guidelines. In the PHP I added:

wp_enqueue_script ( 'my_ajax', ADMIN_URL . 'js/ajax.js','jquery','1.0.0' );
wp_localize_script( 'my_ajax', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

The ajax code is:

jQuery.ajax({url: MyAjax.ajaxurl, type: "POST",data: {action: 'myaction',postID : MyAjax.postID}, success: function(response) {
 alert('Got this from the server: ' + response);
}});

and the PHP function that should be called by ajax is:

function ajax_function() {
...do something...
}
add_action('wp_ajax_myaction', 'ajax_function');
add_action('wp_ajax_admin_myaction', 'ajax_function');

The ajax call is successful (the "alert" works), however, the php function "ajax_function" is never called. After doing some debugging I realized that even though the action call add_action('wp_ajax_ sets a new element in the global $wp_filter array, when the corresponding do_action runs inside admin-ajax.php, the $wp_filter array no longer contains that element.

Therefore, "ajax_function" function is ignored. Any idea why the function is not called?

Share Improve this question asked Dec 1, 2010 at 11:58 user1567user1567 4972 gold badges6 silver badges11 bronze badges 2
  • @user1567: You add the function to the wp_ajax_myaction and wp_ajax_admin_myaction hooks. Should that not be wp_ajax_myaction (for logged in users) and wp_ajax_nopriv_myaction (for anonymous users)? – Jan Fabry Commented Dec 1, 2010 at 13:52
  • yes I fixed it, thanks. By the way, I have an update_option call inside the "ajax_function" that seem to fail because it is called after all wordpress loading sequence is complete? Is there another way to update_option? – user1567 Commented Dec 1, 2010 at 16:26
Add a comment  | 

2 Answers 2

Reset to default 8

In my projects I do it like that

PHP

function foo() {
    echo 'bar';
}
add_action('wp_ajax_foo', 'foo' ); // executed when logged in
add_action('wp_ajax_nopriv_foo', 'foo' ); // executed when logged out

Javascript

data = { action: 'foo', avalue: 'some value', 'anothervalue': 'another value' };
jQuery.post(ajaxurl, data, function(response){
    alert(response);
});

Please follow the code:

add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' );
add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' );

function prefix_ajax_add_myfunc() {
    // Handle request then generate response using WP_Ajax_Response
}

and in your ajax call do this:

jQuery.post(
    ajaxurl, 
    {
        'action': 'add_myfunc',
        'data':   'foobarid'
    }, 
    function(response){
        alert('The server responded: ' + response);
    }
);

in the ajax call you'll call your function without prefix_ajax_. Only call by it's remaining. In that case it's add_myfunc. In the response it will send done if everything goes right. Else response will be 0 or -1.

Hope it will help. Thank you.

Post a comment

comment list (0)

  1. No comments so far