Skip to content

Instantly share code, notes, and snippets.

@trangsihung
Last active June 13, 2019 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trangsihung/9fc4822f1620203eda416480d8bcbcd3 to your computer and use it in GitHub Desktop.
Save trangsihung/9fc4822f1620203eda416480d8bcbcd3 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'vc_autocomplete_[shortcode_base]_[param_name]_callback', 'vc_autocomplete_taxonomies_field_search', 10, 1 );
add_filter( 'vc_autocomplete_[shortcode_base]_[param_name]_render', 'vc_autocomplete_taxonomies_field_render', 10, 1 );
$args = array(
'name' => 'Shortcode Name',
'base' => 'shortcode_base',
'category' => __( 'Themes Elements', 'vtm' ),
'params' => array(
array(
'type' => 'autocomplete',
'heading' => 'Chọn danh mục',
'param_name' => 'param_name',
'settings' => array(
'multiple' => true,
'min_length' => 1,
'groups' => true,
// In UI show results grouped by groups, default false
'unique_values' => true,
// In UI show results except selected. NB! You should manually check values in backend, default false
'display_inline' => true,
// In UI show results inline view, default false (each value in own line)
'delay' => 500,
// delay for search. default 500
'auto_focus' => true,
// auto focus input, default true
),
'description' => __( 'Enter categories, tags or custom taxonomies.', 'js_composer' )
)
),
)
<?php
/* AUTOCOMPLETE SEARCH */
add_filter( 'vc_autocomplete_dalat_diadiem_uudai_hotels_callback', 'nka_vc_include_cpt_hotel_search', 10, 1 );
/* AUTOCOMPLETE RENDER VALUE */
add_filter( 'vc_autocomplete_dalat_diadiem_uudai_hotels_render', 'nka_vc_include_field_render', 10, 1 );
function nka_vc_include_cpt_hotel_search( $search_string ) {
return nka_vc_include_field_search( $search_string, 'dl_hotel' );
}
function nka_vc_include_field_render( $value ) {
$post = get_post( $value['value'] );
return is_null( $post ) ? false : array(
'label' => $post->post_title,
'value' => $post->ID
);
}
function nka_vc_include_field_search( $search_string, $post_type ) {
$query = $search_string;
$data = array();
$args = array(
's' => $query,
'post_type' => $post_type,
);
$args['vc_search_by_title_only'] = true;
$args['numberposts'] = - 1;
if ( 0 === strlen( $args['s'] ) ) {
unset( $args['s'] );
}
add_filter( 'posts_search', 'vc_search_by_title_only', 500, 2 );
$posts = get_posts( $args );
if ( is_array( $posts ) && ! empty( $posts ) ) {
foreach ( $posts as $post ) {
$data[] = array(
'label' => $post->post_title,
'value' => $post->ID
);
}
}
return $data;
}
/* NEW PARAM */
vc_add_shortcode_param( 'dl_post_cat', 'vc_param_dl_post_cat_callback' );
function vc_param_dl_post_cat_callback( $settings, $value ) {
$_terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false
) );
$multiple = isset($settings['multiple']) ? 'multiple="multiple"' : '';
ob_start(); ?>
<select <?php echo $multiple ?> name="<?php echo esc_attr( $settings['param_name'] ) ?>"
class="wpb_vc_param_value wpb-input wpb-select <?php echo esc_attr( $settings['param_name'] ) ?>_field <?php echo esc_attr( $settings['param_name'] ) ?>">
<option value="0">Chọn danh mục</option>
<?php foreach ($_terms as $item) { ?>
<option <?php selected( $value, $item->term_id ) ?> value="<?php echo $item->term_id ?>"><?php echo $item->name ?></option>
<?php } ?>
</select>
<?php $render = ob_get_clean();
return $render;
}
<?php
/*
New Param Type : create dropdown for product_cat taxonomy in WooCommerce
*/
vc_add_shortcode_param( 'woo_product_cat', 'vc_param_woo_product_cat_callback' );
function vc_param_woo_product_cat_callback( $settings, $value ) {
$_terms = get_terms( array(
'taxonomy' => 'product_cat',
'hide_empty' => false
) );
ob_start(); ?>
<select name="<?php echo esc_attr( $settings['param_name'] ) ?>"
class="wpb_vc_param_value wpb-input wpb-select <?php echo esc_attr( $settings['param_name'] ) ?>_field <?php echo esc_attr( $settings['param_name'] ) ?>">
<option value="0">Chọn danh mục</option>
<?php foreach ($_terms as $item) { ?>
<option <?php selected( $value, $item->term_id ) ?> value="<?php echo $item->term_id ?>"><?php echo $item->name ?></option>
<?php } ?>
</select>
<?php $render = ob_get_clean();
return $render;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment