I want the permalinks to be like youtube generated from letters and numbers in 9 digits I modified this code
add_filter( 'wp_unique_post_slug', 'unique_slug_108286', 10, 4 );
function unique_slug_108286( $slug) {
$n=4;
$slug = bin2hex(random_bytes($n)); //just an example
return $slug;
}
so it’s worked and gave me a random slug , but the problem is that this slug is changed every time I entered the post at the backend , so I need it to be generated once and unique
I also found this solution it also worked but the same problem It’s changed every time I enter the post
add_filter( 'wp_unique_post_slug', 'unique_slug_so_11762070', 10, 6 );
function unique_slug_so_11762070( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
$new_slug = so_11762070_unique_post_slug('guid');
return $new_slug;
}
# From: https://stackoverflow.com/a/11762698
function so_11762070_unique_post_slug($col,$table="wp_posts"){
global $wpdb;
$alphabet = array_merge( range(0, 9), range('a','z') );
$already_exists = true;
do {
$guidchr = array();
for ($i=0; $i<32; $i++)
$guidchr[] = $alphabet[array_rand( $alphabet )];
$guid = sprintf( "%s", implode("", array_slice($guidchr, 0, 12, true)) );
// check that GUID is unique
$already_exists = (boolean) $wpdb->get_var("
SELECT COUNT($col) as the_amount FROM $table WHERE $col="$guid"
");
} while (true == $already_exists);
return $guid;
}