Search code examples
wordpresscategories

how to add multi select category in visual composer


How can I add a multi-selector for the Visual composer?

vc_map( array(
"name" => esc_html__("Ajax Posts", '7mag'),
"base" => "YT_ajaxpost",
"icon" => "YT_vc_ico_ajaxpost",
"class" => "YT_vc_sc_ajaxpost",
"category" => esc_html__("7mag", '7mag'),
"params"    => array(
array(
        "type" => "dropdown",
        "heading" => esc_html__("Style", '7mag'),
        "param_name" => "multiple",
        "value" => $categories_array,
    ),
) );

For example: link


Solution

  • You need to register new shortcode attribute.

    // Create multi dropdown param type
    vc_add_shortcode_param( 'dropdown_multi', 'dropdown_multi_settings_field' );
    function dropdown_multi_settings_field( $param, $value ) {
       $param_line = '';
       $param_line .= '<select multiple name="'. esc_attr( $param['param_name'] ).'" class="wpb_vc_param_value wpb-input wpb-select '. esc_attr( $param['param_name'] ).' '. esc_attr($param['type']).'">';
       foreach ( $param['value'] as $text_val => $val ) {
           if ( is_numeric($text_val) && (is_string($val) || is_numeric($val)) ) {
                        $text_val = $val;
                    }
                    $text_val = __($text_val, "js_composer");
                    $selected = '';
    
                    if(!is_array($value)) {
                        $param_value_arr = explode(',',$value);
                    } else {
                        $param_value_arr = $value;
                    }
    
                    if ($value!=='' && in_array($val, $param_value_arr)) {
                        $selected = ' selected="selected"';
                    }
                    $param_line .= '<option class="'.$val.'" value="'.$val.'"'.$selected.'>'.$text_val.'</option>';
                }
       $param_line .= '</select>';
    
       return  $param_line;
    }
    

    Then use it:

    array(
            "type" => "dropdown_multi",
            "heading" => esc_html__("Style", '7mag'),
            "param_name" => "multiple",
            "value" => $categories_array,
        ),