samedi 29 novembre 2014

How to generate random numeric string for a custom post type?


I have this PHP code to generate a random numeric string:



function generateRandomString($length = 12)
{
global $generatedStrings;
$characters = '0123456789';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
if (isset($generatedStrings[$randomString])) {
$randomString = generateRandomString($length);
}
$generatedStrings[$randomString] = $randomString;
return $randomString;
}

$generatedStrings = array();

foreach(range(1, 100000) as $num) {
echo "\n".$num."\t : ".generateRandomString();
}


I was wondering if anyone could help figure out how to integrate this code with WordPress to auto-generate strings as slugs for a custom post type.


Any guidance you can provide is appreciated!





Aucun commentaire:

Enregistrer un commentaire