Dropdown attribute with default value in WPBakery Visual Composer

Learn how to preselect value in dropdown list when custom Visual Composer block is added to page/post layout, based on default plugin settings.

Visual Composer dropdown attribute

I’m currently implementing support for Visual Composer to my plugins, and went to not well documented dropdown attribute parameters.

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

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

Example parameters for dropdown attribute

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

 

14 thoughts on “Dropdown attribute with default value in WPBakery Visual Composer”

  1. 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. 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. 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. 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. 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.

    ================================================
    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. 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.

Leave a Reply

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