title: Get user name inside AJAX success function
body: <p>I'm trying to display the username when an AJAX request is successful. 
  The problem that I'm running into is that the name remains blank.</p>
  
  <p>Simplified code (removed if statements, checks, and other unrelated code)</p>
  
  <pre><code>wp_localize_script('follow-me-ajax', 'ajax_setting', array(
      'ajax_url'              =&gt; admin_url('admin-ajax.php'),
      'ajax_nonce'            =&gt; wp_create_nonce('km-ajax-create-nonce'),
      'ajax_follow_success'   =&gt; $this-&gt;follow_me_success(), 
  ));
  </code></pre>
  
  <p>AJAX Call</p>
  
  <pre><code>var user_to_follow      = $('.km-author-follow a.km-meta-badge').attr('id');
  
      $.ajax( {
          url : ajax_setting.ajax_url,
          type : 'post', 
          data: {  
              action                      : 'theme_ajax_follow_me',
              security                    : ajax_setting.ajax_nonce,
              'data-follow-user'          : user_to_follow,                       
          },  
  
          success: function( data ) {
              $('.km-follow-me').html( ajax_setting.ajax_follow_success ).hide().fadeIn( 'slow' );
               console.log( user_to_follow );
          },
  
      } )
  </code></pre>
  
  <p>Inside the wp ajax function I use this</p>
  
  <pre><code>public function theme_addon_ajax_follow_me() {
  
          ... 
  
          $target_user    = isset( $_POST['data-follow-user'] ) ? $_POST['data-follow-user'] : false;
  
          if( ! empty( $_POST['data-follow-user'] ) ) {           
              $this-&gt;kiwi_follow_user( $current_user, $target_user );
          }   
  
          wp_die();   
  
      }
  </code></pre>
  
  <p>Up next; this function sends the <code>$target_user ID</code> to <code>km_follow_me_author_name</code></p>
  
  <pre><code>public function kiwi_follow_user( $current_user = 0, $user_to_follow = 0  ) {
  
          ...     
  
          $args = array(
              'user_id'   =&gt; $current_user,
              'follow_to' =&gt; $user_to_follow
          );
  
          $response_success = $this-&gt;km_follow_me_author_name( $args );
  
      }
  </code></pre>
  
  <p>Function to grab display name based on AJAX's <code>$_POST['data-follow-user']</code></p>
  
  <pre><code>public function km_follow_me_author_name( $args = array() ) {
  
          $author_info    = get_userdata( $args['follow_to'] );
  
          $author = $author_info-&gt;display_name;           
  
          return $author;
  
      }
  </code></pre>
  
  <p>The success message and where it goes wrong. <code>$name</code> remains blank.</p>
  
  <pre><code>public function follow_me_success() {
  
          $name       = $this-&gt;km_follow_me_author_name();
  
          $content    = sprintf( esc_html__( 'You\'re now following %s.', 'theme' ), $name );
  
          return $content;
  
      }
  </code></pre>
  
  <p>Any help is much appreciated.   </p>