Frontend Submission --> Custom Title and Slug from Fields
Support › MB Frontend Submission › Frontend Submission --> Custom Title and Slug from FieldsResolved
- This topic has 3 replies, 3 voices, and was last updated 1 month ago by
[email protected].
-
AuthorPosts
-
July 4, 2023 at 2:08 AM #42459
[email protected]
ParticipantHi there,
I am very much out of my depth with MetaBox and PHP in general.
The use case:
I have a front-end submission form I would like to extract two fields from to use as the Post Title and the Post Slug (I believe that's post_name).I am able to get the Post Title updated, but not the slug. I have tried piecing together things I've found in the forum, but all of the variations I have tried have not worked.
This is what I have so far:
add_action( 'rwmb_frontend_after_save_post', 'update_bio_post_title'); function update_bio_post_title( $object ) { if ( 'bio' == $post_type ){ $post_type = $object->post_type; $post_id = $object->post_id; // Get the field value $my_meta = rwmb_meta( 'counselor_full_name', '', $post_id ); // Get the post title $my_post_title = get_the_title( $post_id ); // Preprare update post $post_slug = sanitize_title_with_dashes ($my_post_title,'','save'); $post_slugsan = sanitize_title($post_slug); $my_post_title = array( 'ID' => $post_id, 'post_title' => $my_meta, 'post_name' => $post_slugsan, ); wp_update_post( $my_post_title );} } ?>The Field ID for field I would like to use for the Post Title is:
counselor_full_nameThe slug I would like to take the name and simply add dashes, like you would get when creating a Title natively with spaces. For example: Counselor_Full_Name = Matthew Johnson --> matthew-johnson
Other relevant information:
The Post Type = bio
The Field Group ID for the custom fields = counselor-bioAlso, I'm not sure if this really applies in my case, but I saw a similar post I tried replicating which had an if condition, such that the post slug would only be rewritten for the specific post type. I would like to prevent this function from overriding any other custom post types I might have. I was unable to seemingly get that to work, but I feel like I have bigger issues at the moment before solving that mystery.
If you can kindly provide the code for where I went wrong based on the above, it would be GREATLY appreciated!
July 4, 2023 at 11:44 PM #42473Peter
ModeratorHello,
There are two things that I suggest you can try:
- Get the post type before using it:
$post_type = $object->post_type; if ( 'bio' == $post_type ){ ...- Sanitize the field value for the slug
// Get the field value $my_meta = rwmb_meta( 'counselor_full_name', '', $post_id ); // Preprare update post $post_slugsan = sanitize_title($my_meta);July 7, 2023 at 8:24 AM #42512[email protected]
ParticipantThank you! That worked like a charm. I'm not sure if this is the most optimized code, but in case anyone stumbles upon this, here is the final PHP code for the desired result:
<div> add_action( 'rwmb_frontend_after_save_post', 'update_bio_post_title'); function update_bio_post_title( $object ) { $post_type = $object->post_type; if ( 'bio' == $post_type ){ $post_id = $object->post_id; // Get the field value $my_meta = rwmb_meta( 'counselor_full_name', '', $post_id ); // Get the post title $my_post_title = get_the_title( $post_id ); // Prepare update post $post_slug = sanitize_title_with_dashes ($my_post_title,'','save'); $post_slugsan = sanitize_title($my_meta); $my_post_title = array( 'ID' => $post_id, 'post_title' => $my_meta, 'post_name' => $post_slugsan, ); wp_update_post( $my_post_title );} } </div>November 3, 2025 at 8:02 PM #49271[email protected]
ParticipantI know this is an older post, but thought I would add to it, as it is a great feature that MB has.
In my case I have a form that is capturing submissions, so like a contact form. I created a custom post type of "Submissions" and then added the custom fields I wanted on that form.
Now, I did not want the Post Title to be on the frontend, but if you remove it you get an error and if you hide it, the submission captures and empty field. So I took the code above that John submitted an modified it a bit. Now when the form is submitted the Post Title entry is the user's First and Last name.
Hope this helps. =]
add_action( 'rwmb_frontend_after_save_post', 'update_submission_post_title'); function update_submission_post_title( $object ) { $post_type = $object->post_type; if ( 'submission' == $post_type ) { $post_id = $object->post_id; // Get both field values $first_name = rwmb_meta( 'first_name', '', $post_id ); $last_name = rwmb_meta( 'last_name', '', $post_id ); // Combine the names $full_name = trim( $first_name . ' ' . $last_name ); // Only update if we have a name if ( ! empty( $full_name ) ) { // Prepare update post $my_post_title = array( 'ID' => $post_id, 'post_title' => $full_name, 'post_name' => sanitize_title( $full_name ), ); wp_update_post( $my_post_title ); } } } -
AuthorPosts
- You must be logged in to reply to this topic.