Title: Need help using rand() with shortcode button more efficiently
Last modified: August 20, 2016

---

# Need help using rand() with shortcode button more efficiently

 *  Resolved [Salt5](https://wordpress.org/support/users/salt5/)
 * (@salt5)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-help-using-rand-with-shortcode-button-more-efficiently/)
 * I’m working on creating a shortcode button that can generate a random link in
   a more user friendly way. Below is what I currently have and it does function
   with the user input this way:
 * `[btnrandom min="23" max="46" text="Go somewhere with a random value from 23 
   to 46"]`
 * and the code for the shortcode is as follows:
 *     ```
       function ac_buttonrandom( $atts ) {
       	extract( shortcode_atts( array(
       		'min' => '',
       		'max' => '',
       		'text' => 'Continue',
       	), $atts ) );
       	$randompage = rand($min, $max);
       	return "
       		<div class='tease-button tease-button-random'><a href='#$randompage'>$text</a></div>
       	";
       }
       add_shortcode( 'btnrandom', 'ac_buttonrandom' );
       ```
   
 * What I would like is for the shortcode to function more like this:
 * `[btnrandom random="23, 46" text="Go somewhere with a random value from 23 to
   46"]`
 * However a single value doesn’t seem to work with rand(); properly. I’m sure this
   is a simple fix and I’m a beginner when it comes to php.
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-help-using-rand-with-shortcode-button-more-efficiently/#post-3126149)
 * You now have a shortcode attribute called ‘random’ that contains a string value,
   in this case “23, 46”. So your callback function needs to parse the string to
   extract the 2 integers and pass them separately to the rand() function. You could
   use explode() or one of the regexp functions to do the parsing, or concoct your
   own method.
 *  Thread Starter [Salt5](https://wordpress.org/support/users/salt5/)
 * (@salt5)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/need-help-using-rand-with-shortcode-button-more-efficiently/#post-3126151)
 * explode(); fits the bill perfectly.
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Need help using rand() with shortcode button more efficiently’ is closed
to new replies.

## Tags

 * [rand](https://wordpress.org/support/topic-tag/rand/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [Salt5](https://wordpress.org/support/users/salt5/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/need-help-using-rand-with-shortcode-button-more-efficiently/#post-3126151)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
