WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When you create a post in WP, if you don't specify a title rapidly, WP generates a default slug on autosave, based on the post ID. Then, when you add the title later, unless you modify the slug by editing it, it remains the same (based on post ID)

What I'm trying to do is add an action on post publish, that generates and saves the post slug, based on the post title.

share|improve this question
up vote 5 down vote accepted

As long as haven’t touched the slug WordPress will generate a new one after you entered a title.


To change other peoples slugs use a filter (not tested!):

add_filter( 'wp_insert_post_data', 'prevent_numeric_slugs', 10, 1 );

function prevent_numeric_slugs( $post_data )
    if ( ! isset ( $post_data['post_title'] ) 
        or ! is_numeric( $post_data['post_name'] ) 
    {   // exit early
        return $post_data;

    // post_name is the slug
    $post_data['post_name'] = sanitize_title( $post_data['post_title'] );

    return $post_data;
share|improve this answer
Well, some of my users publish articles, and I don't know what they do but their articles' slug is sometimes still the post-id. I'm trying to force slug creation on publication to avoid this. – mike23 May 24 '11 at 11:31
@mike23 I wrote some sample code for a filter. Give it a try, and tell me if it works. – toscho May 24 '11 at 12:03

Your Answer


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.