• sloanthrasher

    (@sloanthrasher)


    I’m developing an app for the iPhone (eventually Android too) that will need to validate the user, and use Ajax to send and receive data from my WordPress site.

    In the handlers for the Ajax calls, there is a parameter for user ID, which defaults to the currently logged-in user. So far, I understand how to get things working with the front-end part of the site, but will soon be working on getting the app working.

    A few questions to get me started on the right path:

    • How do I handle having the app login to the site via Ajax?
    • Will the site remember/track the user that is logged in via the app? Or will I have to repeat the login credentials with each Ajax call?
    • Do I need to have the app do regularly timed keep-alive calls to the server?

    I don’t have any code to share yet, of course, but I need to know where to start on the app side of things. I have the code for the Ajax PHP part done, and working with the pages on the website. I’m not asking for help with specific code in the phone app, just what it will need to do in communicating with the serve.

    Thanks for any help and pointers you may have!

    • This topic was modified 2 months ago by sloanthrasher. Reason: Clarify what code I need help with and what I don't need help with
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator threadi

    (@threadi)

    I would also recommend using REST API rather than AJAX. WordPress has a lot of information on this in its developer manual: https://developer.wordpress.org/rest-api/

    Moderator bcworkz

    (@bcworkz)

    There are actually a couple authentication methods available for this situation, OAuth or JWT. I personally prefer JWT, but I suppose preference depends upon what you’re used to coding for.

    iamcallen

    (@iamcallen)

    AJAX isn’t really ideal for mobile app <-> WordPress communication, since AJAX relies on session cookies that aren’t persistent outside a browser. That’s why the REST API + token-based auth (JWT or OAuth) is the better long-term solution.

    Build your app’s data endpoints using the WordPress REST API, not admin-ajax.php.
    REST is designed for external apps and handles authentication much more cleanly.
    Use fetch or axios in the app for network requests.
    Securely store and handle tokens in the app.

    If you ever need extra validation or user-specific logic, you can protect your REST routes using WordPress’s built-in authentication hooks.

    For a mobile app, you’ll want to use the WordPress REST API with authentication via Application Passwords (or JWT tokens). This approach sends authentication credentials with each request rather than relying on cookies/sessions like a browser would. Check out the REST API Authentication documentation at https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/ to get started with the right approach for mobile apps.

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

You must be logged in to reply to this topic.