Frontend Submission --> Custom Title and Slug from Fields
Support › MB Frontend Submission › Frontend Submission --> Custom Title and Slug from FieldsResolved
- This topic has 2 replies, 2 voices, and was last updated 1 year, 9 months 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>
-
AuthorPosts
- You must be logged in to reply to this topic.