Filter uncanny-continuing-education-credits

save_ceu_setting_update_permission

Filters the permission required to update CEU settings when they are saved, defaulting to 'manage_options'.

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

Description

Allows developers to modify the user capability required to save CEU settings. By default, 'manage_options' is used. You can filter this to enforce a different capability check before settings are saved, useful for custom user roles.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Filter 'save_ceu_setting_update_permission' to allow specific roles to save settings.
 *
 * By default, only users with the 'manage_options' capability can save CEU settings.
 * This filter allows you to grant permission to other roles or users based on specific conditions.
 *
 * @param string $permission The capability required to save settings. Defaults to 'manage_options'.
 * @return string The capability required to save settings.
 */
add_filter( 'save_ceu_setting_update_permission', function( $permission ) {
    // Get the current user object
    $current_user = wp_get_current_user();

    // Check if the user has the default 'manage_options' capability
    if ( user_can( $current_user, 'manage_options' ) ) {
        return $permission; // Keep the default permission
    }

    // Example: Allow users with the 'editor' role to save settings as well
    if ( in_array( 'editor', (array) $current_user->roles ) ) {
        return 'edit_posts'; // Grant 'edit_posts' capability which editors have
    }

    // Example: Grant permission based on a specific user meta key (e.g., a custom capability)
    // if ( get_user_meta( $current_user->ID, 'can_save_ceu_settings', true ) === 'yes' ) {
    //     return 'edit_users'; // Or any other capability you want to grant
    // }

    // If none of the above conditions are met, return the original permission,
    // which will likely cause the current_user_can() check to fail if the user doesn't have 'manage_options'.
    return $permission;
}, 10, 1 );

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/classes/admin-page.php:400

} else {
			$action          = '';
			$data['message'] = __( 'Select an action.', 'uncanny-ceu' );
			wp_send_json_error( $data );
		}

		// Does the current user have permission
		$permission = apply_filters( 'save_ceu_setting_update_permission', 'manage_options' );
		if ( ! current_user_can( $permission ) ) {
			$data['message'] = __( 'You do not have permission to save settings.', 'uncanny-ceu' );
			wp_send_json_error( $data );
		}

		if ( isset( $_POST['credit_designation_label'] ) ) {
			update_option( 'credit_designation_label', $_POST['credit_designation_label'] );

Scroll to Top