Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- This snippet restricts a category to a specific user role. You can modify the $restricted_role and $restricted_category_slug accordingly.
- For installation instructions, see our documentation site: http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
- */
- function restrict_event_categories_by_user_role($search_args) {
- // Get the current user
- $user = wp_get_current_user();
- // Define the role and category to restrict
- $restricted_role = 'contributor';
- $restricted_category_slug = 'contributors-only';
- // Check if the user does not have the restricted role
- if (!in_array($restricted_role, $user->roles)) {
- // Get the category term by slug
- $restricted_category = get_term_by('slug', $restricted_category_slug, 'event-categories');
- if ($restricted_category) {
- $restricted_category_id = '-' . $restricted_category->term_id; // Use negative ID to exclude
- // Ensure category is an array
- if (!is_array($search_args['category'])) {
- // If it's not an array, split it by commas or initialize it as an array if empty
- $search_args['category'] = !empty($search_args['category']) ? explode(',', $search_args['category']) : array();
- }
- // Check if the category is not already in the array (either included or excluded)
- if (!in_array($restricted_category_id, $search_args['category']) && !in_array(ltrim($restricted_category_id, '-'), $search_args['category'])) {
- // Add the restricted category to the array to exclude it
- $search_args['category'][] = $restricted_category_id;
- }
- // Convert the array back to a string if needed
- $search_args['category'] = implode(',', $search_args['category']);
- }
- }
- return $search_args;
- }
- add_filter('em_events_get_default_search', 'restrict_event_categories_by_user_role');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement