Make WordPress Core

Opened 5 years ago

Closed 4 weeks ago

#15291 closed defect (bug) (invalid)

receiving data with response from ajax call

Reported by: christian_gnoth Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0.1
Component: General Keywords:
Focuses: Cc:


i am using an ajax call in my plugin to create a new page with wp_insert_post and I would like to show this new post to the front-end user.

as described in the wp documentation I have in my plugin the following functions.

my javascript function is this:

function show_players_page(id)
    var data = {
             action: 'bbnuke_players_page',
             bbnuke_player_id: id

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post( ajaxurl, data,
       function( response )

my php function which is called is:

function  bbnuke_show_players_page()
  global $wpdb;

  $player_id = $_POST['bbnuke_player_id'];

  $user_id = get_current_user_id();
  if (!$user_id)
    $user_id = 1;

  //  create the page content
  $bbnuke_post_content =  bbnuke_widget_playerstats( $player_id, false );

  $bbnuke_post = array(
    'menu_order'     => 0,
    'comment_status' => 'closed',
    'ping_status'    => 'closed',
    'post_author'    => $user_id,
    'post_content'   => $bbnuke_post_content,
    'post_name'      => 'baseballnuke-players-page',
    'post_status'    => 'publish',
    'post_title'     => 'baseballNuke - Players Page' . $player_id,
    'post_type'      => 'page'

  $post_id = wp_insert_post( $bbnuke_post );


  $url = get_bloginfo('url') . '/?p=' . $post_id;

  bbnuke_update_option( 'bbnuke_ajax_post_url' , $url );


in my plugin file is this

add_action( 'wp_ajax_bbnuke_players_page', 'bbnuke_show_players_page');
add_action( 'wp_ajax_nopriv_bbnuke_players_page', 'bbnuke_show_players_page');

how I can send the create post_id back to the calling javascript function? in the docs is written the function has to be ended with 'exit' - not return.

the response only contains "success: true".

how i can set the responsetext or receive data from the server?

Change History (21)

comment:1 @christian_gnoth5 years ago

  • Cc christian.gnoth@… added

comment:2 @nacin5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Please try the support forums. We do not deal with support requests here.

comment:3 @christian_gnoth5 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

i have opened two threads for that problem - one in the plugin hacks and another in the theme dev forum - no answer.

independend of that is accordingly to the wordpress documentation described to exit the function - no return described.

so there is no possibility to return a response object or array - otherwise the documentation is wrong.

comment:4 @nacin5 years ago

  • Resolution set to invalid
  • Severity changed from major to normal
  • Status changed from reopened to closed

exit; is required to prevent anything else from being executed.

You need to decide how you want data to be returned, which is simply echo'd. Generally we use die('1') for success, die('0') for failure, die('-1') for insufficient permissions. Then you can check for the strings 1, 0, and -1.

Or, you could return the post ID, if that's what you need.

You can also return the data using json_encode(). Or, you can use the WP_Ajax_Response class.

You can also return JS directly to execute it, etc. It's up to you on how you wish to implement your ajax call.

comment:5 @filosofo5 years ago

Sorta related: #14618

comment:6 @christian_gnoth5 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Type changed from defect (bug) to feature request

please keep this ticket open. as there is no documentation about that I would like to have this ticket as a request to change the documentation.

There is no description how to use the WP_Ajax_Response class.

I am making an ajax call and I am getting now always a response code of 0 - no error message, no script error - nothing. even if i type a wrong function in the action only a 0 as result. if the function name is correct same.

comment:7 @scribu5 years ago

  • Cc scribu added
  • Component changed from General to Inline Docs
  • Milestone set to 3.1
  • Type changed from feature request to defect (bug)

comment:8 @scribu5 years ago

I think WP_Ajax_Response needs a refactoring or even a replacement, but in the meantime, some docs would be good.

comment:9 @christian_gnoth5 years ago

the ajax call is now working, but still the return of data is not working.

I have tried different versions of:

  $x = new WP_Ajax_Response( array(
              'what' => 'post_id',
              'id'   => $post_id,
              'data' => $url,
              'supplemental' => $url
          ) );
//  $x->send();


but I get an error message back that mysql_real_escape_string expects one parameter and on the end of that the parameter of die is appended.

i do not use mysql_real-escape in my ajax function and it tells it comes from wp-db.php line 785.

the variables of the wp_ajax_response are not available in the response function of the jquery.post call.

Last edited 5 weeks ago by SergeyBiryukov (previous) (diff)

comment:10 @christian_gnoth5 years ago

in my javascript ajax call i have the following jquery.post call:

  jQuery.post( ajaxurl, data, 
       function( return_data, response, xobj )
         bbnuke_result_page_url = response;

         alert('Server response: ' + response); 
         alert('Type of return data: ' + typeof(return_data)); 
         alert('Type of resposne: ' + typeof(response)); 
         alert('type of xobj: ' + typeof(xobj)); 

         for(key in return_data) 
           alert('Key is ' + [key] + ', Value is ' + data[key]);

         jQuery.each(xobj, function(key, value) 
           alert('Key: ' + key + ' Value: ' + value);

       }, "json"

in my ajax php function i return the following:

  $return_obj = array( 'post_id' => $post_id, 'url' => $url );

  die( $return_obj );

the return_obj is appended to the responseText item of the xobj variable. that is of no use.....

if i try to return data with

  $x = new WP_Ajax_Response( array(
              'what' => 'post_id',
              'id'   => $post_id,
              'data' => $url,
              'supplemental' => $url
          ) );

i do not get any answer.

actually I would like to change it back to a ticket as bug not only for the documentation as ti works not properly.

comment:11 @christian_gnoth5 years ago

  • Component changed from Inline Docs to General

comment:12 @christian_gnoth5 years ago

  • Component changed from General to Inline Docs

comment:13 @christian_gnoth5 years ago

  • Component changed from Inline Docs to General

I took a look in the admin-ajax.php - indeed the problem is not with wordpress as there is only the hook called which is defined in the plugin and then it dies with 0 or -1.

out of some reason my wp_insert_post in the ajax function creates a error message, that the mysql_real_escape function expects the parameter 1 as string in wp-includes/wp-db.php line 785.

And on this message the returned value from the die() function is appended.

with that it is difficult to work.

the whole string is not returned in the data variable of the success(data, responseText, XhttpRequestObj) callback function, but in the 'responseText' key of the XhttpRequestObj variable.

if I use the WP_Ajax_Response class I do not get any response from the jQuery.post call.

comment:14 @scribu5 years ago

How about not using WP_Ajax_Response at all?

comment:15 @azaozz5 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

I think you're missing some of the AJAX basics here. You make an AJAX call that connects to the WordPress back-end, then you need to intercept that call (the hook in admin-ajax.php works well for that), then you need to run your backend function(s), do whatever you need in php, echo a response and exit(). That response could be a simple string if you only need -1, 1, post_ID, etc. or it can be encoded and contain more data.

There are two popular methods of encoding more complex responses: json and xml. Json is native JavaScript and jQuery works with it directly, xml has to be parsed to get the actual values. WP_Ajax_Response and wp-ajax-response.js are used to encode/decode xml responses.

In any case there will be no response if your php function doesn't echo anything or doesn't use WP_Ajax_Response.

comment:16 @christian_gnoth5 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

thanks for the infos. it is working, but the problem is still there, that if some of the function creates an error message, that those error messages are together with the data i return in one string.

i too see that the problem is not with the admin-ajax.php file.

i return an json encoded string with die($return_data), but this return_data is together in one string with the error message the wp_insert_post creates.

i think it is a problem of jQuery.post and its implementation in wordpress.

i will try a ob_start, ob_end_clean around the wp_insert_post call, but actually each function in the ajax call could create some error messages.

intresting is, that the wp_insert_post is working - it creates, the post, creates the error message too and I return the error message with the post_id of the created post.

on the javascript side I extract the json string with my post_id from the whole string and show the created post to the user.

comment:17 @westi5 years ago

  • Milestone 3.1 deleted
  • Resolution set to invalid
  • Status changed from reopened to closed

This is not a support forum for your code - you would do better to ask in the forums or on the wp-hackers mailing list.

comment:18 @christian_gnoth5 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

it is working, so i do not need help. but please check if the implementation of jquery can be changed so that error messages are not together in one string with the returned data.

comment:19 @scribu5 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

PHP errors should be handled in PHP, not in jQuery.

I don't see a clean way to do that, except suppressing errors, which is missing the point.

Feel free to re-open if you have a patch.

comment:20 follow-up: @littlu_lulu5 weeks ago

  • Keywords changed from ajax,response to ajax response
  • Resolution invalid deleted
  • Status changed from closed to reopened


I know this thread is 5 years ago but I am encountering this issue right now. I have been trying to figure out why wp_insert_post is not returning. I have used that function in other plugins and its working. But the current plugin I'm working I used wp_ajax_ hook. The function that is attached to the hook is calling the wp_insert_post. The post is created but the function is not returning the postID. I have also posted my issue in the support forum. Hope someone can help me. Thanks in advance.

comment:21 in reply to: ↑ 20 @SergeyBiryukov4 weeks ago

  • Keywords ajax response removed
  • Resolution set to invalid
  • Status changed from reopened to closed

Replying to littlu_lulu:

I have also posted my issue in the support forum.

Please continue with your topic on the forums, you have some replies there:

Note: See TracTickets for help on using tickets.