Please help. I can’t find a solution for this case.
An item needs to be displayed based on a user meta value. This works fine: if the value is in the array the content shows, if the value is not in the array it doesn’t. But some users may not have the meta value set (array doesn’t exist), other users are just visitors and not logged in.
Both cases throw the error:
Fatal error: Uncaught TypeError: array_intersect(): Argument #1 must be of type array, bool given...
I understand what causes the error, but can’t figure out how to solve it. Adding a user role condition with ‘condition type = all’ does not solve this problem.
With respect to your query here, kindly share with us the actual context of this error based on your site. Also please share with us replication steps to this error.
You can use Code Snippets to import this snippet: dropbox link. Import the snippet in Code snippets and activate. It adds a very basic option page in the WordPress admin (Settings->Save User Meta) with a button to add example user meta for all users.
Click the button ‘Update User Meta’ to add the user meta key.
(optionally you could use JSM’s Show User Metadata to verify that the key has been added to all user profiles, see image below. This plugin also makes it easy to remove the meta key: red icon)
Then create a page in Elementor, add any element and add these Visibility logic settings:
Verify that these settings work as intended by visiting the page as a logged-in user. The element should show. If you change ‘value_1’ to anything that’s not in the meta key (e.g. ‘value_4’) the element does not show.
Now for the error: visit the same page in incognito mode (or logged-out) to reproduce. The same error happens if you now remove the meta key with JSM’s Show User Metadata or for any new user before you add the key with my snippet. It happens every time that the user meta key doesn’t exist, which from a technical viewpoint is to be expected.
But with Visibility Logic I can’t figure out how to add a check to first verify if the user is logged-in, or to check if the key exists.
Conclusion: I need a way to use the User meta option of Visibility Logic, without breaking that page for logged-out users. In that case the condition result should be equal to ‘false’ (element not visible).
Maybe this can be a feature request: an extra option in the User Meta section of Visibility Logic that determines how to handle a non-existing meta key.
Option:
When the user meta does not exist (e.g. for guest visitors) the result of the condition is FALSE:
-> Toggle (ON/off)
I need a solution sooner though (unless you can release such a feature fast π
In this case, the screenshots you embedded cannot be previewed. I did however try out the steps to replicate the issue and did surely come across a fatal error. I will notify the lead team about this so that this is fixed. In the meantime, please navigate to the visibility-logic-elementor\core\settings\UserMetaVisibility.php and on line 327, alter the code in place to:
This has solved the issue for users that are not logged in.
The same error still happens when a user is logged in but the meta key doesn’t exist for that user. In my case I can write my code so that this shouldn’t happen, but maybe the team should consider adding a check for that case too.
I think that Visibility Logic should handle that case as if the array would exist but the value is not in it.
Thanks again for the help!
This reply was modified 3 years, 2 months ago by jleenman. Reason: marked as solved