Ignore Current User Filter for Administrators

If you’ve configured a filter based on the “Current User ID” option and wish to ignore that filter for administrators, this snippet will allow you to do so. Please note that all other filters will still be applied.

This is useful if you wish to show user-specific results to non-admins but want to show all results to admins.

Code

Filename: gppa-ignore-current-user-filter-for-admins.php

<?php
/**
 * Gravity Perks // Populate Anything // Ignore Current User Filter for Administrators
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * If you've configured a filter based on the "Current User ID" option and wish to ignore that filter for administrators,
 * this snippet will allow you to do so. Please note that all other filters will still be applied.
 *
 * This is useful if you wish to show user-specific results to non-admins but want to show all results to admins.
 */
add_filter( 'gppa_object_type_filter_after_processing', function ( $query_builder_args, $args ) {
	/**
	 * @var $field
	 * @var $filter_value
	 * @var $filter
	 * @var $filter_group
	 * @var $filter_group_index
	 * @var $property
	 * @var $property_id
	 */
	// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
	extract( $args );

	if ( $filter['value'] !== 'special_value:current_user:ID' || ! current_user_can( 'administrator' ) ) {
		return $query_builder_args;
	}

	array_pop( $query_builder_args['where'][ $filter_group_index ] );

	if ( empty( $query_builder_args['where'][ $filter_group_index ] ) ) {
		unset( $query_builder_args['where'][ $filter_group_index ] );
	}

	return $query_builder_args;
}, 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.