Opened 8 years ago
Last modified 4 weeks ago
#39295 new enhancement
Prevent infinite loop when calling get_user_locale() in a 'locale' filter
Reported by: | yoavf | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | major | Version: | 4.7 |
Component: | I18N | Keywords: | has-patch 2nd-opinion |
Focuses: | Cc: |
Description
Using get_user_locale()
in a locale
filter (for get_locale()
) might sound like a good idea, for example to change the site locale depending on our user.
<?php if ( $condition) { add_filter( 'locale', 'get_user_locale' ); }
However, if one does that and a non-logged in user (or a user without a locale
value) visits the site, a infinite loop will be triggered, since get_user_locale()
will itself call get_locale()
when no user->locale
value exists.
Since this only affects logged out users, it feels like an easy trap to miss, and we should prevent that.
Attachments (3)
Change History (7)
This ticket was mentioned in Slack in #core by justinahinon. View the logs.
4 years ago
This ticket was mentioned in Slack in #core by nourma. View the logs.
4 years ago
Note: See
TracTickets for help on using
tickets.
39295.2.diff is a refresh that follows the approach of 39295.diff with some improvements:
get_locale()
.39295.2.alt.diff is an alternative patch that just makes
add_filter( 'locale', 'get_user_locale' )
work as expected, without the_doing_it_wrong()
message, by using the existing$locale
global as a fallback to avoid the infinite loop. Perhaps that could be the preferred fix here?