Frontend Submission --> Custom Title and Slug from Fields

Support MB Frontend Submission Frontend Submission --> Custom Title and Slug from FieldsResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #42459
    johnsonmr90@gmail.com[email protected]
    Participant

    Hi 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_name

    The 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-bio

    Also, 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!

    #42473
    PeterPeter
    Moderator

    Hello,

    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);
    
    #42512
    johnsonmr90@gmail.com[email protected]
    Participant

    Thank 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>
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.