Skip to content

Instantly share code, notes, and snippets.

@mahafuz
Created December 12, 2017 11:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mahafuz/9aa69e666f91e09f51c64bdf29dfe0d8 to your computer and use it in GitHub Desktop.
Save mahafuz/9aa69e666f91e09f51c64bdf29dfe0d8 to your computer and use it in GitHub Desktop.
Dropdown attribute with default value in WPBakery Visual Composer
<?php
/**
* Visual Composer DropDown Item Example in Right Way
* Dropdown attribute with default value in WPBakery Visual Composer
*
* @package VC_Dropdown
* @author Mahfuz <asrmahfuz8@gmail.com>
*/
vc_map(array(
"name" => __( "VC Dropdown", 'textdomain' ),
"description" => __( "VC Dropdown items example in right way!!!", 'textdomain' ),
"base" => 'base_name',
"controls" => "full",
"category" => __( 'category_name', 'textdomain' ),
"params" => array(
array(
'type' => 'dropdown',
'heading' => __( 'Dropdown Items', 'textdomain' ),
'value' => array(
__( 'Item One', 'textdomain' ) => '1',
__( 'Item Two', 'textdomain' ) => '2',
__( 'Item Three', 'textdomain' ) => '3',
__( 'Item Four', 'textdomain' ) => '4'
),
'description' => __( 'Upload member photo.', 'textdomain' ),
'param_name' => 'dropdown_items'
)
)
)
);
@sebastienserre
Copy link

Hello,
Where dis you determine the default data ?

@Luckyfella73
Copy link

@sebastienserre

In case you or anybody else still needs this:

array(
    'type'        => 'dropdown',
    'heading'     => __('Test Dropdown', 'text-domain'),
    'param_name'  => 'foo',
    'admin_label' => true,
    'value'       => array(
        'one'   => 'First Option',
        'two'   => 'Second Option',
        'three' => 'Third Option',
        'four'  => 'Fourth Option'
    ),
    'std'         => 'Second Option', // Your default value
    'description' => __('The description'),
    'group' => 'Whatever group you have'
),

Surprisingly (for me at least) you don't set the default by using the index of your values array. Looking at the source code in backend editor you see that the actual value of your select->option tag is the value of the array item you define here in your dropdown array (for example "Second Option") while the label you get in the backend is what you have here as the index of your array. I had expected to have it the other way around...

@aproni34f
Copy link

@Luckyfella73
Copy link

It's a few years ago I wrote that bit of code.. but at time of writing it worked. Here you can read a tutorial about that topic with several comments of other people the code worked for as well:

https://urosevic.net/wordpress/tips/dropdown-defaults-visual-composer/

If I remember right that parameter was not documented years ago either but implemented nevertheless.

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