Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7716513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:39:22+00:00 2026-06-01T02:39:22+00:00

I posted this earlier on wordpress.stackexchange.com. However, never got a reply. Hence, trying my

  • 0

I posted this earlier on wordpress.stackexchange.com. However, never got a reply. Hence, trying my luck here.

I am hereby providing a detailed description of what I need and what I have done for this issue of mine. I am open to any workable solution around what I have done or maybe new suggestions.

I need to make use of user data that is retrieved using the following:

$user_data = get_user_by('login', get_query_var('user_login'));

The above code uses the username passed as a query_var in the URL. All works until here.

I make use of the above code in several Ajax callbacks (handled by admin-ajax.php) on single page load. Since, the site is targeted as a high volume site. All these Ajax requests lead to several database query for the same data. So the obvious idea to save some database queries is to pass the data to a global variable like below:

$_GLOBALS['user_data'] = get_user_by('login', get_query_var('user_login'));

And then use the same in the Ajax callbacks. Here’s problem. None of the Ajax callback functions see the global $user_data variable. Before you ask, yes I have declared the global inside callback as well.

So, the obvious answer would be: why not use wp_localize_script and pass the $user_data to the Ajax callback via javascript like bellow:

In PHP:

wp_localize_script('jquery', 'ajaxVars', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'user_data' => $user_data));

In Javascript:

        jQuery.ajax({
        url: ajaxVars.ajaxurl,   
        type:'POST',
        async: false,
        cache: false,
        timeout: 10000,
        data: 'action=ajax_callback&user_data=' + ajaxVars.user_data,
        success: function(value) {
            alert(value);
        },
        error: function() {
            alert(error);
        }
    });

However, this poses two questions:

Can an object that get_user_by('login', get_query_var('user_login')); returns be handled by wp_localize_script()?

If the answer to above question is yes, then would it not pose a security threat since the object would contain sensitive user information?

To overcome the global variable being not available to Ajax callbacks, I declared it directly in functions.php (without wrapping it inside a function). However, get_query_var('user_login') does not return any data when used directly inside functions.php making this futile exercise (You have to add it inside a function and call it via an action).

So, the question remains: how do I stop making $user_data = get_user_by('login', get_query_var('user_login')); calls for every Ajax request? Or is there a way I could get get_query_var('user_login') to work inside functions.php directly (without wrapping it inside a function) or a workaround?

Or maybe some completely new out of the box thinking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:39:23+00:00Added an answer on June 1, 2026 at 2:39 am

    All these Ajax requests lead to several database query for the same
    data. So the obvious idea to save some database queries is to pass the
    data to a global variable like below:

    $_GLOBALS['user_data'] = get_user_by('login', get_query_var('user_login'));
    

    And then use the same in the Ajax callbacks.

    Each request that your application receives, AJAX or otherwise, lives completely in isolation: the code handling the requests does not share any state between them (besides whatever is persisted to a database). A global (or constant, or property, or variable, or anything) you define in one request will never be available to subsequent requests unless you store it somewhere.

    There are a number of approaches to reducing the number of queries these requests are creating. One would be to retrieve the required user data on page load and pass it to subsequent requests. E.g.:

    var user = 'someUser';
    
    $.get('user-data.php?user=' + user, function(user_data) {
        $.ajax('some-endpoint.php', {
                 type: 'POST',
                 data: { user: user_data },
                 success: function() { /* ... */ }
        });
        $.ajax('some-other-endpoint.php', {
                 type: 'POST',
                 data: { user: user_data },
                 success: function() { /* ... */ }
        });
    });
    

    Alternatively, if it’s the currently logged in user you’re working with you can write their details to a JavaScript object on initial page load for use later.

    var userData = <?php get_currentuserinfo(); echo json_encode($current_user); ?>;
    

    Another option would be to ensure that the get_user_by results were being cached, either by WordPress, MySQL or some other caching layer. That way it doesn’t particularly matter how many times your code calls the method.

    In general if lots of your endpoints are sharing functionality, you could probably stand to refactor some of that code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a related question to one I posted earlier... I'm trying to sum
This simple problem is kiling me. I posted something earlier about trying to clean
I posted a question similar to this earlier, however, after thinking about it and
Hi I posted this earlier and got some help but still no working solution.
I've posted this question in gis.stackexchange earlier but I'm not sure if that was
I posted earlier about how to do this, and got some great replies, and
I had posted this earlier on Stack Overflow, but couldn't get a positive result.
Sorry guys, I posted this earlier today where my add as linked images were
I need to find the maximum route of a triangle. I earlier posted this
I posted a thread about this earlier and have made some progress but now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.