I have a slight problem when making a request via ajax in a wordpress plugin.

Here is the server side:

add_action('admin_enqueue_scripts', 'my_enque_post');

    function my_enque_post()
        wp_register_script('PostList', RADIOWEB__PLUGIN_URL . "/js/postlist.js", array(
        $title_nonce = wp_create_nonce('title_example');
        wp_localize_script('PostList', 'modallist', array(
            'ajaxurl' => admin_url('admin-ajax.php') ,
            'nonce' => $title_nonce

    add_action('wp_ajax_nopriv_get-list-post', 'get_list_post');
    add_action('wp_ajax_ajax_obter-list-post', 'get_list_post');

    function get_list_post()
        $teste = $_POST['teste'];
        echo $teste.'34' ;

As we can see this script points to the following:

jQuery(document).ready(function($) {
    function queryPostsTable() {
            type: "POST",
            url: modallist.ajax_url,
            _ajax_nonce: modallist.nonce,
             action: 'get_list_post',
             teste: 'teste12',            
            success: function(response) {

    $("#programa-search").change(function() {




When I run the script the response variable returns me the entire html of the plugin's adm page.

enter image description here

but this is not what I expect to happen

My goal is to perform the function:  

get_list_post ();

Which captures the post-test variable and echoes it.

My goal is to do operations within this function: get_list_post ();

and return what is echoed. My expectation in executing this code is to return in console.log: teste1234 Can anyone please give me a hint on how to achieve this result?

  • Is there a reason you aren't using the built in REST API? An endpoint for retrieving posts already exists, and it's a much more straightforward API, easier to secure, etc – Tom J Nowell Nov 27 '17 at 23:49
  • Also should wp_ajax_ajax_obter-list-post not be wp_ajax_get-list-post? – Tom J Nowell Nov 27 '17 at 23:51
  • Because i dont have time to learn about API. But thank for helpe me – Atila Silva Nov 28 '17 at 19:37

Your AJAX URL varaible is different in your PHP and your JS. In your JS you refer to ajax_url:

url: modallist.ajax_url,

But the variable defined in PHP is ajaxurl:

'ajaxurl' => admin_url('admin-ajax.php') ,

This is why you're getting the full admin page as a response. Your request is going to the current page because there isn't a real URL.

Your other problem is that the action for your hooks is incorrect. Your hooks are:

add_action('wp_ajax_nopriv_get-list-post', 'get_list_post');
add_action('wp_ajax_ajax_obter-list-post', 'get_list_post');

First of all, they're different. They should be the same but one has no_priv. The second problem is that they don't match the action name in JS. In JS you've set the action to:

action: 'get_list_post',

So your hooks should be:

add_action('wp_ajax_nopriv_get_list_post', 'get_list_post');
add_action('wp_ajax_get_list_post', 'get_list_post');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.