• Hi,

    i want to make a shortcode which displays a random string. i found some javascripts which they do that… (or php is better?) but i dont know how to convert them to shortcodes…

    i’m sure lot of pro people know it easily… thanks!
    here is my code: (u have better idea? do it!)

    <script language="javascript" type="text/javascript">
    function makeid()
    {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
        for( var i=0; i < 5; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));
    
        return text;
    }
    </script>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Here is a simple example:

    function random_string_func( $atts ) {
       extract( shortcode_atts( array(
             'number' => 5,
          ), $atts ) );
       $possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
       $possible_array = str_split( $possible );
       shuffle( $possible_array );
       return implode( '', array_slice( $possible_array, 0, $number ));
    }
    add_shortcode( 'random_string', 'random_string_func' );

    You might want to eliminate zero, one, l (ell), and o (oh) because they are so similar that they cause confusion. You could also add in special characters and code to ensure that at least one digit and one special character are used.

    You can check out the Shortcode API codex. https://codex.wordpress.org/Shortcode_API

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to make a simple Random String shortcode?’ is closed to new replies.