• Resolved jleenman

    (@jleenman)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • john

    (@johnweru)

    Hi there,

    Thanks for reaching out to us.

    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.

    I look forward to your reply.

    Kind Regards,

    John

    Thread Starter jleenman

    (@jleenman)

    Hi John,

    The complete actual context of my site is quite complicated and not very useful to share here.

    To reproduce you need at least Elementor and Visibility Logic.

    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).

    Thread Starter jleenman

    (@jleenman)

    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 ??

    john

    (@johnweru)

    Hi there,

    Thanks for keeping in touch with us.

    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:

    if ( !is_user_logged_in() || empty( array_intersect( $user_meta, $values ) ) ) {

    I hope this helps.

    Kind Regards,

    John

    Thread Starter jleenman

    (@jleenman)

    Thank you!

    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 2 years ago by jleenman. Reason: marked as solved
    john

    (@johnweru)

    Hi there,

    Thanks for sharing this information.

    In this case, I have notified the lead team about this matter so that it is looked into detail.

    Apologies for the inconvenience that this has caused.

    Kind Regards,

    John

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘User Meta error when meta doesn’t exist’ is closed to new replies.