Customizing Replacement Options in Dynamic Shortcode Values

The plugin provides replacement values to cover the most common 80% cases out of the box. However, for your custom needs you may want to add new or adjust existing options and this you can do easily.

Step 1. Declare new or adjust existing replacement options

The plugin applies the following filter to allow you to adjust the replacement options:

$value_options = apply_filters( 'cptemplates/dynamic_shortcode_values/value_options', $value_options );

Removing Single Option

Let's say for example, that you want to remove a single replacement option "Modified Date" under set "Post" in "Common" group.

add_filter( 'cptemplates/dynamic_shortcode_values/value_options', 'mydsv_remove_unused_options', 20 );
function mydsv_remove_unused_options( $value_options ) {
unset( $value_options[ 'post' ][ 'choices' ][ __( 'Basic', 'cptemplates' ) ][ 'modified' ] );
return $value_options;
}

Removing Whole Options Set

Let's say for example, that you think the whole options set "Author" under "Common" group is redundant and want to remove that.

add_filter( 'cptemplates/dynamic_shortcode_values/value_options', 'mydsv_remove_unused_options_set', 20 );
function mydsv_remove_unused_options_set( $value_options ) {
unset( $value_options[ 'author' ] );
return $value_options;
}

Adding New Options

If you want to add support for custom features of your theme or plugin, you may need to register the whole new set of replacement options (optionally under a new group).

add_filter( 'cptemplates/dynamic_shortcode_values/value_options', 'mydsv_add_custom_options', 20 );
function mydsv_add_custom_options( $value_options ) {

// Let's say here you're adding support for your theme's custom functions
$value_options[ 'my_xyz_theme_support' ] = array(
'label' => __( 'My XYZ Theme', 'cptemplates' ),
'type' => 'dropdown',
'group' => __( 'My XYZ Theme', 'cptemplates' ),
'choices' => array(

__( 'Theme Options', 'cptemplates' ) => array(
'projects_per_row' => __( 'Projects per row', 'cptemplates' ),
'accent_color' => __( 'Accent Color', 'cptemplates' ),
),

__( 'Project', 'cptemplates' ) => array(
'project_mega_gallery' => __( 'Project Mega Gallery', 'cptemplates' ),
'project_notes' => __( 'Project Notes', 'cptemplates' ),
),
),
);

// and here you're adding support for included plugins' functions
$value_options[ 'my_xyz_plugins_support' ] = array(
'label' => __( 'My XYZ Theme Plugins', 'cptemplates' ),
'type' => 'dropdown',
'group' => __( 'My XYZ Theme', 'cptemplates' ),
'choices' => array(

__( 'WooCommerce', 'cptemplates' ) => array(
'theme_custom_feature' => __( 'Theme custom feature', 'cptemplates' ),
),

),
);

return $value_options;
}

This will create new group, options set and options. See screenshots below:

Step 2. Define replacement value functions

The plugin is using the following filter to retrieve a replacement value, where:

  • null is passed as the initial value
  • $replacement_type is the current replacement options set, e.g. "my_xyz_theme_support" or "my_xyz_plugins_support" from the example above
  • $replacement_name is the replacement option name, e.g. "projects_per_row" or "theme_custom_feature" from the example above
$value = apply_filters( "cptemplates/dynamic_shortcode_values/get_{$replacement_type}_value", null, $replacement_name, $tag, $atts );

Let's say you're writing function for retrieving "projects_per_row" value:

add_filter( "cptemplates/dynamic_shortcode_values/get_my_xyz_theme_support_value", 'get_my_xyz_theme_support_value', 10, 2 );

function get_my_xyz_theme_support_value( $value, $name ) {
switch ( $name ) {
case 'projects_per_row':
$value = get_option( 'my_xyz_theme_projects_per_row' );
break;
}

return $value;
}

For better understanding how to write own functions and to better organize them, it is recommended to check files under plugin's folder/integrations/dynamic-shortcode-values/

Recent Posts