Friday, October 7, 2022
HomeWordPress Developmentphp - Lengthy Wordpress Random Distinctive Slug Generator

php – Lengthy WordPress Random Distinctive Slug Generator


I’m trying to create a protracted random url slug. That is the answer I’ve adopted, however my website accommodates 10,000 pages and I used to be questioning if it would take numerous time to carry out or if there’s a higher resolution for this process.

Additionally I’m questioning if I really want slugExists perform, for the reason that chance of the identical lengthy random url current on my website is nearly zero ?

 perform randomSlug() {
        $size = 25;
        $chars="abcdefghijklmnopqrstuvwxyz0123456789";
        $slug = '';
        for ($i = 0; $i < $size; $i++) {
            $string .= substr( $chars, wp_rand( 0, strlen($chars) - 1 ), 1 );
        }
        return $slug;
    }
    
    perform slugExists($slug) {
        international $wpdb;
        if($wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE post_name="$slug"")) {
            return true;
        } else {
            return false;
        }
    }
    
    perform uniqueRandomSlug() {
        do {
            $randomSlug = randomSlug();
            $slugExists = slugExists($randomSlug);
        } whereas ( $slugExists );
    
        return $randomSlug;
    }

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments