Integrating WP function with a hook.

By: patrick joyce | Asked: 09/15/2023
ForumsCategory: Code SnippetsIntegrating WP function with a hook.
patrick joyce asked 11 months ago
Hi I am trying to capture which user updates a form and input that current users last name into a hidden field. on the form. I think integrating a WP function to retrieve the current user into a hook might work, but I am not experienced in this and am not sure how to proceed. I put a hook I've used before but put pre determined values in the "New Value" ideally i would put the current "user_username" for the new value but not sure how to do that.   Word Press Functions   wp_get_current_user(): WP_User     <?php
$current_user = wp_get_current_user(); /*
* @example Safe usage: $current_user = wp_get_current_user();
* if ( ! ( $current_user instanceof WP_User ) ) {
* return;
* }
*/
printf( __( 'Username: %s', 'textdomain' ), esc_html( $current_user->user_login ) ) . '<br />';
printf( __( 'User email: %s', 'textdomain' ), esc_html( $current_user->user_email ) ) . '<br />';
printf( __( 'User first name: %s', 'textdomain' ), esc_html( $current_user->user_firstname ) ) . '<br />';
printf( __( 'User last name: %s', 'textdomain' ), esc_html( $current_user->user_lastname ) ) . '<br />';
printf( __( 'User display name: %s', 'textdomain' ), esc_html( $current_user->display_name ) ) . '<br />';
printf( __( 'User ID: %s', 'textdomain' ), esc_html( $current_user->ID ) );   The Hook I was thinking about using   add_filter('frm_setup_edit_fields_vars', 'frm_set_edit_val', 20, 3);
function frm_set_edit_val( $values, $field, $entry_id ) {
if ( $field->id == 171 ) { //Replace 171 with your field ID
// If on the back-end, don't adjust field value
if ( FrmAppHelper::is_admin() ) {
return $values;
} // If on front-end set a specific field value
$values['value'] = 'new value';
}
return $values;
}
1 Answers
Victor Font answered 11 months ago
This is a lot of work when Formidable provides an easier method for retrieving these details. Do you have the registration add-on installed? If so, you can use the [user_meta] shortcode to populate the default values for the fields you want from the WordPress user table like this, [user_meta key="first_name"], or you can use Formidable's default value shortcodes like this, [first_name]. See this Formidable KnowledgeBase article for further details: https://formidableforms.com/knowledgebase/user-registration/#kb-autopopulate-forms
patrick joyce replied 11 months ago

I currently don't have the registration add on installed I would have to upgrade my plan. So I have used the default value short codes which pulls the user who initially submits the form into a field. But I could not get it to work when a different user goes in and edits a form on the front end. would either the default value shortcodes or the user_meta work for this?

Victor Font replied 11 months ago

Default values fire when an entry is created, not edited. If you look at Formidable's Database schema, you'll see that it stores entry ids for both the original creator and the last person to edit the entry in the frm_items table. The frm_items table is the entry's header record.

All other form data is metadata stored in the frm_item_metas table, where each entry field's value is one table row. If you have 10 fields on a form and all 10 fields have values submitted, there will be 10 rows for that entry. Formidable does not store metadata records for fields without a default value that have not been populated by an end user.

Your approach could work assuming the user is logged in when the form displays. The hook you want to use, frm_setup_edit_fields_vars, fires before the form hits the browser, so you can update the field values before they are sent to the browser. When the submit button is clicked, Formidable will update the updated_by field in the database and your hidden fields will be stored as metadata.

here's the database schema: https://formidableforms.com/knowledgebase/database-schema/

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