Skip to content

Instantly share code, notes, and snippets.

@RichardNesbitt
Last active April 23, 2024 09:06
Show Gist options
  • Save RichardNesbitt/5ed1198ef75ef53fcc820419e57307ee to your computer and use it in GitHub Desktop.
Save RichardNesbitt/5ed1198ef75ef53fcc820419e57307ee to your computer and use it in GitHub Desktop.
WP Bakery Page Builder - Add URL option to make entire column clickable - Updated 22 June 2023
<?php
/* add this to your theme's functions.php file */
/* Add option for URL to column settings */
vc_add_param("vc_column", array(
"type" => "vc_link",
"class" => "",
"heading" => "Column Link",
"param_name" => "column_link"
));
/* Add option for URL to column_inner settings */
vc_add_param("vc_column_inner", array(
"type" => "vc_link",
"class" => "",
"heading" => "Column Link",
"param_name" => "column_link"
));
?>
<?php
/* place this file in [your-theme]/vc_templates/ */
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
/* This file modifies the vc_column output to accomodate the otional URL parameter */
/**
* Shortcode attributes
* @var $atts
* @var $el_id
* @var $el_class
* @var $width
* @var $css
* @var $offset
* @var $content - shortcode content
* @var $css_animation
* Shortcode class
* @var WPBakeryShortCode_Vc_Column $this
*/
$el_class = $el_id = $width = $parallax_speed_bg = $parallax_speed_video = $parallax = $parallax_image = $video_bg = $video_bg_url = $video_bg_parallax = $css = $offset = $css_animation = '';
$output = '';
$atts = vc_map_get_attributes( $this->getShortcode(), $atts );
extract( $atts );
wp_enqueue_script( 'wpb_composer_front_js' );
$width = wpb_translateColumnWidthToSpan( $width );
$width = vc_column_offset_class_merge( $offset, $width );
$css_classes = array(
$this->getExtraClass( $el_class ) . $this->getCSSAnimation( $css_animation ),
'wpb_column',
'vc_column_container',
$width,
);
if ( vc_shortcode_custom_css_has_property( $css, array(
'border',
'background',
) ) || $video_bg || $parallax
) {
$css_classes[] = 'vc_col-has-fill';
}
$wrapper_attributes = array();
$has_video_bg = ( ! empty( $video_bg ) && ! empty( $video_bg_url ) && vc_extract_youtube_id( $video_bg_url ) );
$parallax_speed = $parallax_speed_bg;
if ( $has_video_bg ) {
$parallax = $video_bg_parallax;
$parallax_speed = $parallax_speed_video;
$parallax_image = $video_bg_url;
$css_classes[] = 'vc_video-bg-container';
wp_enqueue_script( 'vc_youtube_iframe_api_js' );
}
if ( ! empty( $parallax ) ) {
wp_enqueue_script( 'vc_jquery_skrollr_js' );
$wrapper_attributes[] = 'data-vc-parallax="' . esc_attr( $parallax_speed ) . '"'; // parallax speed
$css_classes[] = 'vc_general vc_parallax vc_parallax-' . $parallax;
if ( false !== strpos( $parallax, 'fade' ) ) {
$css_classes[] = 'js-vc_parallax-o-fade';
$wrapper_attributes[] = 'data-vc-parallax-o-fade="on"';
} elseif ( false !== strpos( $parallax, 'fixed' ) ) {
$css_classes[] = 'js-vc_parallax-o-fixed';
}
}
if ( ! empty( $parallax_image ) ) {
if ( $has_video_bg ) {
$parallax_image_src = $parallax_image;
} else {
$parallax_image_id = preg_replace( '/[^\d]/', '', $parallax_image );
$parallax_image_src = wp_get_attachment_image_src( $parallax_image_id, 'full' );
if ( ! empty( $parallax_image_src[0] ) ) {
$parallax_image_src = $parallax_image_src[0];
}
}
$wrapper_attributes[] = 'data-vc-parallax-image="' . esc_attr( $parallax_image_src ) . '"';
}
if ( ! $parallax && $has_video_bg ) {
$wrapper_attributes[] = 'data-vc-video-bg="' . esc_attr( $video_bg_url ) . '"';
}
$css_class = preg_replace( '/\s+/', ' ', apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, implode( ' ', array_filter( $css_classes ) ), $this->settings['base'], $atts ) );
$wrapper_attributes[] = 'class="' . esc_attr( trim( $css_class ) ) . '"';
if ( ! empty( $el_id ) ) {
$wrapper_attributes[] = 'id="' . esc_attr( $el_id ) . '"';
}
$output .= '<div ' . implode( ' ', $wrapper_attributes ) . '>';
$innerColumnClass = 'vc_column-inner ' . esc_attr( trim( vc_shortcode_custom_css_class( $css ) ) );
$output .= '<div class="' . trim( $innerColumnClass ) . '">';
/* Inserted to check whether a link is present in the settings */
if( !empty($column_link) ){
$column_link_array = vc_build_link($column_link);
$column_link = esc_url($column_link_array['url']);
$column_target = $column_link_array['target'];
$column_rel = $column_link_array['rel'];
$column_title = $column_link_array['title'];
$output .= "<a href='$column_link' ";
$output .= ( $column_target == '_blank' ) ? "target='_blank' " : '';
$output .= ( $column_rel == 'nofollow' ) ? "rel='nofollow' " : '';
$output .= ( $column_title !== '' ) ? "title='$column_title' " : '';
$output .= "class='big_link' style='position:absolute;top:0;left:0;width:100%;height:100%;z-index:9999;'aria-hidden='true'></a>";
}
/* End Changes - Resume base code */
$output .= '<div class="wpb_wrapper">';
$output .= wpb_js_remove_wpautop( $content );
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
echo $output;
<?php
/* place this file in [your-theme]/vc_templates/ */
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
/* This file modifies the vc_column_inner output to accomodate the otional URL parameter */
/**
* Shortcode attributes
* @var $atts
* @var $el_class
* @var $el_id
* @var $width
* @var $css
* @var $offset
* @var $content - shortcode content
* Shortcode class
* @var WPBakeryShortCode_Vc_Column_Inner $this
*/
$el_class = $width = $el_id = $css = $offset = '';
$output = '';
$atts = vc_map_get_attributes( $this->getShortcode(), $atts );
extract( $atts );
$width = wpb_translateColumnWidthToSpan( $width );
$width = vc_column_offset_class_merge( $offset, $width );
$css_classes = array(
$this->getExtraClass( $el_class ),
'wpb_column',
'vc_column_container',
$width,
);
if ( vc_shortcode_custom_css_has_property( $css, array(
'border',
'background',
) ) ) {
$css_classes[] = 'vc_col-has-fill';
}
$wrapper_attributes = array();
$css_class = preg_replace( '/\s+/', ' ', apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, implode( ' ', array_filter( $css_classes ) ), $this->settings['base'], $atts ) );
$wrapper_attributes[] = 'class="' . esc_attr( trim( $css_class ) ) . '"';
if ( ! empty( $el_id ) ) {
$wrapper_attributes[] = 'id="' . esc_attr( $el_id ) . '"';
}
$output .= '<div ' . implode( ' ', $wrapper_attributes ) . '>';
$innerColumnClass = 'vc_column-inner ' . esc_attr( trim( vc_shortcode_custom_css_class( $css ) ) );
$output .= '<div class="' . trim( $innerColumnClass ) . '">';
/* Inserted to check whether a link is present in the settings */
if( !empty($column_link) ){
$column_link_array = vc_build_link($column_link);
$column_link = esc_url($column_link_array['url']);
$column_target = $column_link_array['target'];
$column_rel = $column_link_array['rel'];
$column_title = $column_link_array['title'];
$output .= "<a href='$column_link' ";
$output .= ( $column_target == '_blank' ) ? "target='_blank' " : '';
$output .= ( $column_rel == 'nofollow' ) ? "rel='nofollow' " : '';
$output .= ( $column_title !== '' ) ? "title='$column_title' " : '';
$output .= "class='big_link' style='position:absolute;top:0;left:0;width:100%;height:100%;z-index:9999;'aria-hidden='true'></a>";
}
/* End Changes - Resume base code */
$output .= '<div class="wpb_wrapper">';
$output .= wpb_js_remove_wpautop( $content );
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
echo $output;
@pkoningsgithub
Copy link

