Help with front-end editing

Support General Help with front-end editing

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #7663
    jcleavelandjcleaveland
    Participant

    Hi,

    I am trying to update the post_title from the front-end by joining two metabox fields (first name and last name) together.

    It works as long as I am editing just the metabox with those fields. The issue is that I have several different metaboxes displayed that each have their own set of fields. Think contact info, demographic info, emergency contact, type (taxonomy), status (taxonomy) etc.

    When I update any of the other metaboxes, the title disappears and the slug is rewritten. If I update the post from the back-end, the title reappears and the slug is rewritten again.

    Here is the code I am using to update the title:

    
    function set_member_meta_title( $data , $postarr ) {
    		
      if($data['post_type'] === 'obh_member_meta' && $data['post_date_gmt'] !== $data['post_modified_gmt'] ) {
    
    			$first_name = $_POST['obh_member_meta_contact_first_name']; //get_post_meta($postarr['ID'],'obh_member_meta_contact_first_name',true); 
    			$last_name = $_POST['obh_member_meta_contact_last_name']; //get_post_meta($postarr['ID'], 'obh_member_meta_contact_last_name' , true); 
    			$title = $first_name . ' ' . $last_name;
    			$post_slug = sanitize_title_with_dashes ($title,'','save');
    			$post_slugsan = sanitize_title($post_slug);
    
    			$data['post_title'] = $title;
    			$data['post_name'] = $post_slugsan;
      }
      return $data;
    }
    add_filter( 'wp_insert_post_data' , 'set_member_meta_title' , '10', 2 );
    

    I think I need to do some sort of check to determine if I am updating either of the first or last name fields before making the update and execute only if I am editing those fields, but I am at a loss on how to do that.

    Could you provide any inspiration on how I might accomplish that?

    Thanks so much!

    #7753
    Anh TranAnh Tran
    Keymaster

    Hi,

    I think hooking to wp_insert_post_data is too generic as it fires every time a post is added/updated.

    The extension already have 2 filters that you can use to change the post data when saving: rwmb_frontend_update_post_data for updating posts and rwmb_frontend_insert_post_data for creating new posts. Previously it has only 1 params: the submitted post data. But I've just updated the extension, adding 2nd parameter: the shortcode params, so you can use to check whether you are on the right meta box.

    This is sample code:

    add_filter( 'rwmb_frontend_insert_post_data', 'your_function', 10, 2 );
    add_filter( 'rwmb_frontend_update_post_data', 'your_function', 10, 2 );
    function your_function( $data, $config ) {
        if ( $config['id'] !== 'your_meta_box_id' ) {
            return $data;
        }
        // Your code to get post_title
        $data['post_title'] = ...;
        return $data;
    }

    Please try it and let me know if you need anything. I can update the extension to make it works for you.

    #7756
    jcleavelandjcleaveland
    Participant

    This is great!

    Yes, insert_post_data was too generic. This is much better.

    I ended up with a simple check on the content of the fields prior to executing the code, but this is more elegant and more flexible in the long run.

    
    function set_member_title( $data ) {
    
    if($data['post_type'] === 'obh_member_meta' && $data['post_date_gmt'] !== data['post_modified_gmt'] ) {
    
    			$first_name = $_POST['obh_member_meta_contact_first_name']; 
    			$last_name = $_POST['obh_member_meta_contact_last_name'];
    		
    		if (isset($first_name) || isset($last_name) ) {
    
    			$title = $first_name . ' ' . $last_name;
    			$post_slug = sanitize_title_with_dashes ($title,'','save');
    			$post_slugsan = sanitize_title($post_slug);
    
    			$data['post_title'] = $title;
    			$data['post_name'] = $post_slugsan;
    
    		return $data;
    		}
    	}
    }
    add_filter( 'wp_insert_post_data' , 'set_member_title' , '99', 2 );
    

    Thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Help with front-end editing’ is closed to new replies.