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.

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

1 Answer 1

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.

Update

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

 
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.