2

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;
}
| improve this question | | | | |
1

The 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;
}
| improve this answer | | | | |
  • thank you , but it may I didn't got you , I replace this code with the one I got , but the first slug of the post which is the post title is not changing , so there is no new slug generation – Akel Mar 22 at 21:52
  • @Akel in that case, i have added a 2nd method. The first method will work if you are using wp_insert_post function to create post, second method will work when creating the post through backend. – Shazzad Mar 22 at 22:21
  • I really Aprciate your help but unfortunetly it's not working with me :( I just add the second method to function.php and set the permalinks option to /%postname%/ but nothing changed – Akel Mar 22 at 22:29
  • it's worked first when I finish writing the post title it generates a permalinks as well , but after publishing the post it's taking the title name as a permalink – Akel Mar 22 at 22:33
  • 1
    Although OP didn't mark this as accepted, this is a really good answer. Both approaches are great, especially using post_meta. – disinfor Mar 23 at 14:20
0

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

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.