Dice Roller WordPress Widget from KORPG™

Updated to Version 1.2

Note: Dice Roller has been updated to 1.2

After doing some searching of the WordPress plugin list, I came to the conclusion that a simple dice roller widget either doesn’t exist, or is hidden from the general public. So, just for fun, I decided to create a WordPress widget that others can add to their blog to roll dice.

My initial basic idea was to have an input box for N, a drop-down for X, a drop-down for +/-, and an input box for M which when rolled (via an input button) would display the result of NdX +/- M using something like mt_rand(N,N*X)+/-M

As with all projects, I decided first on the constraints of the system. Per that I decided on some general rules for the inputs:

  • N must be an integer.
  • X will be {2,3,4,6,8,10,12,20,30,%}
  • M must be an integer.

Understand that I have a very small amount of experience with php coding and no idea how widgets/plugins for WordPress are done, so I knew that this exercise was going to be a learning experience. So after a lot of research and downloading a WordPress Widget template that I modified heavily to do something close to what I initially wanted, I’ve gotten the plugin to a “ready for public consumption” state and thought I’d release it for fun.

However, the plugin, while not exactly what I initially envisioned, isn’t too far off and I’m pleased with the results of just an afternoon of tinkering.

For those running WordPress, download the files here: WordPress Dice Roller

Installation in 2 easy steps:

  1. Unzip the archive and place the folder in your plugins directory.
  2. Navigate to your plugins page and Activate the Dice Roller plugin.

Dice Roller Plugin

Setup in 4 expanded but very simple steps:

  1. Navigate to the Widget panel in WordPress
  2. Add the Dice widget to your sidebar
  3. Modify the settings:
    1. Change the title to anything you want
    2. Enter in the number of dice to roll
      1. If this value is blank or zero, the widget produces no result
      2. This value can actually be a negative number
    3. Use the drop-down to select the die you want to roll
    4. Enter in any modifier
      1. This value can be positive or negative
    5. Change the drop-down should you not want to display the link to korpg.com
  4. Save

Dice Roller Widget

Now each time a user views a page they will generate a new result that will be displayed.

Dice Roller Result

Compatibility:

Update: This plugin has been tested up to v. 3.4.2
The plugin doesn’t do anything special, so it should be backward compatible as far as I can ascertain. However, should you discover that’s incorrect, please comment and correct me.

Completed Updates:

  • Add a ReadMe for installation and use.
    • Edit: Done
  • Add it to the WordPress plugin Directory at some point.
  • Add the ability to remove the “display link to korpg.com” option in the future.
    • Edit: I went ahead and added this. While I would like people to leave the link in the widget, if it were me installing the plugin, I’d want the option to turn it off too.
  • Modify the link to korpg.com to rel=”nofollow”
    • Edit: This is done. Again, while it might be nice to have the links send some juice my direction, were it me doing the installation, I’d want to see the nofollow in that link.

Feel free to download and add this plugin to your blog, post comments, or whatnot.

Posted on
Last modified on:
This entry was posted in Products.
Tagged , .
Bookmark the permalink.

8 Responses to Dice Roller WordPress Widget from KORPG™

  1. Kevin says:

    This is just a test of a new shortcode using the input [roll N="1" X="6" M="1"] for rolling NdX+M

    If this doesn’t work, ignore it.
    1d6+1 = 3

  2. Kevin says:

    Well, this isn’t as difficult as it first appears.
    Couple of take-aways from this methodology:

    1. you needn’t cap the N=”1″, n=”1″ works
    2. you also don’t have to encapsulate the numerals in quotes. N=”1″ is equivalent to n=1

    That said, the easiest way to do this appears to be to include the following in your functions.php file:

    function roll_dice($atts, $content = null) {
      extract(shortcode_atts(array(
        "n" => '',
        "x" => '',
        'm' => ''
        ), $atts));
      $N = $atts["n"];
      $X = $atts["x"];
      $M = $atts["m"];
      $result = rand($N,$N*$X)+$M;
      $output .= $N.'d'.$X.'+'.$M.' = '.$result;
      return $output;
    }
    add_shortcode('roll', 'roll_dice');
    add_filter('comment_text', 'do_shortcode');
    

    That last line is a significant piece to the puzzle. Without it shortcodes don’t function in the comments portion of a blog. I’ll be looking for a means to pare down the codes that are allowed since, with this in place, all shortcodes you have active will be available to a commenter.

    But the most significant drawback to this method is that each time the page is reloaded, the random roll occurs again.

    So if someone wanted to use this as a means of giving visitors the ability to roll a die and leave the result present, this isn’t going to function well.

  3. lindevi says:

    Would these shortcodes work with Buddypress? Specifically, inserted into group forum topics and replies.

    Additionally, would it be possible to design one with Roll/Keep functionality such that all you’d have to do was type d+ = 0?

    I run my own L5R writing/role-playing community on word/buddypress and this looks nearly perfect for what I’m trying to do accomplish, though I have little to no experience with PHP myself for modifying it to my needs.

  4. Kevin says:

    Adding a roll/keep function shouldn’t be too difficult.
    And it should be possible to rework the shortcodes into a buddypress system, but the main issue is that there is no state-full holding of the results.
    For instance, each time the page is reloaded, the result of the roll will change.

    So, if your end desire is a dice roller for gaming online, I’m not sure this is the correct way to go.

  5. lindevi says:

    Ah, I see. That’s kinda a bummer but whatcha gonna do?

    Regardless, I really appreciate the swift response especially on a post so old. It seems like it’s finally time for me to knuckle down and learn PHP/MySQL if I want to make my ideas real. Thanks again. :)

  6. Kevin says:

    Yeah, it is a bummer indeed. One that I’ve noodled over from time to time but haven’t had the time or taken the effort (in light of other projects) to address.
    Sorry I wasn’t able to help further, but you might cosider asking these guys if they have any ideas: http://www.awesomedice.com/wordpress-dice-roller

  7. lindevi says:

    It’s not the widget I’m interested in so much as the bbcode/shortcode-style roller. Like the freakin awesome one they have on Myth-weavers.

  8. Kevin says:

    For persistent rolls, you’d need to store the rolls in the SQL db and re-write the actual post to reference a call to the result. That’s not actually difficult per se, but it would take some deeper knowledge of the product (phpBB/vbulletin) you’re using.

    Unfortunately I’m working on releasing another game this month and wouldn’t have time to commit to a project like this… well, that and my php coding skills aren’t what the kids these days call tight. ;)

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=""> <strike> <strong>