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
Intermediate
Web Hosting Canada

Last modified : Jul 07 2021

Estimated reading time : 1 minute, 3 seconds - 130 words

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]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>