Create a custom page builder element
Salient's page builder is a build of WPBakery Page Builder, so a custom element is registered the WPBakery way: a vc_map call on the vc_before_init hook from your child theme, plus a template that renders the shortcode. This page walks through both.
Adding a map for your new element
Adding a new element to the page builder needs to be handled via a child theme. In the examples below, we’ll add the code directly into the child theme functions.php file (salient-child/functions.php).
The first step in adding a new element to the page builder is to register a map using the WPBakery function vc_map. This map contains all of the element settings as well as the general information about the element, such as the title and shortcode name. For a full list of the parameters available when creating your element, see the WPBakery documentation.
add_action('vc_before_init', 'salient_child_custom_wpbakery_elements');
function salient_child_custom_wpbakery_elements() {
vc_map( array(
"name" => __( "My Custom Element", "salient-child" ),
"base" => "my_custom_element",
"class" => "",
"category" => __( "Content", "salient-child"),
"html_template" => get_stylesheet_directory() . '/custom_element_templates/my_custom_element.php',
"params" => array(
array(
"type" => "textfield",
"class" => "",
"heading" => __( "Text", "salient-child" ),
"param_name" => "text_content",
"value" => __( "Default param value", "my-text-domain" ),
"description" => __( "Description for foo param.", "my-text-domain" )
),
array(
"type" => "colorpicker",
"class" => "",
"heading" => __( "Text color", "my-text-domain" ),
"param_name" => "color",
"value" => '#FF0000',
"description" => __( "Choose text color", "my-text-domain" )
)
)
));
}
Creating a template to display the element
With the above snippet in place, a new element will now be available to select in the page builder. However, nothing will display on the front-end of the site because there’s no template file yet. In the function above, we’ve specified that the template file will live within the child theme at salient-child/custom_element_templates/my_custom_element.php. This is handled through the line:
"html_template" => get_stylesheet_directory() . '/custom_element_templates/my_custom_element.php',
That location can be changed to wherever you wish to store the templates.
Creating the template file to process and display our element
Inside the file salient-child/custom_element_templates/my_custom_element.php, we can now use the parameters however we want. Our element has two parameters:
- A text field stored as text_content
- A color field stored as color
As an example, we’ll take both of those fields and output them in a basic paragraph:
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
extract(shortcode_atts(array(
"color" => "#000",
'text_content' => ''
), $atts));
echo '<p style="color: '.$color.';">'.$text_content.'</p>';
Going further
For further examples of processing elements in template files, examine the Salient element set template files at wp-content\plugins\salient-core\includes\vc_templates.