Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have implemented a post_type_link filter for my custom post and this is working great. However, the only downside is that the final URL only seems to be created when the post is published.

What I'd like is to create the custom URL after a user has typed in a title for the post. I'm having trouble finding the correct hook (filter/action) to accomplish this.

Does anyone know what the best way is to hook into the URL generation before a post is published?

share|improve this question
add comment

2 Answers

I think the post needs to be inserted first. This is a sample function that updates the post slug after it's been inserted.

// Update post slug
$my_post['ID'] = $pid;
$my_post_name = 'post_name' . "-" . $pid;
$my_post['post_name'] .= $my_post_name;

// Update the post into the database
wp_update_post( $my_post )

It's combining the post_name with post ID to create the URL. You also may want to look at http://codex.wordpress.org/Function_Reference/register_post_type which goes over the options for setting slugs. I'm not sure if you want to allow your members to be able to set their own permalink or if you want that disabled and to have it generated in the background.

share|improve this answer
add comment

Barely tested, so be careful, but maybe this will get you started.

function lock_permalink_early($data,$postarr) {
  global $wpdb;
  if ($data['post_type'] == 'post' && !empty($postarr['ID']) && empty($data['post_name'])) {
    $data['post_name'] = $data['post_title'];
  }
  return $data;
}

add_filter('wp_insert_post_data','lock_permalink_early',99,2);

WordPress does not set the permalink until a post is published. That lets you work with draft titles without too much fiddling if you change your mind. If you intentionally set the post_name which is the post specific part of the permalink, on the first save though, it appears to stick. Obviously, you will need to change that post_type to match your custom post type.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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