且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WordPress中的随机永久链接键

更新时间:2023-12-03 08:14:10

function wp_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;
}

可以通过多种方式对其进行优化.

This can be optimised in a number of ways.

关于此wp_unique_post_slug()-yikes还要注意名称间隔. WordPress已使用此功能名称

Also regarding this wp_unique_post_slug() -- yikes watch out for the name spacing. Wordpress already uses this function name