toolkit_settings_save_cap
Filters the capability required to save toolkit settings, allowing customization of who can access this functionality.
add_filter( 'toolkit_settings_save_cap', $callback, 10, 1 );
Description
This filter hook allows developers to modify the user capability required to save toolkit settings. By default, 'manage_options' is used. Developers can change this to a custom capability to restrict who can alter toolkit configurations.
Usage
add_filter( 'toolkit_settings_save_cap', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of how to modify the capability required to save settings.
*
* This example demonstrates how a plugin or theme could hook into
* 'toolkit_settings_save_cap' to require a more specific capability
* than the default 'manage_options' for saving settings.
*
* @param string $capability The current capability required.
* @return string The modified capability.
*/
add_filter( 'toolkit_settings_save_cap', 'my_custom_toolkit_settings_save_cap', 10, 1 );
function my_custom_toolkit_settings_save_cap( $capability ) {
// If the current user has the 'administrator' role, they can save settings.
// Otherwise, we stick with the default 'manage_options' capability.
// This example assumes you might want to allow administrators directly
// or a more granular capability if needed.
if ( current_user_can( 'administrator' ) ) {
// In a real scenario, you might want to return a custom capability
// like 'edit_uncanny_toolkit_settings' if you defined one.
// For simplicity, we'll just ensure administrators can save.
return 'manage_options';
}
// Fallback to the original capability if the administrator check isn't met.
return $capability;
}
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:675
public static function ajax_settings_save() {
// Nonce verification
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
echo wp_json_encode(
array(
'error' => true,
'message' => 'Nonce verification failed.',
)
);
wp_die();
}
$response = array(
'error' => true,
'message' => '',
);
$capability = apply_filters( 'toolkit_settings_save_cap', 'manage_options' );
if ( ! current_user_can( $capability ) ) {
echo wp_json_encode(
array(
'error' => true,
'message' => __( 'You must be an admin to save settings', 'uncanny-learndash-toolkit' ),
)
);
wp_die();
}
if ( ! isset( $_POST['class'] ) ) {
echo wp_json_encode(
array(
'error' => true,
'message' => __( 'Class for addon is not set', 'uncanny-learndash-toolkit' ),
)
);
wp_die();
}
$class = sanitize_text_field( $_POST['class'] );
$options = ( isset( $_POST['options'] ) ) ? $_POST['options'] : array();
// Validate action if any module need some values to set.
do_action( 'toolkit_settings_save_validation', $class, $options );
// Delete option and add option are called instead of update option because
// sometimes update value is equal to the existing value and a false
// positive is returned
delete_option( $class );
$save_settings = add_option( $class, $options );
$response['error'] = ! $save_settings;
if ( $save_settings ) {
$response['message'] = __( 'Settings saved successfully', 'uncanny-learndash-toolkit' );
} else {
$response['message'] = __( 'Something went wrong. Please, try again', 'uncanny-learndash-toolkit' );
}
echo wp_json_encode( $response );
wp_die();
}