Elementor Button Link Target

User
$repeater->add_control(
'button_link',
[
'label' => __( 'Button Link', 'sage' ),
'type' => ElementorControls_Manager::URL,
'label_block' => true,
]
);
in elementor get if open to new widow selected
Assistant
To check if the **"Open in new window"** option is selected for a URL control (`ElementorControls_Manager::URL`) in Elementor, you can use the `is_external` property from the returned URL array.

Here’s how you can access it in your widget render method or template:

$button_link = $item['button_link'];

$url = $button_link['url'] ?? '';
$is_external = $button_link['is_external'] ?? false;
$nofollow = $button_link['nofollow'] ?? false;

// Example anchor tag
echo '<a href="' . esc_url( $url ) . '"'
    . ( $is_external ? ' target="_blank" rel="noopener"' : '' )
    . ( $nofollow ? ' rel="nofollow"' : '' )
    . '>Button Text</a>';

### Explanation:
– `is_external` is `true` if "Open in new window" is checked.
– `nofollow` is `true` if "Add nofollow" is checked.
– You should include `rel="noopener"` for security when using `target="_blank"`.

Let me know if you're doing this inside a repeater loop or a custom widget class and want the full context.

Assistant
Elementor’s URL control returns an array that includes keys such as `url`, `is_external`, and `nofollow`. You can check if the “open in new window” option is selected by looking at the `is_external` key in the control’s settings. For example, if you’re retrieving the settings from your widget, you can do something like this:

// Assuming $settings contains your widget's settings:
$button_link = $settings['button_link']['url'];
$is_external = ! empty( $settings['button_link']['is_external'] ) ? ' target="_blank"' : '';

// Example usage in an anchor tag:
echo '<a href="' . esc_url( $button_link ) . '"' . $is_external . '>Button</a>';

In this code, the `! empty( $settings['button_link']['is_external'] )` check will determine if the “open in new window” checkbox was selected, and if so, it adds the `target="_blank"` attribute to your link. This way, you can dynamically control the link behavior based on the user’s selection in Elementor.

Leave a Comment

Your email address will not be published. Required fields are marked *