• Resolved Salt5

    (@salt5)


    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

    (@bcworkz)

    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

    (@salt5)

    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.