Okay
  Print

Advanced: How to modify shortcode mapping?

What is shortcode mapping? Other words this is shortcode parameters.


You may need to modify the shortcode params if you want to extend the shortcode with new features or hide some existing params.


Modifying the mapping of Visual Composer's shortcodes

You have two options:

  • Using the Visual Composer API
  • Using the filter


Example code of using Visual Composer API:

function startapp_child_modify_mapping() {
    vc_add_param( 'vc_row', array(
        'param_name'  => 'test_param',
        'type'        => 'textfield',
        'weight'      => - 3,
        'heading'     => esc_html__( 'Test Param', 'startapp' ),
        'description' => esc_html__( 'Test Param Description', 'startapp' ),
    ) );
}
add_action( 'vc_after_init', 'startapp_child_modify_mapping', 15 );

Note the priority 15. It should be lower than default value (> 10). Result will be:


For more information about VC inner API see the VC documentation.

Also you can use a filter startapp_shortcode_vc_params. Example:

function startapp_child_replace_params( $params, $tag ) {
   if ( 'vc_row' === $tag ) {
      $params[] = array(
        'param_name'  => 'test_param',
        'type'        => 'textfield',
        'weight'      => -3,
        'heading'     => esc_html__( 'Test Param', 'startapp' ),
        'description' => esc_html__( 'Test Param Description', 'startapp' ),
      );
   }
   return $params;
}
add_filter( 'startapp_shortcode_vc_params', 'startapp_child_replace_params', 10, 2 );


Modifying the mapping of StartApp shortcodes

To modify the brand shortcodes params use filter startapp_shortcode_params. Fox example I'm going to add a new parameter to Animated Digits shortcode:

function startapp_child_animated_digits_extra_params( $params, $shortcode ) {
    if ( 'startapp_animated_digits' !== $shortcode ) {
        return $params;
    }
    $params[] = array(
        'param_name'  => 'test_param',
        'type'        => 'textfield',
        'weight'      => 10,
        'heading'     => esc_html__( 'Test Param', 'startapp' ),
        'description' => esc_html__( 'Test Param Description', 'startapp' ),
    );
    return $params;
}
add_filter( 'startapp_shortcode_params', 'startapp_child_animated_digits_extra_params', 10, 2 );

Result will be:


To apply this param you should add it to shortcode_atts() function inside the shortcode template. You can modify the template or simply use filter:

function startapp_child_animated_digits_extra_atts( $atts, $shortcode ) {
   if ( 'startapp_animated_digits' !== $shortcode ) {
      return $atts;
   }
   $atts['test_param'] = '';
   return $atts;
}
add_filter( 'startapp_shortcode_default_atts', 'startapp_child_animated_digits_extra_atts', 10, 2 );

Modifying the template is common case, because obviously you should use your new param in some manner. But in some cases it is enought just to add a new attribute to list. This is required for Visual Composer shortcodes, too.


Related atricles

Advanced: How to modify shortcode template?

Advanced: Cache in shortcodes