Registering Dynamic Data Tag - User IP

@timmse @thomas
Have the following code, function works perfectly registers new dynamic tag and creates / adds the new group.
I am working with Bricks 2.0.1 local dev, + advanced themer, no other plugins. Using a simple text element with the dynamic data tag. ssl is good.

The IP renders in the builder for admin as:

::1

My filter / function code is:

add_filter( 'bricks/dynamic_tags_list', 'add_user_ip_tag_to_builder' );

function add_user_ip_tag_to_builder( $tags ) {
    $tags[] = [
        'name'  => '{user_ip}', // tag name to use in Bricks
        'label' => 'User IP Address', // label for dynamic data dropdown
        'group' => 'User Data', // custom Bricks Group 
    ];
    return $tags;
}

add_filter( 'bricks/dynamic_data/render_tag', 'get_user_ip_tag_value', 20, 3 );

function get_user_ip_tag_value( $tag, $post, $context = 'text' ) {
    // Only proceed if the tag matches my custom tag
    if ( $tag !== '{user_ip}' ) {
        return $tag; // Return the original tag if it's not my custom tag
    }

    // Retrieve the user's IP address
    // Fallback to HTTP_X_FORWARDED_FOR if present check this when live cloudflare (for proxies/CDNs)
    $ip_address = $_SERVER['REMOTE_ADDR'];
    if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return $ip_address;
}

The problem I’m having is it’s not rendering on frontend for logged in or logged out user.
No cache and no php or js errors in console.
On the front end the tag shows as {user_ip}

Any hints on what might be the issue OR is this because this is local dev?

because it is localhost if you test it on online server it will show proper ip

so it works dont worry :slight_smile:

Would like to understand “why” it doesn’t render anything on the frontend.
Also as an action I have email, and nothing renders in the email.

If ::1 renders on the canvas why not show SOMETHING that would indicate it is / will work on live server?
When I look in form submissions… there is a IP Address entry done by default which shows ::1, but yet the column for mine shows nothing.

not sure why your setup doesnt work or how it works cant say anything about that

but I know for a fact localhost ip returns weird value and because of that the conditions steups having problems with it

here read this topic to understand whats going on

this math explanation is great too

1 Like

Well, on live site, it does NOT work. While it does work in the builder.

I used a code element, and used:

<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
if ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
echo $ip_address;
?>

Signed it, and it shows the IP on the frontend just fine. So there is no issue with the ip_address variable or server, the issue is with bricks not rendering this tag.