• 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.

    my javascript function is this:

    function show_players_page(id)
    {
      jQuery()
      {
        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 )
           {
             alert(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 );
    
      sleep(3);
    
      $url = get_bloginfo('url') . '/?p=' . $post_id;
    
      bbnuke_update_option( 'bbnuke_ajax_post_url' , $url );
    
      exit;
    }

    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?

    the response only contains “success: true”.

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter christian_gnoth

    (@christian_gnoth)

    has anyone an idea how to user the WP_Ajax_Response class? there is no documentation about this class.

    Thread Starter christian_gnoth

    (@christian_gnoth)

    I am getting now always a response code of 0 from the server – no other info – how i can find out what is the problem?

    Thread Starter christian_gnoth

    (@christian_gnoth)

    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();
    
      die($url);

    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.

    put die() at the end of your php script

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘return data from ajax call’ is closed to new replies.