0

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?

2 Answers 2

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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