Support Forum
Support › MB Frontend Submission › User Display Name instead of ID in MB Frontend SubmissionResolved
Hello 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.
Hi,
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 to post_title
(standard field of WP). You can use the function wp_update_post()
and get_userdata()
to update the post title based on the user ID and hook the callback function to the action rwmb_{$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-33677
And 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/
Hi 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.
Hi 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.