pkoningsgithub commented Jan 24, 2023

Hello,

First of all thank you for this code, it's such a good function!
It still works in WP Bakery 6.10 but there is a tiny problem.
The link always opens with target=_blank but i do not select it in the link options when editing the page.
If I switch to wordpress view and edit the target to target=_self it does not do anything and on the front end it keeps opening in a new tab.

Is there a solution for this? I'm not an expert but i tried hard and I can't do it...

EDIT: nevermind, its only when you are logged in ^^ for visitors it works

@RichardNesbitt
Copy link
Author

I've just published an update for the latest WP Bakery Page Builder v. 6.13

Slight changes in their code, but nothing major. Added some ternary operators to bypass bits of code if title, rel, and target boxes aren't checked.

@Abdullahijazvu
Copy link

Abdullahijazvu commented Sep 13, 2023

The Best way to add WP Bakery column clickable

Add a Custom CSS Class:

  1. In WPBakery Page Builder, edit the column you want to make clickable.
  2. Under the "Design Options" or "Design" tab (depending on your WPBakery version), you should find an option to add a custom CSS class to the column. It might be labeled as "Extra class name" or something similar.
  3. Add a custom CSS class, for example, "clickable-column" to the column.

.clickable-column{
cursor: pointer;
}

Custom JavaScript:

  1. In your WordPress theme, you can add custom JavaScript code to make the columns with the "clickable-column" class clickable. You can do this by adding the JavaScript code in the theme's JavaScript file or using a custom JavaScript plugin.

