0

Im working on an interface for a custom post type where I need the post title to carry the date and venue name. So on the Admin end I've got js that dynamically populate the title as the user fills out the form. If the user dosen't languish this information is then saved as a part of the permalink. Sometimes however the post is saved as a draft before the title is populated and we end up with an misleading URL.

My feeling is that it would be best I were to just hide the title all together on this post type, and look for a hook to populate the title AND rework the post url with the field values the user has set when saving the post. As of now I cant find any hooks or filters that look like they would be of help. example screen shot

Help?

0

You don't need to hide the title. To my knowledge there is atleast two way to do this, one is manual and other through codes.

Manual way:--

After you are done with adding the title, if your permalink doesn't match the title just click on permalink Edit button clear the text and click OK. It will get you the permalink as per the title.

Through Code:--

Put the below code in the theme's functions.php file.

add_filter( 'wp_insert_post_data', 'wpse_121035', 50, 2 );
function wpse_121035( $data, $postarr ) {
    //Check for the  post statuses you want to avoid
    if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {           
        $data['post_name'] = sanitize_title( $data['post_title'] );
    }
    return $data;
}

The above will automatically change the permalink of the post on the fly.

For more detail on how you can use it follow this blog

|improve this answer|||||
  • I've updated the title to better reflect what Im after. I think hiding the title field is nessicary to prevent the user from getting the impression they can efect changes here. Ultimatly I would like to pull custom field data but wp_insert_post_data is exactly what I need to acomplish this. One question - what happens if two posts have exactly the same $data['post_name'] ? – orionrush Nov 5 '13 at 20:45
  • The later one gets a suffix of -2 to the permalink, for example if two post has a title orionrush then the permalink of the first will be orionrush and the later will be orionrush-2, and the beauty is WordPress takes care of this, so you don't have to be bothered about this – Maruti Mohanty Nov 6 '13 at 6:34
  • Did you check, if this work for you? – Maruti Mohanty Nov 6 '13 at 17:39
  • 1
    I've not been able to test this yet, but am accepting this as is because the WP documentation supports what you've said. Thanks! – orionrush Nov 8 '13 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.