Extending the Visual Composer Row element with a new property

Visual Composer for WordPress provides many ways to extend its functionality. There are already many add-ons available for this dynamic page builder, but its easy to do the extending yourself,  by following the hints in the documentation ;)

In this small tutorial we are adding a “type” field to allow a special css class to be added to each of our row containers.

New Visual Composer element properties can be easily added by using the vc_add_param function. See documentation for more information. In our case we are adding a property to the vc_row element.

ADJUSTING YOUR THEME

By adding the following lines to your themes functions.php, this new property will show in the admin row settings popup.

  1. vc_add_param("vc_row", array(
  2.         "type" => "dropdown",
  3. "group" => "portalZINE Additions",
  4.         "class" => "",
  5.         "heading" => "Type",
  6.         "param_name" => "type",
  7.         "value" => array(
  8.                 "In Container" => "in_container",
  9.                 "Full Width Background" => "full_width_background",
  10.                 "Full Width Content" => "full_width_content"           
  11.         )
  12. ));

The new setting will be saved, when you save your document, but you wont see any changes on the frontend yet! Our goal is to switch a css class depending on the type we selected.

Each element within Visual Composer has its own template and these templates can be overruled using your own theme templates. In this case vc_row.php.

  1. Create a new folder “vc_templates” in your theme or child-theme.
  2. Copy vc_row.php from /wp-content/plugins/js_composer/includes/templates/shortcodes/ to your newly created folder.

BEFORE

The file contents should look something like this:

  1. <?php
  2. $output = $el_class = $bg_image = $bg_color = $bg_image_repeat = $font_color = $padding = $margin_bottom = $css = '';
  3. extract(shortcode_atts(array(
  4.     'el_class'        => '',
  5.     'bg_image'        => '',
  6.     'bg_color'        => '',
  7.     'bg_image_repeat' => '',
  8.     'font_color'      => '',
  9.     'padding'         => '',
  10.     'margin_bottom'   => '',
  11.     'css' => ''
  12. ), $atts));
  13.  
  14. // wp_enqueue_style( 'js_composer_front' );
  15. wp_enqueue_script( 'wpb_composer_front_js' );
  16. // wp_enqueue_style('js_composer_custom_css');
  17.  
  18. $el_class = $this->getExtraClass($el_class);
  19.  
  20. $css_class = apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, 'vc_row wpb_row '. ( $this->settings('base')==='vc_row_inner' ? 'vc_inner ' : '' ) . get_row_css_class() . $el_class . vc_shortcode_custom_css_class( $css, ' ' ), $this->settings['base'], $atts );
  21.  
  22. $style = $this->buildStyle($bg_image, $bg_color, $bg_image_repeat, $font_color, $padding, $margin_bottom);
  23. $output .= '<div class="'.$css_class.'"'.$style.'>';
  24. $output .= wpb_js_remove_wpautop($content);
  25. $output .= '</div>'.$this->endBlockComment('row');
  26.  
  27. echo $output;

AFTER

Now we are adding the magic from the backend to the frontend :) This is how the altered version should look like in your theme folder

  1. <?php
  2. $output = $el_class = $bg_image = $bg_color = $bg_image_repeat = $font_color = $padding = $margin_bottom = $css = '';
  3. extract(shortcode_atts(array(
  4. 'type'                   => 'in_container',  
  5.  'el_class'        => '',
  6.     'bg_image'        => '',
  7.     'bg_color'        => '',
  8.     'bg_image_repeat' => '',
  9.     'font_color'      => '',
  10.     'padding'         => '',
  11.     'margin_bottom'   => '',
  12.     'css' => ''
  13. ), $atts));
  14.  
  15. // wp_enqueue_style( 'js_composer_front' );
  16. wp_enqueue_script( 'wpb_composer_front_js' );
  17. // wp_enqueue_style('js_composer_custom_css');
  18.  
  19. $el_class = $this->getExtraClass($el_class);
  20.  
  21. $css_class = apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, 'vc_row wpb_row '. ( $this->settings('base')==='vc_row_inner' ? 'vc_inner ' : '' ) . get_row_css_class() . $el_class . vc_shortcode_custom_css_class( $css, ' ' ), $this->settings['base'], $atts );
  22.  
  23. $style = $this->buildStyle($bg_image, $bg_color, $bg_image_repeat, $font_color, $padding, $margin_bottom);
  24. $output .= '<div class="'.$type.' '.$css_class.'"'.$style.'>';
  25. $output .= wpb_js_remove_wpautop($content);
  26. $output .= '</div>'.$this->endBlockComment('row');
  27.  
  28. echo $output;

We added
‘type’ => ‘in_container’
to the shortcode parameter extraction, with a default setting.

We also added the resulting type to our row wrapper
$output .= ‘<div class=”‘.$type.’ ‘.$css_class

Visual Composer allows you to modify any of its elements that way and extend it with your own options or mighty extensions ;) Visual Composer allows some nice custom tweaking, you just need to look a bit closer at the documentation, which is not always that simple to follow :)

Enjoy coding …

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

View Comments

  • Visual composer never was a smart thing. It's crap, in fact.
    Why make it so complicated?
    What's wrong with the good old CSS?
    So easy to change any style, layout, etc., with plain CSS. With VC? Waste time and still can't do what you "Really" want and otherwise could do with plain, straight forward CSS files.

    VC is for idiots and poor hackers (fits WP overall purpose)

    • All depends what your capabilities are. Most people are not comfortable to edit CSS or even dive into SASS / SCSS.

      All page builders have their pros and cons. For those that want to tweak things themselves, without getting someone that knows how to, they are a blessing ;)

      So they are not for idiots, but for those that have other strengths and rather concentrate on the content. Nobody is forcing you to use a page builder!

      Cheers
      Alex

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago