Passing a variable value to a hidden field in Frontend Form Submission
Support › MB Frontend Submission › Passing a variable value to a hidden field in Frontend Form SubmissionResolved
- This topic has 7 replies, 3 voices, and was last updated 2 years, 2 months ago by
Fernando Arbex.
-
AuthorPosts
-
November 6, 2022 at 8:07 PM #38968
Denise Field
ParticipantHi I have a form that I want a user to submit and I have 3 fields,
user_name
,registered_email
andcheckbox_membership
.I want to pre-fill the
user_name
with$current_user->user_login
and theregistered_email
with
$current_user->user_email
I dont know how to do this, can you help?
<div class='user_membership--form'>"; global $current_user; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . "</br>"; echo 'User email: ' . $current_user->user_email . "</br>"; echo 'User first name: ' . $current_user->user_firstname . "</br>"; echo 'User last name: ' . $current_user->user_lastname . "</br>"; echo 'User display name: ' . $current_user->display_name . "</br>"; echo 'User ID: ' . $current_user->ID . "</br>"; echo do_shortcode ('[mb_frontend_form id="request-membership-status"]') ."</div>";
November 7, 2022 at 10:40 PM #38987Peter
ModeratorHello Denise,
When registering the meta box and custom fields, you can get the user name with your code and set the default value with the name. For example:
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) { // your code to get the current user name here $user_name = 'Default user name'; $meta_boxes[] = [ ... 'fields' => [ [ 'name' => 'User Name', 'id' => 'user_name', 'type' => 'text', 'std' => $user_name ], ], ]; return $meta_boxes; } );
Read more on the documentation https://docs.metabox.io/creating-fields-with-code/#field-settings
November 8, 2022 at 3:13 AM #38995Denise Field
ParticipantThanks I did see that on the docs, but thought it was more of a general setting and I wondered about conflicts - e.g all user name fields as user_name that it would cause an issue in the post_meta table.
However, if I am understanding what you are saying is that I could have several fields e.g.
user_name1
,user_name2
etc and then declare them each in here with the same variable name in this case$user_name
.Thank you for this, I've literally read so much php/sass/jquery etc my eyes are going funny! Sometimes you can't see the wood, for the trees!
November 8, 2022 at 4:57 AM #38996Denise Field
ParticipantI think I must be doing something wrong? The code isnt populating the fields on the form, do I need to add something to the shortcode?
Here is the function I created
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) { // your code to get the current user name here global $current_user; get_currentuserinfo(); $user_name = $current_user->display_name; $user_email = $current_user->user_email; $user_first = $current_user->user_firstname; $user_last = $current_user->user_lastname; $meta_boxes[] = [ 'fields' => [ [ 'name' => 'username', 'id' => 'user_name', 'type' => 'text', 'std' => $user_name ], [ 'name' => 'email', 'id' => 'user_email', 'type' => 'text', 'std' => $user_email ], [ 'name' => 'firstname', 'id' => 'user_firstname', 'type' => 'text', 'std' => $user_firstname ], [ 'name' => 'lastname', 'id' => 'user_lastname', 'type' => 'text', 'std' => $user_lastname ], ], ]; return $meta_boxes; } );
Can you help?
November 8, 2022 at 5:55 AM #38997Denise Field
ParticipantEDIT: I corrected a typo on the last 2 variables.
Here is how I am calling the form and that shows up exactly as expected
echo do_shortcode ("[mb_frontend_form id='request-membership-status' ]")
And here is one of the form fields to be populated with the variables
ScreenshotNovember 8, 2022 at 4:06 PM #38999Denise Field
ParticipantMorning, can I just clarify, is there a way, using MB Builder and Frontend Form Submission, to set the
std
to a variable?November 8, 2022 at 8:59 PM #39001Denise Field
ParticipantI solved it myself thank you, here is how I did it , for anyone else wishing to do this that has some level of skill but not a developer.
1. Created the Custom Field Types using the Metabox interface. Set them up exactly as I want, without the default value.
2. ClickedGet PHP Code
and copied that code, minus the<?php
tag into my functions.php
3. Added my PHP query to get the user details
`global $current_user;
get_currentuserinfo();
$user_name = $current_user->display_name;
$user_email = $current_user->user_email;
$user_first = $current_user->user_firstname;
$user_last = $current_user->user_lastname; `4. added the
'std' => $user_name,
etc etc to each field to show the correct variable
5. output the shortcode of the form<?php echo do_shortcode("[mb_frontend_form id='request-membership-status' ]") ?>
Job Done!!!
Here is the complete function I created so if anyone else wants to follow it they can see how it has to be set up as a working function for theMB Frontend Submission
form.add_filter( 'rwmb_meta_boxes', 'request_membership_status' ); function request_membership_status( $meta_boxes ) { $prefix = ''; // your code to get the current user name here global $current_user; get_currentuserinfo(); $user_name = $current_user->display_name; $user_email = $current_user->user_email; $user_first = $current_user->user_firstname; $user_last = $current_user->user_lastname; $meta_boxes[] = [ 'title' => __( 'Request Membership Status', 'pinkequine-com' ), 'id' => 'request-membership-status', 'post_types' => ['membership-request'], 'fields' => [ [ 'name' => __( 'First Name', 'pinkequine-com' ), 'id' => $prefix . 'user_first', 'type' => 'text', 'admin_columns' => 'replace title', 'std' => $user_first, ], [ 'name' => __( 'Last Name', 'pinkequine-com' ), 'id' => $prefix . 'user_last', 'type' => 'text', 'admin_columns' => 'after user_first', 'std' => $user_last, ], [ 'name' => __( 'Username', 'pinkequine-com' ), 'id' => $prefix . 'user_name', 'type' => 'text', 'admin_columns' => 'after user_last', 'std' => $user_name, ], [ 'name' => __( 'Email', 'pinkequine-com' ), 'id' => $prefix . 'user_email', 'type' => 'text', 'admin_columns' => 'after user_name', 'std' => $user_email, ], [ 'name' => __( 'Membership', 'pinkequine-com' ), 'id' => $prefix . 'checkbox_membership', 'type' => 'checkbox', 'label_description' => __( 'Please add user Member to my profile', 'pinkequine-com' ), 'required' => true, 'admin_columns' => 'after user_email', ], ], 'validation' => [ 'rules' => [ $prefix . 'checkbox_membership' => [ 'required' => true, ], ], 'messages' => [ $prefix . 'checkbox_membership' => [ 'required' => 'Please check the box to confirm your request', ], ], ], ]; return $meta_boxes; }
February 17, 2023 at 8:49 PM #40575Fernando Arbex
ParticipantVery good to see there's a solution.
I tried out myself but when I used the shortcode, it only displayed the title.Here“s the shortcode I used on Gutenberg.
[mb_frontend_form id="vendas" post_fields="title" label_title="Nome do cliente" submit_button="Cadastrar venda" edit="false" allow_delete="false" post_status="publish" redirect="" confirmation="Cadastro feito com sucesso"]
What could be?
-
AuthorPosts
- You must be logged in to reply to this topic.