Add Custom Dynamic Variables
The following snippet will show you how to add your own custom dynamic variables which can be used inside theme elements or certain theme settings.
This filter returns an array where each key is the dynamic variable name and each value is what will be displayed on the live site. The value can be a static string, the name of a callback function or an actual function.
/**
* Add Custom Dynamic Variables.
*/
add_filter( 'totaltheme/replace_vars/vars', function( $vars ) {
// Add a new static custom var
$vars['my_custom_var'] = 'test';
// Add a new dynamic custom var that calls the function every time
$vars['my_custom_var_dynamic'] = 'my_custom_var_callback_function_name';
return $vars;
} );
/**
* Custom Dynamic Variables Callback function.
*/
function my_custom_var_callback_function_name() {
return 'Dynamic Value Test';
}All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Related Snippets