Filter Since 1.0.0 uncanny-learndash-groups

ulgm_add_role_capabilities

Filters role based capabilities before being added Filters role capabilities before they are assigned to modify permissions.

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

Description

This filter hook allows developers to modify the capabilities assigned to specific WordPress roles before they are applied. Use it to customize which roles can access plugin features, ensuring granular control over user permissions for your group management plugin.


Usage

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

Parameters

$set_role_capabilities (string)
Path to the plugins template folder

Return Value

The filtered value.


Examples

/**
 * Example of filtering the 'ulgm_add_role_capabilities' hook.
 * This function demonstrates how to add a new capability to the 'group_editor' role
 * and also remove a capability from the 'administrator' role.
 *
 * @param array $capabilities The original array of role capabilities.
 * @return array The modified array of role capabilities.
 */
add_filter( 'ulgm_add_role_capabilities', function( $capabilities ) {

	// Add a new capability 'ulgm_edit_group_settings' to the 'group_editor' role.
	// If the 'group_editor' role doesn't exist, it will be created by WordPress
	// when this capability is added.
	if ( ! isset( $capabilities['group_editor'] ) ) {
		$capabilities['group_editor'] = array();
	}
	$capabilities['group_editor'][] = 'ulgm_edit_group_settings';

	// Remove the 'ulgm_group_management' capability from the 'administrator' role.
	// This is an example of limiting functionality for a default role.
	if ( isset( $capabilities['administrator'] ) ) {
		$capabilities['administrator'] = array_diff( $capabilities['administrator'], array( 'ulgm_group_management' ) );
	}

	// If after removing capabilities, an array for a role becomes empty,
	// we might choose to remove the role entry altogether, depending on desired behavior.
	// In this case, we'll keep it if it was originally there but now empty.

	return $capabilities;
}, 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/class-load-groups.php:433
src/classes/class-setup.php:307

public function groups_capabilities() {

		// Set which roles will need access
		$set_role_capabilities = array(
			'group_leader'  => array( 'ulgm_group_management' ),
			'administrator' => array( 'ulgm_group_management' ),
		);

		/**
		 * Filters role based capabilities before being added
		 *
		 * @param string $set_role_capabilities Path to the plugins template folder
		 *
		 * @since 1.0.0
		 */
		$set_role_capabilities = apply_filters( 'ulgm_add_role_capabilities', $set_role_capabilities );

		include_once __DIR__ . '/includes/capabilities.php';
		$capabilities = new Capabilities( $set_role_capabilities );
		$capabilities->add_capabilities();
	}


Scroll to Top