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') ) ) ) ); ?>
14 responses to “Dropdown attribute with default value in WPBakery Page Builder”
Works great for me. Sadly they don’t even have ‘std’ option listed in their parameters section.
Works like a charm. For checkboxes too. Super.
great! thanks
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
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’]).
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
So what will be default value for that dropdown?
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?
Then simply use this:
'std' => '',
and completely remove
$defaults = MyPluginClass::defaults();
Exactly what I was looking for.
Thank you for revealing this -still- undocumented parameter
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
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.Hi Aleksander,
Please tell me exact place to add code.
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.