Forked from bennemann/gravity-view-claim-entry-by-user-data.php
Last active
February 17, 2026 02:13
-
-
Save zackkatz/8a214c102c174c95dddab8ce1c324f30 to your computer and use it in GitHub Desktop.
Modify whether the currently logged-in user can edit an entry that was not created by them
This file contains hidden or 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 | |
| add_filter( 'gravityview/edit_entry/user_can_edit_entry', 'gv_claim_entry_by_user_data', 20, 3 ); | |
| /** | |
| * Allow logged-in users to edit entries where their user data matches a field value in the entry. | |
| * | |
| * Supports multiple Views, each with its own field to check against the current user's display name. | |
| * | |
| * @see https://docs.gravitykit.com/article/520-allow-user-edit-field-match | |
| * | |
| * @param bool $user_can_edit Whether the current user can edit this entry. | |
| * @param array|WP_Error $entry The Gravity Forms entry array, or WP_Error if the entry could not be found. | |
| * @param int $view_id The ID of the GravityView View being displayed. | |
| * | |
| * @return bool True if the user's data matches the entry field; otherwise, the existing value. | |
| */ | |
| function gv_claim_entry_by_user_data( $user_can_edit, $entry, $view_id = 0 ) { | |
| // If the entry is a WP_Error, we can't check field values — return existing permissions. | |
| if ( is_wp_error( $entry ) ) { | |
| return $user_can_edit; | |
| } | |
| /** | |
| * Configure your Views below. | |
| * | |
| * Format: View ID => Field ID to check | |
| * | |
| * - View ID: Found in the URL when editing a View (e.g., post.php?post=9840 means 9840) | |
| * - Field ID: The Gravity Forms field ID whose value should match the logged-in user's data. | |
| * Find this in the Form Editor by clicking on a field — the ID is shown in the | |
| * field settings panel. | |
| * | |
| * Add as many Views as you need — just add a new line for each one. | |
| */ | |
| $views_config = array( | |
| 9840 => 4, // View ID 9840 checks field 4 | |
| 1234 => 7, // View ID 1234 checks field 7 | |
| // 5678 => 3, // Uncomment and change to add another View | |
| ); | |
| // If this View isn't in our list, return existing permissions | |
| if ( ! array_key_exists( (int) $view_id, $views_config ) ) { | |
| return $user_can_edit; | |
| } | |
| if ( ! is_user_logged_in() ) { | |
| return $user_can_edit; | |
| } | |
| $id_field_to_check = $views_config[ (int) $view_id ]; | |
| if ( ! isset( $entry[ $id_field_to_check ] ) || ! $entry[ $id_field_to_check ] ) { | |
| return $user_can_edit; | |
| } | |
| global $current_user; | |
| /** | |
| * This compares the entry's field value to the logged-in user's display name. | |
| * | |
| * To match a different user property, change `$current_user->display_name` to one of: | |
| * - $current_user->user_email (match by email address) | |
| * - $current_user->user_login (match by username) | |
| * - $current_user->ID (match by user ID — field must store a numeric ID) | |
| * - $current_user->first_name (match by first name) | |
| * - $current_user->last_name (match by last name) | |
| */ | |
| if ( $current_user->display_name === $entry[ $id_field_to_check ] ) { | |
| return true; | |
| } | |
| return $user_can_edit; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment