The MU forums have moved to WordPress.org

A link to a random blog? (48 posts)

  1. drmike
    Member
    Posted 16 years ago #

    Greets:

    Just wondering if anyone has come up with a method of posting a link to a random blog on your install. I have it on my to do list and have some ideas but don't want to dupe someone else's effort or reinvent the wheel if it's already been done.

    thanks,
    -drmike

  2. andrewbillits
    Member
    Posted 16 years ago #

    I don't have it coded but if I were to do it, this would be my method:

    1) get the ID of the first and latest blog created. Let's say 1 and 2500

    2) Generate a random number (tons of ways to do this) between 1 and 2500

    3) Check to see if a blog with that ID number exists just in case it was deleted at some point.

    Ofcourse it won't hit an active blog every single time but it should not take more than 2-3 loops to grab one that exists.

    Now, the another method is grabing all the blog IDs and putting them in an array. Then generate a random number based on the array. This method would toss out an existing blog everytime but that would be one *BIG* array if you had several thousand blogs.

    The last method would be similar to the one above except I would do this:

    1) grab the first created blog ID and the latest.

    2) subtract 100 from the latest blog ID number.

    3) Generate a random number between 1st blog ID# and last blog ID# - 100 and copy the number to another variable and add 100

    4) Get all blog IDs between $ran1 and $ran2(ie ran1 +100) and put those blog IDs into an array.

    5) Grab random blog ID from array

    This last method is simailar to the one above it except it will drastically cut down on the array size.

    You could probably whip up a plugin for this in ten minutes. :)

  3. drmike
    Member
    Posted 16 years ago #

    You would also have to check for the mature and public flags as well I would think. I know WP.com checks for the mature one. Probably do it before dumping it into the array. I would also check to see if it had at least more than a single post. Hate to send someone to an empty blog.

    Anyone know of a MySQL function to call a random record from with a database? I would hate to have to use the array method like Andrew suggests. (That though was my first thought on how to do it.)

  4. andrewbillits
    Member
    Posted 16 years ago #

    Here's an example of how to pull a random row from a table

    SELECT * FROM blogs WHERE public = '1' ORDER BY RAND() LIMIT 1

    Might need cleaning up but it works. I use "ORDER BY RAND()" all the time. I don't know why I didn't mention it earlier. Sorry about that.

  5. mickemus
    Member
    Posted 16 years ago #

    dr mike! how about a plugin ;)

  6. drmike
    Member
    Posted 16 years ago #

    Actually I've never done a WordPress plugin. Wouldn't know the first place to start.

    I was thinking of adding it to the admin bar, though then it would only be for users who are signed in.

  7. mickemus
    Member
    Posted 16 years ago #

    hahaha... i tried putting the list-all-posts plugin into a widget by copying and pasting from another (seamlessly simple widget) but it screwed up more than a few things when i loaded it!!

    Another way of doing a 'fake' random blog... use the list-all plugin (the one for blogs) and set it to 1. Then the latest updated blog will show up! At least the most active people will be promoted the most!!

    Anyone care to share a 'framework' widget whereby one could include the standard if exists() blablabla stuff... no need for a options panel but it would help getting more stuff into widgets. Personally I don't mind changing some settings inside the widget (like the number of posts etc)

  8. andrea_r
    Moderator
    Posted 16 years ago #

    Well, in the widgets dist, the Google search widget is greatly commented as a tutorial widget.

    Alsthough I'm curious; if most of us are using widgets as a way for *users* to customize sidebars, why would you want admin or main-blog specific items available to everyone?

    (although I have taken widget code and hard-coded it into my main blog theme...)

  9. suleiman
    Member
    Posted 16 years ago #

    andrew, is there any chance of getting something coded that would require a simple admin-bar hack? I'm also really interested in this and would be willing to front some cash.

  10. andrewbillits
    Member
    Posted 16 years ago #

    suleiman,

    It would be simple to code so I wouldn't charge for it. However i'm up to my neck in other projects at the moment so it might be a few days before I can sit down and code a new plugin to add a function for a random blog. So far it seems that monday is going to be a dead day at the office so I should be able to code it up then.

    I'm also horrible about forgetting to do things so you might want to drop me an email monday. :)

    andrew(no-spam)@idtstudios.com

  11. suleiman
    Member
    Posted 16 years ago #

    thanks a bunch andrew, you make the experience of being a wpmu admin half as hard as it would be.

  12. drmike
    Member
    Posted 16 years ago #

    Figured it out. Going to place it up at wpmudev.org shortly. :)

  13. drmike
    Member
    Posted 16 years ago #

    Now up.

    Random Blog

    I'm creating an array on the spot with your blogs in them. Would love to hear how long this takes to load on larger sites.

    Thanks,
    -drmike

  14. drmike
    Member
    Posted 16 years ago #

    17 downloads currently. Is it working for folks?

  15. suleiman
    Member
    Posted 16 years ago #

    I'm having a tough time integrating it into the admin bar. I wish I knew more php but I've tried using echo and putting a link in that way and a simple html link but neither seem to work in the admin bar plugin.

  16. drmike
    Member
    Posted 16 years ago #

    Do what I do and put it with the logout links:

    # Login and logout link.
    echo "\n\t<li><a href="http://daria.be/next.php">Next Blog</a></li>";
    echo "\n\t<li>"; wp_loginout(); echo "</li>";
    echo "\n</ul></div>";

    Hope this helps,
    -drmike

  17. thatposhgirl
    Member
    Posted 16 years ago #

    This is giving me a strange error. A 406 "Not Acceptable" error. Is this something I'm doing wrong? http://screenwriters.la along the top.

  18. Ovidiu
    Member
    Posted 16 years ago #

    the download is somehow broken, does not work for me at least... I had a look at the code and I see there is nowhere a closing ?> can someone give me another link to download this plugin from, please?

  19. andrea_r
    Moderator
    Posted 16 years ago #

    that's where it's stored. Did you follow the instructions? It does not get dumped in the plugin folder. Did you ad the closing ?> ?

  20. Ovidiu
    Member
    Posted 16 years ago #

    well I followed the instructions, dumped it into the wpmu root folder, but I did not make the link to the random blog in the admin menue but on my menue (frontpage)...

    still, can I have a working copy, just to make sure that is not the problem?

  21. Ovidiu
    Member
    Posted 16 years ago #

    well, nobody answered so far... I downloaded the .zip file from wpmudev.org unziped and this is the content of the file if opened with nano on linux:

    ÿþ<^@?^@p^@h^@p^@^M^@
    ^@^M^@

    can anyone please send em a working copy to ovizii at zice . ro ?

  22. Ovidiu
    Member
    Posted 16 years ago #

    well, if I unzip it on windows I get this:


    <?php

    / *

    P l u g i n N a m e : R a n d o m B l o g

    P l u g i n U R I : h t t p : / / t d j c . b e / 2 0 0 6 / 0 9 / 0 6 / r a n d o m - b l o g - p l u g i n - f o r - w p m u /

    D e s c r i p t i o n : C r e a t e s a s t a t i c l i n k t o a r a n d o m b l o g

    V e r s i o n : 1 . 0

    A u t h o r : D r . M i k e W e n d e l l

    A u t h o r U R I : h t t p : / / t d j c . b e

    I n s t a l l i n s t r u c t i o n s

    1 ) P l a c e t h i s f i l e ( i e n e x t . p h p ) i n t o y o u r W P M U r o o t

    2 ) A d d a l i n k t o h t t p : / / m y d o m a i n . t l d / n e x t . p h p t o w h e r e e v e r y o u w a n t i t .

    strage still... I'll now start manually removing the spaces that are all over the place...

  23. andrea_r
    Moderator
    Posted 16 years ago #

    I went and checked myself and something must be up with your ftp program. File is fine for me.

    Ah,m heck, here's a code dump for ya.

    <?php
    /*
    Plugin Name: Random Blog
    Plugin URI: http://tdjc.be/2006/09/06/random-blog-plugin-for-wpmu/
    Description: Creates a static link to a random blog
    Version: 1.0
    Author: Dr. Mike Wendell
    Author URI: http://tdjc.be
    Install instructions
    1) Place this file (ie next.php) into your WPMU root
    2) Add a link to http://mydomain.tld/next.php to where ever you want it.
    3) Click on the link.
    You should go to a random public blog on your site. :)
    I would recommend putting this link within your Admin Bar if you use that plugin.
    Assumptions:
    Your main blog has the blog_id of '1'. That should be a given with WPMU but if it's not, modify the $sql statement.
    Also I'm assuming that the name of your _blogs table is wp_blogs. If not, then you need to change that as well.
    */
    include('wp-config.php');
    $values = array();
    $sql="SELECT * FROM wp_blogs WHERE (public = '1' AND mature = '0' AND spam = '0' AND deleted = '0' AND blog_id != '1')";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) {

    $randomdomain = $row['domain'];
    $randompath = $row['path'];
    $randomurl = 'http://' . $randomdomain . $randompath;
    array_push($values, "$randomurl");
    }
    shuffle($values);
    $rand_keys = array_rand($values, 2);
    $randomblog = $values[$rand_keys[0]];
    ?>
    <html><head>
    <META HTTP-EQUIV=Refresh CONTENT="0; URL=<?php echo $randomblog; ?>">
    </head><body>
    </body></html>

  24. Ovidiu
    Member
    Posted 16 years ago #

    thx :-)

    I dunno why everybody tried giving me advices on how to download that file, I did it in 2 different ways:

    1. click on it on wpmu-dev.org, it opened in winrar thats where I got the second version from with all those blanks.
    2. wget http://wpmudevorg.wordpress.com/download/126428064_randomblog.zip on my server - the usual way I download stuff directly to the server.... this try gave me that scrambled code after I did a unzip 126428064_randomblog.zip

    anyway, its working now :-) thank YOU

  25. DoggyBloggy
    Member
    Posted 16 years ago #

    The file on the dev site is corrupted.

  26. skcsknathan001
    Member
    Posted 15 years ago #

    how come the randomblog link tries to go to

    http://1arivippu.adadaa.com/%3C?php%20echo%20$randomblog;%20?%3E

    for me?

    Any fix?

  27. flys
    Member
    Posted 15 years ago #

    to andrea_r:
    thank you for the code, after several upload, now it works
    but
    i get an header error before the next blog is loaded:

    Warning: Cannot modify header information - headers already sent by (output started at /home/blog/public_html/next.php:1)

    How can i fix this ?

    Thanks in advance ;)

  28. qza
    Member
    Posted 15 years ago #

    I would like to know this too.
    For the moment, you can disable warnings in php.

  29. flys
    Member
    Posted 15 years ago #

    qza:
    i can i do to disable it ?
    Thanks

  30. demonicume
    Member
    Posted 15 years ago #

    how come the randomblog link tries to go to

    http://1arivippu.adadaa.com/%3C?php%20echo%20$randomblog;%20?%3E

    for me?

    Any fix?

    any ideas yet on this?

About this Topic