Skip to content

Instantly share code, notes, and snippets.

@lkoudal
Created January 21, 2017 02:26
Show Gist options
  • Select an option

  • Save lkoudal/346b3d9717168ebd341a27660da38751 to your computer and use it in GitHub Desktop.

Select an option

Save lkoudal/346b3d9717168ebd341a27660da38751 to your computer and use it in GitHub Desktop.
WP: Visual composer custom element
<?php
// Before VC Init
add_action( 'vc_before_init', 'vc_before_init_actions' );
function vc_before_init_actions() {
//.. Code from other Tutorials ..//
// Require new custom Element
require_once( get_stylesheet_directory().'/vc-custom-element.php' );
}
/*
Element Description: vcCustomElement
*/
// Element Class
class vcCustomElement extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_custom_mapping' ) );
add_shortcode( 'vc_nvl_cat_item', array( $this, 'vc_custom_html' ) );
}
// Element Mapping
public function vc_custom_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('Custom element item', 'text-domain'),
'base' => 'custom_element_item_base',
'description' => __('Custom element item', 'text-domain'),
'category' => __('Custom element', 'text-domain'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'params' => array(
array(
'type' => 'textfield',
'holder' => 'div',
'class' => 'field-class',
'heading' => __( 'Field name', 'text-domain' ),
'param_name' => 'param',
'value' => __( 'Field name', 'text-domain' ),
'description' => __( 'Field name', 'text-domain' ),
'admin_label' => false,
'weight' => 0,
'group' => 'Field group',
),
),
)
);
}
// Element HTML
public function vc_custom_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'param' => '',
),
$atts
)
);
ob_start();
?>
<div><?php echo $param; ?></div>
<?php
return ob_get_clean();
}
} // End Element Class
// Element Class Init
new vcCustomElement();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment