Skip to content

Instantly share code, notes, and snippets.

@pavelthq
Last active March 27, 2024 14:51
Show Gist options
  • Save pavelthq/b7948f5951755e8ddcd2 to your computer and use it in GitHub Desktop.
Save pavelthq/b7948f5951755e8ddcd2 to your computer and use it in GitHub Desktop.
Visual Composer: Custom markup element example
(function($) {
window.VcCustomElementView = vc.shortcode_view.extend( {
elementTemplate: false,
$wrapper: false,
changeShortcodeParams: function ( model ) {
var params;
window.VcCustomElementView.__super__.changeShortcodeParams.call( this, model );
params = _.extend( {}, model.get( 'params' ) );
if ( ! this.elementTemplate ) {
this.elementTemplate = this.$el.find( '.vc_custom-element-container' ).html();
}
if ( ! this.$wrapper ) {
this.$wrapper = this.$el.find( '.wpb_element_wrapper' );
}
if ( _.isObject( params ) ) {
var template = vc.template( this.elementTemplate, vc.templateOptions.custom );
this.$wrapper.find( '.vc_custom-element-container' ).html( template( { params: params } ) );
}
}
} );
})(window.jQuery)
<?php
vc_map(array(
'name' => __( 'Custom Markup', 'js_composer' ),
'base' => 'vc_custom_markup',
'category' => array(
__( 'Content', 'js_composer' ),
),
'description' => __( 'Custom markup element', 'js_composer' ),
'params' => array(
array(
'type' => 'textfield',
'param_name' => 'style',
'value' => 'custom style!',
'heading' => 'Style',
),
array(
'type' => 'textfield',
'param_name' => 'color',
'value' => 'custom color!',
'heading' => 'Color',
),
array(
'type' => 'textfield',
'param_name' => 'title',
'value' => 'custom title!',
'heading' => 'Title',
),
),
'js_view' => 'VcCustomElementView',
'custom_markup' => '<div class="vc_custom-element-container">Style: "{{ params.style }}", Color: "{{ params.color }}", Title: "{{{ params.title }}}"</div>',
)
);
@htesligte
Copy link

It took me a long time to find how to properly enqueue the script. This helped:

add_action( 'admin_enqueue_scripts', function() {
    wp_enqueue_script( 'YOUR_SCRIPT_NAME', plugin_dir_url( __FILE__ ) . 'vc-custom-element-view.js', array('vc-backend-min-js'), '1.0', true );
});

@drnourhomsi
Copy link

drnourhomsi commented Mar 27, 2024

I know this has been for a long time, but anyone who came across can use this implementation:


vc_map(array(

"name" => __("Shortcode_Name" "TEXT_DOMAIN"),

"base" => "Shortcode_Name",

"class" => "Shortcode_Name",

"icon" => "path/to/icon",

"admin_enqueue_css" => plugin_dir_url(__FILE__) . '../../assets/css/style.css',

"admin_enqueue_js" => plugin_dir_url(__FILE__) . '../../assets/js/script.js',

'js_view' => 'VcCustomElementView',

'custom_markup' => '<div class="vc_custom-element-container"><span style="background-color:{{{params.title_color_bg_custom}}}; color:{{{params.title_color_custom}}}; font-size:{{{params.title_font_size}}}px; font-family:{{{params.title_font_art}}};">{{{ params.title }}}</span></div>',

"params" => array(

	array(
		"type" => "textfield",
		"holder" => "div",
		"heading" => __("Title", "LIS_TEXT_DOMAIN"),
		"param_name" => "title",
		"value" => ''
	)
),

));

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