Last active
October 29, 2021 12:09
-
-
Save luistinygod/ec515f50debcfcacee81 to your computer and use it in GitHub Desktop.
GravityView extend the Advanced Filter to a certain users' role
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* When using the Advanced Filter extension to filter entries to show just the ones created by the user logged-in | |
* BUT remove the restriction to a certain role of users ( administrators or editors ) | |
* | |
* Replace the following to your own values: | |
* - REPLACE_BY_VIEW_ID, use the view id where this logic applies | |
* - REPLACE_BY_THE_ROLE, use 'administrator' or 'editor' or your own custom role | |
* | |
* | |
* Add this gist to your theme functions.php | |
* | |
*/ | |
add_filter( 'gravityview_search_criteria', 'my_gv_custom_role_filter', 110, 1 ); | |
function my_gv_custom_role_filter( $criteria ) { | |
// check if we are displaying the correct View | |
if( function_exists('gravityview_get_view_id') && 'REPLACE_BY_VIEW_ID' != gravityview_get_view_id() ) { return $criteria; } | |
// Check if user is logged-in | |
if( !is_user_logged_in() ) { return $criteria; } | |
// Check if user belongs to a certain 'admin' role | |
$current_user = wp_get_current_user(); | |
if( !in_array( 'REPLACE_BY_THE_ROLE', $current_user->roles ) ) { return $criteria; } | |
// if we get here, then remove the 'create by' filter from the $criteria | |
if( !empty( $criteria['search_criteria']['field_filters'] ) && is_array( $criteria['search_criteria']['field_filters'] ) ) { | |
foreach( $criteria['search_criteria']['field_filters'] as $k => $filter ) { | |
if( $k !== 'mode' && 'created_by' === $filter['key'] ) { | |
unset( $criteria['search_criteria']['field_filters'][ $k ] ); | |
} | |
} | |
} | |
return $criteria; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment