How to set the value of a hidden field in an entry to show on view

By: Naomi Spirit | Asked: 10/23/2024
ForumsCategory: General questionsHow to set the value of a hidden field in an entry to show on view
Naomi SpiritNaomi Spirit asked 7 days ago

I want to record some day-specific information in a user's entry so that when I display their entry using a view, I can display this information.  My plan was:

  1. Use the action from the entry being created - frm_after_create_entry
  2. Call a function which changes the value of some hidden fields in the form
  3. Use the normal view to display these 

I cannot find the function in the help docs to actually set a value.  I have found the ones to get a value, but I need to set it. 

This is the function ultimately being called from the user having added and entry, and it works as expected.  Just need the code to actually change a value, or understand what would be an alternative way of approaching this.

This help documentation https://formidableforms.com/knowledgebase/set-values-to-be-used-in-custom-displays-or-default-values/ doesn't show any usable PHP, only shortcodes.

 

function tw_add_day_data_to_entry($entry_id, $form_id ){
  $entry = FrmEntry::getOne($entry_id);
  $user_id = $entry->user_id;
if ( ! $user_id ) { return; //don't continue if no user } // $day = tw_return_status(); $url = tw_home_page()."/day-".$day; echo "Day= ".$day; echo "URL= ".$url; // at this point they return a value $dayURL = FrmProEntriesController::get_field_value_shortcode(array('field_id' ='IDn8jec', 'entry' => $entry_id)); echo $dayURL; $day = FrmProEntriesController::get_field_value_shortcode(array('field_id' ='9q904', 'entry' => $entry_id)); echo $day; }
2 Answers
Best Answer
Naomi SpiritNaomi Spirit answered 6 days ago
Think I've sorted this.  Was using the key instead of the id (although key would be better) Found the code to set a value Would be great to have that on the documentation. FrmEntryMeta::update_entry_meta( $entry_id, $dayFieldID, null, $day );
Rob LeVineRob LeVine replied 6 days ago

Note: that update_entry_meta will only work if there's a value already in there for that field. Most of the time it's best to run the update and if false is returned then run FrmEntryMeta::add_entry_meta

Rob LeVineRob LeVine answered 6 days ago
The code that was commented out will do it (it was in the notification email for this post but you apparently edited it out of the post). FrmEntryMeta::add_entry_meta( $entry_id, 1819, "", $key) (obviously replacing the 2nd and 4th params with your own). You can also always use straight SQL commands. Additionally, those echo commands might cause you a problem somewhere along the way. If you're doing it for debugging purposes it's better to turn on WP debugging and use error_log.
Naomi SpiritNaomi Spirit replied 6 days ago

Thanks, any idea how I can use the key instead of ID as this would make it so much more versatile for moving the code into prodcution.

Rob LeVineRob LeVine replied 6 days ago

Agreed, you should always use keys. I feel strongly that you should always (re)name your keys to know what they are when you're looking at them in code, i.e.., [form_key]_somethingdescriptive, e.g., myformkey_user_first_name. That being said, what you want to do is make use of the key->id and id->key functions that Formidable offers. https://formidableforms.com/knowledgebase/php-examples/#kb-fields

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right