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 tried to create a function that generate a random slug when a post is created. I am a bit worried that the function generates non unique strings as slugs. How can I solve this?

The function looks like:

add_filter('name_save_pre','unique_slug', 0);
function random_string() {
    $length = 6;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;

function unique_id($slug) {
    global $wpdb;
        return $wpdb -> get_row("SELECT ID FROM wp_post WHERE post_name = '" . $random_slug . "' && post_status = 'publish' && post_type = 'post'");    
    }
    function unique_slug($slug) {
        if($slug) return $slug;

        $random_slug = random_string();
        if(!unique_id($random_slug)){
            //what to do here?       
        }
        else {
            return $random_slug;
        }
    }
share|improve this question
add comment

2 Answers

Use the function wp_unique_post_slug(). Do not reinvent the wheel, this one is quite tricky.

Usage:

$unique_slug = wp_unique_post_slug( 
    $slug,
    $post_ID, 
    $post_status,
    $post_type, 
    $post_parent 
);

Then you can test if $slug === $unique_slug and generate a new one if the test fails.

You can find the function in wp-includes/post.php. It ends with a filter 'wp_unique_post_slug', so you can still adjust the return value if you don’t like it.

share|improve this answer
    
Here is the full documentation in the Wordpress Codex: codex.wordpress.org/Function_Reference/wp_unique_post_slug –  Andrew Odri Nov 26 '12 at 19:34
1  
@AndrewOdri Unfortunately, this is less than the plain code will tell you. I have added a link to queryposts.com’s docs. Hm, maybe I should write an extended documentation … –  toscho Nov 26 '12 at 19:39
    
@tocscho Heheh, I guess "full" was the wrong word, I should've gone with "official" :P The querypost.com documentation is great though :) –  Andrew Odri Nov 26 '12 at 19:45
    
I am trying to create a random permalink structure that automatically generates a page_name (or slug). E.g. www.domain.tld/xc34sd/ (where the slug is xc34sd) instead of having the post_title or post_id in the slug. The problem is that i don't want to have an existed slug with an added suffix. E.g. www.domain.tld/xc34sd-2/ Is this something I can use the wp_unique_post_slug for? –  Erikm Nov 26 '12 at 20:29
    
@Erikm See my update. Just test if the function has changed the slug. –  toscho Nov 26 '12 at 20:36
show 1 more comment
up vote 0 down vote accepted

I've found this plugin that should do the trick: http://wordpress.org/extend/plugins/wp-hashed-ids/

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.