WordPress: How to randomly generate different scripts


If you want to generate multiple scripts randomly, but located in the same location, for example to switch different boards of pubs at the same place… (works with any other script)

Here is the procedure…

In the case of 2 scripts to chance equal:

You must create 2 php files:

  • 1 php file named ads_1.php. Contains the 1st script
  • 2nd php file named ads_2.php. Therefore contains the 2nd script

You send on your server (in the folder of your theme), and at the location where you want there is a random loading, you insert:

<? php $ads = rand(1, 2);

if($ads == '1') {include (TEMPLATEPATH. ('/ ads_1.php'); }
if($ads == '2') {include (TEMPLATEPATH. ('/ ads_2.php'); }


At the defined location, visitors will have as much chance to see the 1 script 2 script! Nothing simpler!

If you want to change the name of files, you also need to change the code: replace just ads with the name of the file without the figures.

If you wish to increase the chance of one script over the other:

<? php $ads = rand(1, 3);

if($ads == '1') {include (TEMPLATEPATH. ('/ ads_1.php'); }
if($ads == '2') {include (TEMPLATEPATH. ('/ ads_2.php'); }
if($ads == '3') {include (TEMPLATEPATH. ('/ ads_1.php'); }


In the case of over 2 scripts (X scripts):

<? php $ads = rand(1, X);

if($ads == '1′) {include (TEMPLATEPATH. ('/ ads_1.php'); }
if($ads == '2′) {include (TEMPLATEPATH. ('/ ads_2.php'); }
if($ads == 'X′) {include (TEMPLATEPATH. ('/ ads_X.php'); }


Replace X with the total number of scripts, and between the if($ads == '2′) and if($ads == 'X′), you must complete depending on the number of scripts. (In this case, you must have as many lines if as scripts)

Thanks to PSP for this piece of code ;-)

Leave a comment

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