Here's an example of how you can use JavaScript (jQuery) to make columns with the "clickable-column" class clickable and navigate to a specific URL:

jQuery(document).ready(function($) {
// Add a click event handler to elements with the "clickable-column" class
$('.clickable-column').click(function() {
// Get the URL from a data attribute or set it directly
var targetUrl = $(this).data('target-url') || 'https://example.com';
// Navigate to the URL
window.location.href = targetUrl;
});
});

@RichardNesbitt
Copy link
Author

DO NOT DO THIS.
Even if it wasn't a bad idea because you'd have to do it for every 👏 single👏 column👏, it can cause accessibility issues. Without having a semantically clickable element, there's no target for assistive technology.

@Abdullahijazvu
Copy link

DO NOT DO THIS. Even if it wasn't a bad idea because you'd have to do it for every 👏 single👏 column👏, it can cause accessibility issues. Without having a semantically clickable element, there's no target for assistive technology.

I'm using it for Anchor ID, & it works for me :)

@RichardNesbitt
Copy link
Author

RichardNesbitt commented Sep 14, 2023 via email

@eldarsht
Copy link

@RichardNesbitt everything works fine in the backend, but frontend-wise the column is still not clickable :/

WPB Version 6.4.2

@jennyLupo
Copy link

Hi I am trying to get this to work for me. I am able to add the link in the back end, but it is not working in the front end. I may be putting the second lot of code in the wrong place? I cannot find [your-theme]/vc_templates/ */. I found this which I hoped was the same thing
Jevelin: Visual Composer Compatibility(beta!) Page Template (page-vc.php).

Where do I find vc_templates? I tried theme file editor in wordpress and I searched the files on cpanel

Or is there an easier way??

Im am just winging it, any help would be appreciated!!!

@eldarsht
Copy link

@jennyLupo i just pasted both vc files in the theme root folder, since i also dont have any /vc_templates/ folder. maybe that might also be the issue for me.

my theme is bought of envato.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment