Filter uncanny-learndash-toolkit

toolkit_settings_load_cap

Filters the capability required to load toolkit settings, allowing customization of access permissions.

add_filter( 'toolkit_settings_load_cap', $callback, 10, 1 );

Description

This filter hook allows developers to modify the capability required to load Uncanny Toolkit settings via AJAX. By default, it's set to 'manage_options'. You can change this to a different capability, enabling finer-grained control over who can access and modify plugin settings, useful for multi-user sites.


Usage

add_filter( 'toolkit_settings_load_cap', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

<?php
/**
 * Example of modifying the capability required to load toolkit settings.
 *
 * This filter allows administrators to restrict who can load and view
 * the Uncanny Toolkit settings. For instance, you might want to
 * only allow users with a specific custom capability to access these settings.
 *
 * @param string $capability The capability required to load settings. Defaults to 'manage_options'.
 * @return string The modified capability.
 */
add_filter(
	'toolkit_settings_load_cap',
	function( $capability ) {
		// Example: Allow users with the 'edit_users' capability to also load settings.
		// In a real-world scenario, you might check for a custom capability.
		if ( current_user_can( 'edit_users' ) ) {
			return 'edit_users'; // This is just for demonstration, in practice, you might want to combine them or use a more specific capability.
		}

		// If the user doesn't have the custom capability, fall back to the default.
		return $capability;
	},
	10, // Priority
	1  // Accepted arguments
);

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:736

public static function ajax_settings_load() {
		// Nonce verification
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'uncanny_toolkit' ) ) {
			echo 'Nonce verification failed.';
			wp_die();
		}

		$capability = apply_filters( 'toolkit_settings_load_cap', 'manage_options' );
		if ( current_user_can( $capability ) ) {
			if ( isset( $_POST['class'] ) ) {
				$class    = sanitize_text_field( $_POST['class'] );
				$settings = get_option( $class, array() );
				foreach ( $settings as &$setting ) {
					$setting['value'] = stripslashes( $setting['value'] );
				}
				$response = wp_json_encode( $settings );
			} else {
				$response = __( 'Class for addon is not set.', 'uncanny-learndash-toolkit' );
			}
		} else {
			$response = __( 'You must be an admin to save settings.', 'uncanny-learndash-toolkit' );
		}
		echo $response;

		wp_die();

	}

Scroll to Top