Create HTML5 Vertical Endless Runner cross platform games

Using Phaser framework and other FREE software
Endless runner games are gaining more and more popularity and they are really easy to code.
I will take you by hand through the creation of a complete HTML5 vertical endless runner cross platform game with a lot of features and room for customization.
100 pages + 31 source code examples with a free update to come in a few days.

How to display (some) WordPress sidebar widgets in random order

Emanuele Feronato Php, WordPress

Do you need a plugin to display WordPress sidebar widgets in random order? You won’t find it. Or you will find messy scripts which need to be tweaked to return some kind of result.

That’s why I made my 22 lines script to display my sidebar widgets in random order. And not just in “random order”, because I needed to keep some widgets in fixed positions, and having the remaining ones rotating.

Motivation

Once I removed Adsense and network ads from the blog I received a lot of sponsorship requests.

There’s nothing wrong with the ones I denied, it’s just they do not match with the content of the blog.

Anyway, as I am doing some minor changes to the blog, I am starting to place the supporters in the sidebar.

Every block is a widget created from the Appearence menu this way:

And what I want is having the first and the last widgets (respectively the twitter “Follow me” button and the Facebook fan widget) to remain fixed in their first and last position, while the remaining widget should be displayed in random order.

Saying it quick, I needed a script to display in random order widgets from the i-th to the j-th

The theory

If you theme is designed according to WP guidelines, your sidebar.php file should contain something like this:

Where dynamic_sidebar function calls each of the active widget callbacks in order, and outputs the sidebar itself.

The generated html is something like this one:

At this time, since there aren’t hooks to handle widget output, we have to save the generated html, break it into individual widgets’ html, and mix it as required.

The code

The following code shouldn’t be copied and pasted, and assumes you haven’t lists inside your widgets. But it can give you a brief idea of what you’re supposed to do.

First, call your sidebar in sidebar.php this way:

I am calling random_widgets function rather than dynamic_sidebar.

The arguments are the sidebar name, the index of the first widget to be shuffled and the index of the last widget to be shuffled. Every widget outside such indexes won’t be shuffled.

random_widgets function is defined inside functions.php

What happens? I am saving the output inside a variable called $html. From such variable, I create an array with all widgets, break the array into sub arrays, shuffle the required array and rebuild the final code.

And that’s it, without installing any plugin. You can see the result refreshing the page, sidebar widgets will shuffle, with the only exception of the first and the last widgets, which remain in their place.

Stay up to date
Follow me on the social networks and never miss a post.

Official Emanuele Feronato Facebook page

Official Emanuele Feronato Twitter account

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 5

  1. Pingback: Mostrar aleatoriamente los widgets de una sidebar | SummArg

  2. Paul

    Very interesting method, especially using ob_start() etc.
    I’ve used this method in a shortcode handler for displaying dynamic sidebars.
    Thank you.

  3. Guillaume

    I’m using twenty eleven (child) theme and want to use this nice solution to rotate my sidebar widgets.
    I can’t find what to change in my sidebar.php. Where and how do I call this function ?

    [code]

    'monthly' ) ); ?>

    [/code]

Leave a Reply

Your email address will not be published. Required fields are marked *