Results 1 to 6 of 6

Thread: Random link generator for wordpress plugin authors

Threaded View

  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)

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