Add Link to a primary field label

I would like to embed a link in a primary field label.
I have already written to support, who confirmed that this works with the filter gform_field_content.

As I am an absolute beginner, I have no idea how and where to use this filter to make it work?

I hope someone can help me!

Kind regards
Mathias

Hi Mathias.

What do you mean by “a primary field label”?

You can add a link to any field label on your form with the gform_field_content filter.

Please note that this is a tricky request as it involves intercepting the current field label markup, finding the right spot to insert the link, and then returning the modified field label markup to the front end to be displayed.

In the following example, the code adds a custom link to the ‘Name’ field label as a prefix.

add_filter( 'gform_field_content_8_1', 'add_link_to_field_label', 10, 5 );
function add_link_to_field_label( $field_content, $field, $value, $lead_id, $form_id ) {
    // Define the URL you want to add
    $url = 'https://www.example.com';

    // Use a regular expression to insert the link after the opening label tag
    $field_content = preg_replace('/(<label[^>]*>)/', '$1<a href="' . esc_url( $url ) . '">Your Link Text</a> ', $field_content);

    return $field_content;
}

The output would be something like this:

image

NOTE: If you plan to use the code above, make sure to change the Form ID and Field ID in the filter name.

Note that I’m using gform_field_content_8_1. That means it will affect a Form with ID 8 and the Field with ID 1.

You’d need to identify what is your form’s ID and the ID of the field you want to add the link to.

Also, keep in mind that the code should go in Appearance > Theme Editor > Functions.php on your WordPress admin or in your preferred code snippets plugin.

Here is the official documentation for the gform_field_content filter: https://docs.gravityforms.com/gform_field_content/.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.