+ Reply to Thread
Results 1 to 6 of 6

Thread: Random link generator for wordpress plugin authors

  1. #1
    Dormant Account
    Join Date
    Jan 2011
    Location
    @ Home - except when I'm out (UK)
    Posts
    123
    Thanks
    97
    Thanked 42 Times in 29 Posts
    Rep Power
    4

    Random link generator for wordpress plugin authors

    Hi, I decided to post this here because it was kind of taking my other thread off topic. For the background on what lead me to this - see here

    Did you read it..? - if not please do, it may make the rest of this more understandable

    Right... with that out of the way, I just like to thank grynge for the original idea and input so far.

    Here is the code I've come up with. It will generate a random link with random anchor text. Currently, it will pick from 3 URLs, giving each URL one of 3 different anchor texts. It could easily be add to too give more URLs and anchor texts.

    There are two ways this can be used:

    First method:

    The idea is to call the main function during plugin install process. This method will add a 'static' link to the plugins options. Then when the plugin is parsed to the front end (visitors view), if the link is set to display, the same static link will show every time.

    Code:
    <?php 
    ### Function to call on plugin install #########################
    function make_random_link ()
    {
     # get base url
     $sitebase = get_option('siteurl') . '/';
     
     # build array of landing pages
     // edit these to your page links
     $url = array ('page-1', 'page-2', 'page-3');
     
     # build arrays of anchor-texts
     // edit these to your required anchor texts
     $Ankr1 = array ('anchor-text1', 'anchor-text2', 'anchor-text3');
     $Ankr2 = array ('anchor-text4', 'anchor-text5', 'anchor-text6');
     $Ankr3 = array ('anchor-text7', 'anchor-text8', 'anchor-text9');
     
     # generate random number for url
     $u = mt_rand(0, 2);
     # generate random number for anchor-text
     $a = mt_rand(0, 2);
     
     # build random url
     $random_url = $sitebase . $url[$u];
     
     # find which url we have and get appropriate anchor text
     if ($u == 0){
     $anchor_text = $Ankr1[$a];
     } else if ($u == 1){
     $anchor_text = $Ankr2[$a];
     } else {
     $anchor_text = $Ankr3[$a];
     }
     
     # build and return complete link
     $random_link = '<a href="'. $random_url .'" title="'. $anchor_text .' - '.$sitebase.'">'. $anchor_text .'</a>';
     return $random_link;
    }
     
    ### Add to plugin install function ###############
    $get_author_link = make_random_link ();
    add_option('author_link', $get_author_link);
     
    ### To display link ##############################
    # see if link will show
    if (get_option('show_author_link') == "YES") {
     
     #check we've got a link - if we have, load link in to var
     If (get_option('author_link') != '') {
      $Author_link = get_option('author_link');
     
      # display link with random anchor-text
      echo '<div style="width:'.$cft_mn_wth.'px; margin:0 auto; font-size: x-small; text-align: center;">'. $Author_link .'</div>';
     }
    }
    ?>
    Method 2:

    Almost the same except the main function is called while the page is being parsed instead of when the plugin is installed. Done this way, it will generate a random link on-the-fly (every time the page loads).

    Code:
    <?php 
    ### Function to call on plugin install #########################
    function make_random_link ()
    {
    # get base url
     $sitebase = get_option('siteurl') . '/';
     
     # build array of landing pages
     // edit these to your page links
     $url = array ('page-1', 'page-2', 'page-3');
     
     
     # build arrays of anchor-texts
     // edit these to your required anchor texts
     $Ankr1 = array ('anchor-text1', 'anchor-text2', 'anchor-text3');
     $Ankr2 = array ('anchor-text4', 'anchor-text5', 'anchor-text6');
     $Ankr3 = array ('anchor-text7', 'anchor-text8', 'anchor-text9');
     
     # generate random number for url
     $u = mt_rand(0, 2);
     # generate random number for anchor-text
     $a = mt_rand(0, 2);
     
     # build random url
     $random_url = $sitebase . $url[$u];
     
     # find which url we have and get appropriate anchor text
     if ($u == 0){
     $anchor_text = $Ankr1[$a];
     } else if ($u == 1){
     $anchor_text = $Ankr2[$a];
     } else {
     $anchor_text = $Ankr3[$a];
     }
     
     # build and return complete link
     $random_link = '<a href="'. $random_url .'" title="'. $anchor_text .' - '.$sitebase.'">'. $anchor_text .'</a>';
     return $random_link;
    }
     
     
    ### To display link ##############################
    # see if link will show
    if (get_option('show_author_link') == "YES") {
     
     #check we've got a link - if we have, load link in to var
      $Author_link =  make_random_link ();
     
      # display link with random anchor-text
      echo  '<p>'. $Author_link .'</p>';
     
    }
    ?>
    So there it is...

    I have tested this on wordpress 3.1.4 and not had any errors. Also with minor editing I don't see why it wouldn't work on any php based site. There are probably improvments that could be made to this (I'm quite new to php ).

    Thanks for reading my blabber, I would be interested to hear your thoughts.

  2. The Following 2 Users Say Thank You to MrP For This Useful Post:

    Clinton (July 5th, 2011), grynge (July 4th, 2011)

  3. #2
    Premium Member
    Join Date
    Aug 2010
    Location
    Adelaide
    Posts
    2,553
    Blog Entries
    6
    Thanks
    1,345
    Thanked 1,570 Times in 840 Posts
    Rep Power
    53
    Well done, I can't see any problems with it.

    Once the link was generated I would probably store it in the db so it becomes static across all users.

    I would love to test the random generator on a very successful plugin/theme. I do have to wonder what the results would be in the serps with a random link each time the google bot visited and then on say 10,000 sites that would be some test hmmm

  4. #3
    Dormant Account
    Join Date
    Jan 2011
    Location
    @ Home - except when I'm out (UK)
    Posts
    123
    Thanks
    97
    Thanked 42 Times in 29 Posts
    Rep Power
    4
    Quote Originally Posted by grynge View Post
    Well done, I can't see any problems with it.

    Once the link was generated I would probably store it in the db so it becomes static across all users.
    Yes, If you use the first method (creating link on plugin install), the link will be static for all users and stored in the 'options' table of the wordpress DB.

    I do have to wonder what the results would be in the serps with a random link each time the google bot visited
    That would be Method 2 (a random link on each page load).


    10,000 sites... wow... I'm obviously thinking too small, I was hoping to hit the 1000 mark.

  5. #4
    Premium Member
    Join Date
    Aug 2010
    Location
    Adelaide
    Posts
    2,553
    Blog Entries
    6
    Thanks
    1,345
    Thanked 1,570 Times in 840 Posts
    Rep Power
    53
    Quote Originally Posted by MrP View Post
    Yes, If you use the first method (creating link on plugin install), the link will be static for all users and stored in the 'options' table of the wordpress DB.
    Sorry about that it was late at night and I had a few drinks not the best time to read code lol

    Quote Originally Posted by MrP View Post
    That would be Method 2 (a random link on each page load).
    10,000 sites... wow... I'm obviously thinking too small, I was hoping to hit the 1000 mark.
    I can see a popular plugin in the making here for you.
    You could incorporate this plugin into just about every cms there is as well as for just about every theme builder.
    Make an admin section for it and let it loose into the wordpress wild.

  6. The Following User Says Thank You to grynge For This Useful Post:

    Clinton (July 7th, 2011)

  7. #5
    Dormant Account
    Join Date
    Jan 2011
    Location
    @ Home - except when I'm out (UK)
    Posts
    123
    Thanks
    97
    Thanked 42 Times in 29 Posts
    Rep Power
    4
    Quote Originally Posted by grynge View Post
    Make an admin section for it and let it loose into the wordpress wild.
    Hmmm, I give that some thought. I can see that it would be a good addition for theme builders as well as other plugin authors to incorporate into their code. I'm not sure how it would be used on its own though, even with an admin section.

    I'll apologies now just in-case I've misunderstood your meaning. It's 3.55am here

  8. #6
    Premium Member
    Join Date
    Aug 2010
    Location
    Adelaide
    Posts
    2,553
    Blog Entries
    6
    Thanks
    1,345
    Thanked 1,570 Times in 840 Posts
    Rep Power
    53
    You could offer 2 versions a free version which has your links already inserted in (they could remove if they know mysql)
    A paid version that they could use as they wish with no paid links.

    The other thing would be people would link to the page you had the plugin on which would if the plugin takes off get quite a few links as well.

  9. The Following User Says Thank You to grynge For This Useful Post:

    MrP (July 8th, 2011)

+ Reply to Thread

Similar Threads

  1. Wordpress membership plugin
    By ibscom in forum Website 101
    Replies: 5
    Last Post: March 13th, 2011, 4:55 AM
  2. FP - Wordpress Based Groupon Clone Plugin
    By chockfactor in forum Site Flipping
    Replies: 2
    Last Post: February 11th, 2011, 1:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts