Created
August 27, 2021 17:54
-
-
Save pingram3541/017d249e197ed12c075bb5ce11ef7743 to your computer and use it in GitHub Desktop.
Elementor - Modify Spacer Widget to wrap an anchor tag around it (link)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Modify Spacer Widget to wrap an anchor tag around it | |
* | |
* 1) Add url control to spacer widget for loading w/ a link | |
* | |
* 2) Use 'elementor/widget/render_content' action hook to update the spacer widget's | |
* normal output on the front end | |
* | |
**/ | |
//part 1. to add our url control | |
add_action( 'elementor/element/spacer/section_spacer/before_section_end', function( $element, $args ) { | |
/** @var \Elementor\Element_Base $element */ | |
$element->add_control( | |
'anchor_wrap', | |
[ | |
'type' => \Elementor\Controls_Manager::URL, | |
'label' => __( 'External Link', 'elementor' ), | |
'placeholder' => __( 'https://your-link.com', 'elementor' ), | |
'show_external' => true, | |
'default' => [ | |
'url' => '', | |
'is_external' => true, | |
'nofollow' => true, | |
], | |
] | |
); | |
}, 10, 2 ); | |
//part 2. action to update spacer widget render | |
add_action( 'elementor/widget/render_content', function( $content, $widget ) { | |
if ( 'spacer' === $widget->get_name() ) { | |
$settings = $widget->get_settings(); | |
if ( ! empty( $settings['anchor_wrap']['url'] ) ) { | |
$target = $settings['anchor_wrap']['is_external'] ? ' target="_blank"' : ''; | |
$nofollow = $settings['anchor_wrap']['nofollow'] ? ' rel="nofollow"' : ''; | |
$content = '<a href="' . $settings['anchor_wrap']['url'] . '"' . $target . $nofollow . '>' . $content . '</a>'; | |
} | |
} | |
return $content; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment