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:
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;
}
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/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;
}
Share
Improve this question
asked Mar 22, 2020 at 18:57
AkelAkel
211 bronze badge
2 Answers
Reset to default 1The second argument of wp_unique_post_slug
filter is post id. You can utilize that to generate the slug only once while creating the post for the first time.
Method 1:
add_filter( 'wp_unique_post_slug', 'unique_slug_108286', 10, 2 );
function unique_slug_108286( $slug, $postId ) {
if ( ! $postId ) {
$n = 4;
$slug = bin2hex( random_bytes( $n ) ); //just an example
}
return $slug;
}
Other method would be using post meta to indicate whether a slug has been generated.
Method 2:
add_filter( 'wp_unique_post_slug', 'unique_slug_108286', 10, 2 );
function unique_slug_108286( $slug, $postId ) {
if ( $postId && ! get_post_meta( $postId, 'slug_generated', true ) ) {
$n = 4;
$slug = bin2hex( random_bytes( $n ) ); //just an example
update_post_meta( $postId, 'slug_generated', true );
}
return $slug;
}
OK finally I found the solution
function append_slug($data) {
global $post_ID;
if (empty($data['post_name'])) {
$n=4;
$data['post_name'] = bin2hex(random_bytes($n));
}
return $data;
}
thank you all for help