Title: Possible to randomize shortcode parameter?
Last modified: August 30, 2016

---

# Possible to randomize shortcode parameter?

 *  Resolved [FriendlyHal](https://wordpress.org/support/users/friendlyhal/)
 * (@friendlyhal)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/)
 * This seems like it should be fairly simple but I just can’t seem to make it work
   and after an embarrassing number of hours searching for an answer and trying 
   different things I have finally decided to post my issue to the experts.
 * I have a shortcode which works as such:
 * `[shortcode id = '123']`
 * and it works just fine like that.
    But what I would like, is to have the “id”
   parameter be a random number between “x” and “y”. For example, where “x” might
   be 120, “y” might be 130, and “id” would end up as ‘123’, ‘128’, ‘122’ or etc.
 * Is this possible? And if it is, why is it not used more often? It seems like 
   it would be quite useful in order to customize pages for each user.
 * Thank you for helping out in advance.

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

 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353884)
 * Do you mean auto generating an ID between 120 and 130 in PHP?
    You could use `
   rand("120","130")` for that..
 * Or the user has to set that ID?
 * Explain more on this 🙂
 *  Thread Starter [FriendlyHal](https://wordpress.org/support/users/friendlyhal/)
 * (@friendlyhal)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353954)
 * Thank you for responding 🙂
 * As I’m editing the page I can write `[shortcode id = '123']` and it causes a 
   modal popup with the “id” of ‘123’ to appear on the page. If I code `[shortcode
   id = '128']` then instead of popup ‘123’ up comes ‘128’. What I would like to
   happen is that a random popup appears instead of just the same ‘123’ or ‘128’
   popup over and over.
 * Putting `[shortcode id = 'rand("120","130")']` doesn’t fly.
 * Can this only be done by editing the plugins code?
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353981)
 * > Putting [shortcode id = ‘rand(“120″,”130”)’] doesn’t fly.
 * No, that code can be implemented in the shortcode function.
 * What is the shortcode about? and can we have a look at the code used for it? 
   e.g PHP function, plugin..
 *  Thread Starter [FriendlyHal](https://wordpress.org/support/users/friendlyhal/)
 * (@friendlyhal)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353984)
 * It doesn’t seem to work in my case anyway :/
 * The plug-in is easy popup announcement, the code for the shortcode is thus:
 *     ```
       <?php
   
       /*
        * Shortocode for single post / page
        */
       function epa_setcookie_post() {
       	$epa_expire = get_option( 'epa_expire' );
       	$expire = time() + (float) $epa_expire;
       	if(isset($_COOKIE['epa_popup'])):
       	else:
       		//$_COOKIE['epa_popup'] = 'active';
       		setcookie('epa_popup', 'active', $expire);
       	endif;
       } 
   
       function epa_shortcode_single( $atts ) {
       	$atts = shortcode_atts( array(
        	      'id' => ''
             ), $atts );
   
       	$default_popup = get_post($atts['id']);
   
       	if(get_option('epa_enable') == 'no') :
   
       		$epa_html .= '<div id="my_popup" class="well"><span class="my_popup_close"></span>';
       		$epa_html .= $default_popup->post_content;
       		$epa_html .= '</div>';
   
       		if(!isset($_COOKIE['epa_popup'])) {
       			return $epa_html;
       		}
   
       	endif;
   
       }
       add_shortcode( 'epapop', 'epa_shortcode_single' );
       ```
   
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353985)
 * Can you check this:
 *     ```
       <?php
   
       /*
        * Shortocode for single post / page
        */
       function epa_setcookie_post() {
       	$epa_expire = get_option( 'epa_expire' );
       	$expire = time() + (float) $epa_expire;
       	if(isset($_COOKIE['epa_popup'])):
       	else:
       		//$_COOKIE['epa_popup'] = 'active';
       		setcookie('epa_popup', 'active', $expire);
       	endif;
       } 
   
       function epa_shortcode_single( $atts ) {
       	$atts = shortcode_atts( array(
        	      'id' => ''
             ), $atts );
   
       	//$default_popup = get_post($atts['id']);
       	$default_popup = get_post(rand("120","130"));
   
       	if(get_option('epa_enable') == 'no') :
   
       		$epa_html .= '<div id="my_popup" class="well"><span class="my_popup_close"></span>';
       		$epa_html .= $default_popup->post_content;
       		$epa_html .= '</div>';
   
       		if(!isset($_COOKIE['epa_popup'])) {
       			return $epa_html;
       		}
   
       	endif;
   
       }
       add_shortcode( 'epapop', 'epa_shortcode_single' );
       ```
   
 * And don’t set any ID attribute for the shortcode..
 * ?
 *  Thread Starter [FriendlyHal](https://wordpress.org/support/users/friendlyhal/)
 * (@friendlyhal)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6353988)
 * Wooohooo!!!! You did it 😀 If Apple hadn’t made such a mockery of the word genius
   I would call you one, in lieu, you are a virtuoso!
 *  anonymized-13749270
 * (@anonymized-13749270)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6354005)
 * Hahahaha !
 * I am glad it worked for you. and thanks for the compliment!
 * Have a nice day 🙂

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

The topic ‘Possible to randomize shortcode parameter?’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: anonymized-13749270
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/possible-to-randomize-shortcode-parameter/#post-6354005)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
