• Hello,

    I have built a simple plugin which is related to redeeming the points at checkout.I have a form to enter the points that the logged in customer want to spend. On entering, the wallet has to be checked for enough balance. The problem here is,I have used ajax url to redirect to a specific page of the same plugin where required calculation will be done.The control goes to the page but in order to get the current user I have used wp_get_current_user() which gives error stating call to undefined function wp_get_current_user(). How to solve this. The same wp_get_current_user() is used in some other page of the plugin where it works.

    • This topic was modified 7 years, 9 months ago by Ash.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hey,

    Maybe the pluggable.php file where the wp_get_current_user is defined is not yet loaded. Have you tried to wrap up the code where the function is called, like this:

    add_action('init','plugin_code');
    function plugin_code(){
      $current_user = wp_get_current_user();
    }
    

    and see if things change?

    Moderator bcworkz

    (@bcworkz)

    What is the Ajax URL you used? In WP, it must lead to /wp-admin/admin-ajax.php. This also requires an action value be passed, which is used to compose a custom action that your Ajax handler hooks. When the request comes in, admin-ajax.php will call the function you hooked to that action.

    While the current user will then be available when doing Ajax that way, I would instead pass the current user’s ID as part of the form data. (Place it in a hidden field) The ID should be all you need to retrieve their points, you don’t really need the user object returned by wp_get_current_user().

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Call to undefine function wp_get_current_user()’ is closed to new replies.