uo_toolkit_settings_config_value
Filters the value of a toolkit setting before it's saved or displayed, allowing customization based on the setting key and arguments.
add_filter( 'uo_toolkit_settings_config_value', $callback, 10, 3 );
Description
Allows modification of a specific settings value before it's returned. Filter the default value, the setting key, or additional arguments passed to the function. Useful for dynamically altering stored settings or providing custom defaults based on context.
Usage
add_filter( 'uo_toolkit_settings_config_value', 'your_function_name', 10, 3 );
Parameters
-
$default(mixed) - This parameter represents the default value for a setting if it's not found or explicitly set.
-
$key(mixed) - This parameter holds the default value for the setting if it's not found in the saved configurations.
-
$args(mixed) - This parameter represents the unique identifier or name of the setting being retrieved.
Return Value
The filtered value.
Examples
add_filter( 'uo_toolkit_settings_config_value', 'my_custom_settings_config', 10, 3 );
/**
* Example callback for the uo_toolkit_settings_config_value filter.
* This function demonstrates how to modify a setting's value or default
* based on its key and associated arguments.
*
* @param mixed $default The default value of the setting.
* @param mixed $key The key of the setting.
* @param mixed $args An array of arguments passed to the get_settings_value function.
* Expected keys: 'key', 'class', 'default', 'class_settings'.
* @return mixed The modified setting value.
*/
function my_custom_settings_config( $default, $key, $args ) {
// Example: If the setting key is 'api_key' for the 'My_Integration_Class',
// and it's currently empty, prepend a warning message to the default value.
if ( isset( $args['class'] ) && 'My_Integration_Class' === $args['class'] && 'api_key' === $key ) {
if ( empty( $default ) || '%' === substr( $default, 0, 1 ) ) { // Check if it's empty or a placeholder
// Ensure we are returning a string to concatenate
if ( ! is_string( $default ) ) {
$default = (string) $default;
}
return '<strong>Warning:</strong> API Key not configured. Please set it in integration settings.';
}
}
// Example: For a specific setting related to social sharing,
// ensure its default value is never an empty string.
if ( 'social_share_buttons' === $key && 'My_Theme_Features' === $args['class'] ) {
if ( empty( $default ) ) {
return 'default'; // Provide a non-empty default if it's empty
}
}
// If no specific modification is needed, return the original value.
return $default;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/config.php:798
src/config.php:803
src/config.php:809
public static function get_settings_value( $key, $class, $default = '', $class_settings = array() ) {
$args = array(
'key' => $key,
'class' => $class,
'default' => $default,
'class_settings' => $class_settings,
);
// get module settings key
$class = str_replace( __NAMESPACE__, '', stripslashes( $class ) );
// Get all module settings
$options = get_option( $class, '' );
// set default settings if placeholder is to be used as default
if ( '%placeholder%' === $default ) {
// fallback
//$default = '';
foreach ( $class_settings as $setting ) {
if ( isset( $setting['option_name'] ) && $key === $setting['option_name'] ) {
if ( isset( $setting['placeholder'] ) ) {
$default = $setting['placeholder'];
}
}
}
}
// Check if setting key has an associated class
if ( ! empty( $options ) && '' !== $options ) {
foreach ( $options as $option ) {
if ( in_array( $key, $option, true ) ) {
if ( '' !== $default && '' === trim( $option['value'] ) ) {
return apply_filters( 'uo_toolkit_settings_config_value', $default, $key, $args );
}
$value = stripslashes( $option['value'] );
return apply_filters( 'uo_toolkit_settings_config_value', $value, $key, $args );
}
}
}
return apply_filters( 'uo_toolkit_settings_config_value', $default, $key, $args );
}