Support

Home Forums Event Espresso Premium How to get events to which the logged in user is subscribed to by the Rest API

How to get events to which the logged in user is subscribed to by the Rest API

Posted: March 19, 2018 at 2:51 pm

Viewing 4 reply threads


rcouto

March 19, 2018 at 2:51 pm

I’m a beginner developer, and I’m developing a mobile app in which subscribing users can view the events in which they sign up and download their tickets through the app.
I already use a plugin for authentication for wordpress.
I need to actually know what urls I should use to get json with user logged events.
From what I checked in the documentation, I could use the following url: http: // localhost / {my_url_site} /wp-json/ee/v4.8.36/attendees?where [WP_User.ID] = {user_ID_logged}, to bring the participant related to the logged in user. Then I would use: http: // localhost / {my_url_site} /wp-json/ee/v4.8.36/attendees/ {attendee_ID} / events, to bring in all user events. It would be exactly what I would need, but when I look for the first URL, I get an empty json: “[]” only.

My Settings:
WordPress – version: 4.9.2;
Event Espresso – version: 4.9.54.rc.040;

Awaiting response.


Josh

  • Support Staff

March 19, 2018 at 3:57 pm

Hi there,

One thing to check is make sure you have a valid attendee ID. You can go to Event Espresso > Registrations > Contact List, then check what’s listed there to see if there’s an ID that matches the attendee ID.

The other thing to check is whether the request was authenticated. Other routes like
/wp-json/ee/v4.8.36/attendees
and
/wp-json/ee/v4.8.36/registrations
with no additional params also require authentication, whereas requests to routes like these:
/wp-json/ee/v4.8.36/events
and
/wp-json/ee/v4.8.36/venues
do not require authentication.


rcouto

March 21, 2018 at 2:13 pm

Hello Josh,

I checked and there is yes a participant with a valid ID listed. Which I can edit and view that has a wordpress user profile linked to the participant.

But when I try to access it by request, filtering by the user (which is the first information I can get), no participant is returned, only an empty list.

And it’s not a permission issue, since I use the “JWT Authentication for WP-API” plugin for authentication, passing the user token in the request header. I know some information is public and some is not.

You see, I have an application in which a non-admin user needs to view their enrollments. So for this, I had to enable viewing permissions for non-admin users with the “Members” plugin. But the question is that when searching for ‘/wp-json/ee/v4.8.36/attendees’, the answer brings me all the participants, being that I want only the participant linked to the logged in user.

I also noticed that when you filter by ‘/wp-json/ee/v4.8.36/attendees?where [WP_User.ID] = {admin_ID}’, all participants are returned in response. And when I filter by ‘/wp-json/ee/v4.8.36/attendees?where [WP_User.ID] = {ID_of_my_user}’, it always returns an empty list. That is, all participants are bound by default to the admin user.

Is this normal operation?
If so, how would you search for non-admin user registrations?


Josh

  • Support Staff

March 21, 2018 at 6:16 pm

Let’s step back a bit because you don’t actually get events or registrations from a request to /wp-json/ee/v4.8.36/attendees, that particular route returns a list of attendees.

The route that lists registrations is:
/wp-json/ee/v4.8.36/registrations
and you can get the name of the associated event with each registration.

If you only have the User ID, to get a list of registrations for the user, you’ll have to do two API endpoint queries:
The first query to usermeta using the WP rest api to get the Attendee ID, and then the second with that Attendee ID to the EE REST API.

Another step back: WordPress does not expose meta to the REST API by default, so you’ll need to add a little function like this to your site’s functions plugin to expose the user meta field for Event Espresso’s Attendee ID:

add_action('rest_api_init', 'my_register_ee_attendee_id_meta');
function my_register_ee_attendee_id_meta() {
    global $wpdb;
    $args = array(
        'type'         => 'integer',
        'single'       => true, 
        'show_in_rest' => true
    );
    register_meta(
        'user', 
        $wpdb->prefix .'EE_Attendee_ID', 
        $args
    );
}

Once that’s ready, your first query will go like this:

/wp-json/wp/v2/users/{user_id}?_fields=meta

which will include the EE_Attendee_ID in the response. Then in the second query do this:

/wp-json/ee/v4.8.36/registrations?where[Attendee.ATT_ID]={EE_Attendee_ID}&include=Event.EVT_name


rcouto

March 22, 2018 at 10:48 pm

Perfect. Exactly what I needed.
Good job Josh !!!
I noticed that when you get Attendee_ID, you can get your related events as follows too: wp-json/ee/v4.8.36/attendees/{EE_Attendee_ID}/events
Thanks!

Viewing 4 reply threads

The support post ‘How to get events to which the logged in user is subscribed to by the Rest API’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso