NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Créer un shortcode – Obtenir un ID aléatoire

---- Last modified : 7 July 2021

With this tutorial, we will help you create a new shortcode that will allow you to generate a random ID; like in a Formidable Form hidden field for example.

In our example below, we want to hide a field that can generate a random ID in a form.

First of all, we need to use the mt_rand() PHP function which will return a custom ID. We add an argument that will fetch a number between 1000 and 9999.

Then we give it a custom entity like in our WPU example –

Our custom shortcode - random ID

// Add Shortcode
function wpu_number_entreprise_shortcode() {
   $numberplugin = mt_rand(1000, 9999);
   return 'wpu-' . $numberplugin;
add_shortcode( 'idrand', 'wpu_number_entreprise_shortcode' );

So we could obtain something like: wpu-3874

You’re also free to change any arguments within the mt_rand function.

Shortcode : [idrand]