Advertisement
mbis

Prefill GravitForms

Aug 2nd, 2023
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.82 KB | None | 0 0
  1. /**
  2.  * Prefill Gravity Forms
  3.  */
  4. function bis_init_prefill_gf_fields() {
  5.     global $post;
  6.  
  7.     if ( ! empty( $post->ID ) ) {
  8.  
  9.         $post_meta = get_post_meta( $post->ID );
  10.  
  11.         $post_meta_keys = array_keys( $post_meta );
  12.  
  13.         if ( is_array( $post_meta_keys ) ) {
  14.             foreach ( $post_meta_keys as $meta_key ) {
  15.                 if ( preg_match( '/^prefill_gf_(.*)/', $meta_key, $matches ) ) {
  16.                     $gf_field_parameter = $matches[1];
  17.  
  18.                     add_filter( 'gform_field_value_' . $gf_field_parameter, 'bis_prefill_gf_fields', 10, 2 );
  19.                 }
  20.             }
  21.         }
  22.     }
  23. }
  24. add_action( 'wp', 'bis_init_prefill_gf_fields' );
  25.  
  26. function bis_prefill_gf_fields( $value, $field ) {
  27.     global $post;
  28.  
  29.     if ( ! empty( $field->inputName ) && ! empty( $post->ID ) ) {
  30.         $value = get_post_meta( $post->ID, 'prefill_gf_' . $field->inputName, true );
  31.     }
  32.  
  33.  
  34.     return $value;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement