Created
July 24, 2026 08:25
-
-
Save herbie4/510648ac65186ad0b8dd95fb7372a42d to your computer and use it in GitHub Desktop.
Gravity Forms: encrypt and decrypt entry/fields
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
| // Gravity Forms: encrypt and decrypt form field | |
| // runs only on form id 9 | |
| // change it to you own form id(x) : gform_save_field_value_x and gform_get_input_value_x | |
| // change it to you own form id(x) and field (y) : gform_save_field_value_x and gform_get_input_value_x_y | |
| // info: https://docs.gravityforms.com/gform_get_input_value/ | |
| // info: https://docs.gravityforms.com/gform_save_field_value/ | |
| // -------------------------- | |
| // add it to wp-config or leave it here... | |
| // add your own key here! | |
| define('HHDEV_GF_ENCRYPTION_KEY','add 48 characters key'); | |
| // Encrypt | |
| // ------------------------ | |
| function hhdev_gravityforms_encrypt( $to_encrypt ) | |
| { | |
| if ( '' === $to_encrypt ) return null; | |
| if ( function_exists('openssl_encrypt') && function_exists('openssl_random_pseudo_bytes') ) | |
| { | |
| $iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) ); | |
| $encrypted = openssl_encrypt( $to_encrypt, 'aes-256-cbc', HHDEV_GF_ENCRYPTION_KEY, 0, $iv ); | |
| return base64_encode( $encrypted . '::' . $iv ); | |
| } | |
| return base64_encode( $to_encrypt ); | |
| } // end hhdev_gravityforms_encrypt | |
| // Decrypt | |
| // ------------------------- | |
| function hhdev_gravityforms_decrypt( $to_decrypt ) | |
| { | |
| if ( '' === $to_decrypt ) return null; | |
| if ( function_exists('openssl_decrypt') ) | |
| { | |
| @list( $encrypted_data, $iv ) = explode( '::', base64_decode( $to_decrypt ), 2 ); | |
| return openssl_decrypt( $encrypted_data, 'aes-256-cbc', HHDEV_GF_ENCRYPTION_KEY, 0, $iv ); | |
| } | |
| return base64_decode( $to_decrypt ); | |
| } | |
| // gravity forms functions | |
| // -------------------------- | |
| /** | |
| * Encrypt all values for GF entries | |
| **/ | |
| add_filter( 'gform_save_field_value_9', 'hhdev_gf_save_field_value', 10, 4 ); | |
| function hhdev_gf_save_field_value( $value, $lead, $field, $form ) | |
| { | |
| return hhdev_gravityforms_encrypt( $value ); | |
| } | |
| // Always decrypt | |
| // ---------------------------- | |
| add_filter( 'gform_get_input_value_9', 'hhdev_gf_decrypt_field', 10, 4 ); | |
| function hhdev_gf_decrypt_field( $value, $entry, $field, $input_id ) | |
| { | |
| return hhdev_gravityforms_decrypt( $value ); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment