Forums › Forums › Search & Filter Pro › User state lost on page updates, filtering and paging
- This topic has 9 replies, 3 voices, and was last updated 5 years, 3 months ago by Ross.
-
Anonymous(Private) July 16, 2019 at 6:40 pm #216474
I have a custom template that displays different fields depending on whether the user has a “Subscriber” role. This template works correctly on the initial page load, and has a category dropdown to filter on, but on all subsequent calls (either ajax enabled or disabled with a Submit button).
The filters work as expected displaying the appropriate records, but no $user state is being received on the page updates when clicking on the next/previous page or selecting a new category from the filter’s dropdown.
If I go back to the original URL and load it again, it works as expected and the $user data is present.
How do I get the $user data to be available to the template in subsequent calls?
Trevor(Private) July 21, 2019 at 1:22 pm #216812As an aside, looking at the form setup, this will not be needed, as (Post) Categories do not apply to the Startups Post Type:
https://www.screencast.com/t/PG7Sh251lGB
In your code, you are using the
get_currentuserinfo()
But that has been deprecated in WordPress for quite some time. You should use
wp_get_current_user()
instead. This may help to re-factor the code:
https://pluginrepublic.com/get-current-user-role-in-wordpress/
Anonymous(Private) July 21, 2019 at 7:12 pm #216833I refactored the code to not use that, it now is:
<?php if ( current_user_can('subscriber') ){ $subscriber = TRUE; echo "<br><br><br><br><br><br><h1>UserID " . get_current_user_id() . " IS SUBSCRIBER</h1>"; } else { $subscriber = FALSE; echo "<br><br><br><br><br><br><h1>UserID " . get_current_user_id() . " IS NOT A SUBSCRIBER</h1>"; } get_header(); ?>
However, it still does not work. I added code to show the current userID in the template, and the system forgets the UserID when clicking on any of the pager/filter links.
For example, if you go to the /startups page it shows at the top:
UserID 15 IS SUBSCRIBER
but if you click the NEXT link for page 2, it then shows at the top:
UserID 0 IS NOT A SUBSCRIBER
showing that it has lost the user state (now user 0 instead of the actual User 15), causing the template to display a non-logged in results page.
It seems like there is something wrong in your ajax call where it is losing the user state for the session.
Anonymous(Private) July 24, 2019 at 7:28 pm #217154I tested the site with adding random variables to the query string and it did NOT show the user as logged out. Screen recording of the test here:
https://www.loom.com/share/75f0511697064f118f5cb0f3670f53e3
You may have seen a cached page when you did your test, but if you hard-reload the page or go to the URL directly as shown in the video, it does in fact preserve the user state.
I didn’t recreate the filters in the video (somebody else set it up originally) but it seems to be working properly except for the S&F user issue.
If you want to work through this together, you can reach me at 1-443-910-3677 (cell/text) and we can debug in real-time. If you want to give me a date/time we can discuss, I’ll try to make myself available so we can get this resolved.
Do you have this working on other sites where the data is rendered varying based on the logged in user, or is this a new thing we’re trying to do?
Thanks,
– Brian
Ross Moderator(Private) July 29, 2019 at 3:46 pm #217459Hi Brian
I think I see the issue.
When you search, the page goes to a
http
version of the site, where you are no longer logged in.This is because in your
Results URL
you have put a URL with justhttp://
This then sends you to a logged out version of the site, where the user ID can only be
0
Because you hide the admin bar I don’t think you noticed you were looking at a logged out version of the site?
Updating the Results URL to use
https
should do the trick.Thanks
-
AuthorPosts