Inconsistency with rwmb_the_value/rwmb_get_value and Switch fields

Support General Inconsistency with rwmb_the_value/rwmb_get_value and Switch fieldsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #43342
    AndreAndre
    Participant

    I set up a Switch field on the user and set the ON label to “online” and the OFF label to “offline”. That works fine in the backend. But it produces inconsistent results on the frontend. I need to show the label of that field in different places on the frontend. I use Bricksbuilder with a query loop. Bricksbuilder can output Metabox fields automatically. But for Switch fields it only outputs the value which is 0 or 1 for Switch fields. To show the label I created a function:

    function is_online() {
    return rwmb_the_value('status',['object_type' => 'user'],get_queried_object_id());
    }

    Then I added the function to Bricksbuilder like this
    {echo: is_online}

    That outputs the label twice because Bricks echoes it and the rwmb_the_value as well.

    When I change the function to use rwmb_get_value it returns 0 or 1.

    How can I get just the label without an additional echo via PHP?

    Remark: I know that I can build a function with rwmb_get_value and use an if then statement, but that would cause me to keep the label in the custom field and the function in sync.

    #43348
    PeterPeter
    Moderator

    Hello,

    Can you call the function is_online() in the Bricks template without using the echo statement? If you want to use the helper function rwmb_the_value(), you can set the fourth parameter echo to false. Like this

    rwmb_the_value( 'status', ['object_type' => 'user'], get_queried_object_id(), false )

    Read more in the documentation https://docs.metabox.io/functions/rwmb-the-value/

    #43374
    AndreAndre
    Participant

    Yes, the fourth parameter did the trick. I am not sure why I did not realize it earlier. Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.