gform_add_meta

Add metadata associated with an entry.

Description

gform_add_meta( (int) $entry_id, (string) $meta_key, (mixed) $meta_value, (null) $form_id = null ); 

Data will be serialized; Don't forget to sanitize user input.

Parameters (4)

0. $entry_id (int)
The ID of the entry where metadata is to be added
1. $meta_key (string)
The key for the meta data to be stored
2. $meta_value (mixed)
The data to be stored for the entry
3. $form_id — Optional. (null) => null
The form ID of the entry (optional, saves extra query if passed when creating the metadata)

Usage

  1. if ( !function_exists( 'gform_add_meta' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'gravity-forms/forms_model.php'; 
  3.  
  4. // The ID of the entry where metadata is to be added 
  5. $entry_id = -1; 
  6.  
  7. // The key for the meta data to be stored 
  8. $meta_key = ''; 
  9.  
  10. // The data to be stored for the entry 
  11. $meta_value = null; 
  12.  
  13. // The form ID of the entry (optional, saves extra query if passed when creating the metadata) 
  14. $form_id = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = gform_add_meta($entry_id, $meta_key, $meta_value, $form_id); 
  18.  

Defined (1)

The function is defined in the following location(s).

/forms_model.php  
  1. function gform_add_meta( $entry_id, $meta_key, $meta_value, $form_id = null ) { 
  2. global $wpdb, $_gform_lead_meta; 
  3. $table_name = RGFormsModel::get_lead_meta_table_name(); 
  4. if ( false === $meta_value ) { 
  5. $meta_value = '0'; 
  6. $serialized_meta_value = maybe_serialize( $meta_value ); 
  7.  
  8. if ( empty( $form_id ) ) { 
  9. $lead_table_name = RGFormsModel::get_lead_table_name(); 
  10. $form_id = $wpdb->get_var( $wpdb->prepare( "SELECT form_id from $lead_table_name WHERE id=%d", $entry_id ) ); 
  11. } else { 
  12. $form_id = intval( $form_id ); 
  13.  
  14. $wpdb->insert( $table_name, array( 'form_id' => $form_id, 'lead_id' => $entry_id, 'meta_key' => $meta_key, 'meta_value' => $serialized_meta_value ), array( '%d', '%d', '%s', '%s' ) ); 
  15.