Dropdown attribute with default value in WPBakery Page Builder

Visual Composer dropdown attribute

I’m currently implementing support for WPBakery Page Builder to my plugins and went to not well-documented dropdown attribute parameters.

So, when we have an array of available values (no matter if the array contains key=>value, values only or it’s mixed), and we also have a default value for that parameter set in plugin options (for example), it is possible to pre-select that default value in the dropdown list.

Even you will not find a clear explanation about how to do that in the WPBakery Knowledge Base for the vc_map() function, if you dig plugin files, or analyse other plugins, you’ll find it for sure 🙂

Example parameters for the dropdown attribute

So, here we go. The magic parameter we need here is std and should contain the default value (key for multidimensional array). Here is the demo code:

<?php
vc_map( array(
  'name'     => __('Foo Bar'),
  'base'     => 'foo_bar',
  'category' => __('Content'),
  'params'   => array(
    array(
      'type'        => 'dropdown',
      'heading'     => __('Foo'),
      'param_name'  => 'foo',
      'admin_label' => true,
      'value'       => array(
        'one'   => 'First Option',
        'two'   => 'Second Option',
        'three' => 'Third Option',
        'four'  => 'Fourth Option'
      ),
      'std'         => 'two', // Your default value
      'description' => __('The description')
      )
    )
  )
);
?>

Comments

14 responses to “Dropdown attribute with default value in WPBakery Page Builder”

  1. Zeshan Ahmed Avatar

    Works great for me. Sadly they don’t even have ‘std’ option listed in their parameters section.

  2. Roman Dynkin Avatar
    Roman Dynkin

    Works like a charm. For checkboxes too. Super.

  3. Nacho Avatar
    Nacho

    great! thanks

  4. Leonie Avatar
    Leonie

    Hello,

    thank you so very much for this tutorial!

    I’m using your Code in my child themes function.php… So I’m currently having no Class.
    Could you tell me what I have to write instead of this line:
    $defaults = MyPluginClass::defaults(); ?

    Thank you!
    Leonie

    1. Aleksandar Urošević Avatar

      If you have some option to define defaults in Dashboard, then call function which read default values.

      If you simply wish to set default value once, then you even can remove that line and set default value in line 19 of my example code (for example ‘green’ instead $defaults[‘foo’]).

      1. Leonie Avatar
        Leonie

        Hello Aleksandar,

        thanks for your quick reply!
        I have multiple values like so:

        array(
        “type” => “dropdown”,
        “heading” => __(“Post Type”),
        “param_name” => “posttype”,
        “admin_label” => true,
        “value” => get_post_types(),
        “std” => $defaults[‘posttype’]
        ),

        But, I have no options to define default values..
        Could you give any tip?

        Thanks!!
        Leonie

        1. Aleksandar Urošević Avatar

          So what will be default value for that dropdown?

          1. Leonie Avatar
            Leonie

            hm..
            I only want to display all post types as dropdown list. No “Default Value” needed.
            I call the values in my shortcode like so:

            add_shortcode( ‘wb_list_view’, ‘wb_list_view_func’ );
            function wb_list_view_func( $atts, $content = null ) {

            $atts = shortcode_atts( array(
            ‘posttype’ => ”,
            ), $atts, ‘wb_list_view’);

            $out = $atts[‘posttype’];

            return $out;

            }

            Is there the mistake?

          2. Aleksandar Urošević Avatar

            Then simply use this:
            'std' => '',
            and completely remove
            $defaults = MyPluginClass::defaults();

  5. Arno Avatar
    Arno

    Exactly what I was looking for.
    Thank you for revealing this -still- undocumented parameter

  6. Rabis Naqvi Avatar
    Rabis Naqvi

    Hi Aleksander!
    I am having a issue with the code. The Problem is i don’t know to which class i should replace MyPluginClass with. I am using the following code to create a element in VC.

    class spartaHeading extends WPBakeryShortCode {
         
        // Element Init
        function __construct() {
            add_action( 'init', array( $this, 'sparta_heading_mapping' ) );
            add_shortcode( 'spartaHeading', array( $this, 'sparta_heading_html' ) );
        }
         
        // Element Mapping
        public function sparta_heading_mapping() {
             
                 
        // Stop all if VC is not enabled
        if ( !defined( 'WPB_VC_VERSION' ) ) {
                return;
        }
        // Map the block with vc_map()
        vc_map( 
      
            array(
                'name' => __('Sparta Heading', 'sparta'),
                'base' => 'sparta_heading',
                'description' => __('Sparta Heading Element with Customizations', 'sparta'), 
                'category' => __('Sparta', 'sparta'),   
                'icon' => get_template_directory_uri().'/assets/images/heading.png',            
                'params' => array( 
                               // Fields Here
    )
            )
        );                             
            
        } 
         
         
        // Element HTML
        public function sparta_heading_html( $atts ) {
             
            //.. the Code is in the next steps ..//
             
        } 
         
    } // End Element Class
     
    // Element Class Init
    new spartaHeading();

    ================================================
    I can’t get my class name!. For your information, i’m coding a theme and the code above is used in it.
    Thanks & sorry if the question is abnormal.
    Best Regards,
    Rabis Naqvi

    1. Aleksandar Urošević Avatar

      In my example I’m getting default values from my own class. You do not need to use such approach, and you can completely remove that line. Then for std (default) VC element value you can use static integer, string, bool, whatever default value for that element is. Look at this example.

  7. Satyajit Ghosh Avatar
    Satyajit Ghosh

    Hi Aleksander,
    Please tell me exact place to add code.

    1. Aleksandar Urošević Avatar

      Hi Satyajit,

      If you do not have any experience with PHP, then you have to find developer to do what you wish.

      What you need from my example snippet code is just to set std parameter for block element.