User Display Name instead of ID in MB Frontend Submission
Support › MB Frontend Submission › User Display Name instead of ID in MB Frontend SubmissionResolved
- This topic has 3 replies, 2 voices, and was last updated 2 years, 11 months ago by
Fernando Arbex.
-
AuthorPosts
-
May 23, 2022 at 7:52 AM #36136
Fernando Arbex
ParticipantHello there, I created a shortcode to MB Frontend Submission like this below:
[mb_frontend_form id="celulas" post_fields="endereco,mapa,dia_da_semana,hora_celula,thumbnail" label_thumbnail="Foto da fachada" submit_button="Cadastrar célula" edit="false" allow_delete="false" post_status="publish" redirect="/perfil" confirmation="Muito obrigado"]
Then after submission of the form everything is ok except it using the WP user ID instead of Display name (image bellow)
I have setup the Custom Field as follow in image bellow:
The thing is, I couldn't figure a way to make it save as User Display Name instead of User ID.
Any idea?Thank you.
May 23, 2022 at 3:50 PM #36147Long Nguyen
ModeratorHi,
The field type
user
saved the user ID in the database, so it will display the user ID as the post title if you set the field ID topost_title
(standard field of WP). You can use the functionwp_update_post()
andget_userdata()
to update the post title based on the user ID and hook the callback function to the actionrwmb_{$meta_box_id}_after_save_post
. Refer to this topic
https://support.metabox.io/topic/append-a-related-posts-title-to-the-title-of-created-post/#post-33677And read more on the documentation https://docs.metabox.io/fields/user/#data
https://developer.wordpress.org/reference/functions/wp_update_post/
https://developer.wordpress.org/reference/functions/get_userdata/May 23, 2022 at 8:48 PM #36151Fernando Arbex
ParticipantHi long, thank you for the answer.
You are very helpful.I believe I'm getting close, but for some reason the title is blank.
Here is the code:
<?php add_action( 'rwmb_celulas_after_save_post', 'update_post_title'); function update_post_title( $post_id ) { // Get user display name $user_id = rwmb_meta( 'post_title' ); $user = get_userdata( $user_id ); $displayName = $user->display_name; // $displayName = rwmb_the_value( 'my_field_id', ['link' => false] ); $post_slug = sanitize_title_with_dashes ($my_post,'','save'); $post_slugsan = sanitize_title($post_slug); // Prepare update post $my_post = array( 'ID' => $post_id, 'post_title' => $displayName, 'post_name' => $post_slugsan, ); wp_update_post( $my_post ); }
What is wrong here?
Thank you very much.May 23, 2022 at 9:36 PM #36154Fernando Arbex
ParticipantHi Long,
Just figure out the issue, here's the code:
<?php add_action( 'rwmb_celulas_after_save_post', 'update_post_title'); function update_post_title( $post_id ) { // Get user display name $user = wp_get_current_user(); $displayName = $user->display_name; // $displayName = rwmb_the_value( 'my_field_id', ['link' => false] ); $post_slug = sanitize_title_with_dashes ($my_post,'','save'); $post_slugsan = sanitize_title($post_slug); // Prepare update post $my_post = array( 'ID' => $post_id, 'post_title' => $displayName, 'post_name' => $post_slugsan, ); wp_update_post( $my_post ); }
I really appreciate your help and effort.
-
AuthorPosts
- You must be logged in to reply to this topic.