Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: roach6390 on August 24, 2006, 07:04:17 PM

Title: Change username link colors
Post by: roach6390 on August 24, 2006, 07:04:17 PM
A while ago I found this code to change the color of links on a page. It uses JavaScript.

I'll try to explain better: Each member of your forum can have their personal color. Every time a link is posted with their name, (or how you set the code), the link will be their custom color.

Here is the code, with an example on the bottom:
Quote<script type="text/javascript">
function ColourName(username,colour,method) {
   var iLink = document.getElementsByTagName("a");
      for (a=0; a<iLink.length; a++) {
         switch (method) {
            case "num":
               if (iLink[a].href.match(new RegExp('showuser=' + username,'i'))) {
               iLink[a].style.color = colour;
               }break;
            case "name":
               if (iLink[a].href.match(new RegExp('index.php?')))  {
               if (iLink[a].innerHTML.toString().match(new RegExp(username,'i'))) {
               iLink[a].style.color = colour;
               }break;
                         }
      }
   }
}

ColourName("roach6390","#3399CC","name");
</script>

You can change what is in bold to furthur refine which links are highlighted in the color.

You can either do it by "name" or "num" (number).
If you do it by number, it uses the first bold.
If you do it by name, it uses the 2nd bold.

Here is a PHP script to get your started integrating this into your forum. It will take the submission and edit the file above. Edit the values on the top.
Quote<?

   $USER = $_POST['user'];
   $COLOR = '#'.$_POST['color'];
   $output = 'ColourName("'.$USER.'","'.$COLOR.'","name");';
   $fname = 'unc_change.txt';
   $found = false;
    
     // Read the file and count the lines
     $filetxt = file($fname);               // $filetext = a new array
     $line_count = count($filetxt);         // Line count in $filetext
    
     // Check each line to see if the user's name is in it.
     for ($i = 1; $i <= $line_count; $i += 1) {
       if (substr_count($filetxt[$i],'ColourName("'.$USER) == 1) {
         if ($found == false) {
           $newfile = str_replace($filetxt[$i],$output.'
         ',implode('',$filetxt));
           $found = true;
         }
       }
     }
    
     // The user's name is NOT in any of the files, so add a new one, and a blank "buffer" zone.
     if ($found == false) {
       $filetxt[($line_count-2)] = $output.'
      

';
     } 
    
   
   // Write the new file
   if ($newfile == '') {
     $newfile = implode('',$filetxt);
   }
   $handle = fopen($fname,"w");
   fwrite($handle,$newfile);
   fclose($handle);
   
?>
Settings changed!<BR>
Your color is now: <? echo $COLOR ?><BR>
Preview: <FONT COLOR="<? echo $COLOR ?>"><? echo $USER ?>

Any bugs, please tell me!

Please move to Tips and Tricks!

Enjoy,
roach
Title: Re: Change username link colors
Post by: junglecat on November 25, 2006, 01:44:54 PM
I would like to do this, but where do I put the php script that writes the code and the first file?

I guess I need idiot instructions. :-[
Title: Re: Change username link colors
Post by: MinasC on March 07, 2007, 07:03:48 AM
yeah , where ?
Title: Re: Change username link colors
Post by: codenaught on June 30, 2007, 10:51:15 PM
junglecat, MinasC, do either of you still need help with this? First place you should probably try is somewhere between the <head> </head> tags in Themes/<theme_name>/index.template.php.
Title: Re: Change username link colors
Post by: MinasC on July 01, 2007, 04:18:13 AM
you mean i should put both the script and the phpcode anywhere in the head section of the index.template.php ?

thnx a lot .
Title: Re: Change username link colors
Post by: MinasC on December 01, 2007, 06:44:15 AM
Quote from: MinasC on July 01, 2007, 04:18:13 AM
you mean i should put both the script and the phpcode anywhere in the head section of the index.template.php ?

thnx a lot .

well ?
Title: Re: Change username link colors
Post by: IchBin™ on December 15, 2007, 12:11:50 AM
Quote from: MinasC on December 01, 2007, 06:44:15 AM
Quote from: MinasC on July 01, 2007, 04:18:13 AM
you mean i should put both the script and the phpcode anywhere in the head section of the index.template.php ?

thnx a lot .

well ?
That is what he said. Did you try it?
Title: Re: Change username link colors
Post by: MinasC on December 16, 2007, 03:27:26 AM
yes but didn't change anything ...
Title: Re: Change username link colors
Post by: Sarge on December 23, 2007, 02:55:29 AM
Quote from: MinasC on December 16, 2007, 03:27:26 AM
yes but didn't change anything ...

If you use a custom theme in your forum, you need to make the change there.

Have you tried akabugeyes' suggestion and placed the code in index.template.php of the theme you're using?
Title: Re: Change username link colors
Post by: MinasC on December 23, 2007, 03:44:29 AM
i am using the default theme , and yes i tried it .
Title: Re: Change username link colors
Post by: Oldiesmann on January 19, 2008, 09:20:52 PM
The javascript won't work at all because it's looking for things in the url that SMF doesn't use. I'm not sure what the php script is supposed to do. The best way to do this is to add a custom profile field to handle the color, then modify the link that's generated in the loadusercontext function in Load.php to change the link color if a color is set. Unfortunately I can't post examples right now because I'm on my cell phone, but that should get you started.
Title: Re: Change username link colors
Post by: MinasC on January 22, 2008, 02:38:11 PM
i'd be a liar if i say that i can do any of that by myself ! so i guess i'll have to wait till you get to a pc !  :P

thnx a lot !
Title: Re: Change username link colors
Post by: H on February 02, 2008, 05:51:42 PM
Oldiesmann / MinasC, any update on this? :)
Title: Re: Change username link colors
Post by: MinasC on February 10, 2008, 06:08:26 PM
Quote from: MinasC on January 22, 2008, 02:38:11 PM
i'd be a liar if i say that i can do any of that by myself ! so i guess i'll have to wait till you get to a pc !  :P

thnx a lot !

still waiting myself !
Title: Re: Change username link colors
Post by: Rumbaar on February 17, 2008, 05:22:54 AM
This mods might offer additional/alternate options:
ColorizePost (http://custom.simplemachines.org/mods/index.php?mod=1061)
Title: Re: Change username link colors
Post by: MinasC on February 20, 2008, 01:54:19 AM
actually it does !
Title: Re: Change username link colors
Post by: Rumbaar on February 20, 2008, 04:15:54 PM
So what the mod a solution to your current issue?
Title: Re: Change username link colors
Post by: MinasC on February 22, 2008, 04:54:30 AM
i suppose it gave me some similar solution , so , yes , please consider this matter solved !
thnx !
Title: Re: Change username link colors
Post by: eliteghost on August 31, 2008, 12:37:28 PM
Does this old thing still work? ive been looking for this, now that ive found it i  cant get it to work,  maybe i need better dummy instructions..

After all this argument up top here about where to put it, you would think they could just specify.
I pasted it all into my index.template.php after <head> and it wouldnt load my page, gave me a parse error.

now, i know with coding my own page that it should go in the <head> of the page. of index template.   I would place it somwhere in the profile.php  so that in the users profile they can change thier color and appearance on the entire forum.

Anyone have this working?  Help me out?
Title: Re: Change username link colors
Post by: Rumbaar on August 31, 2008, 06:05:51 PM
I would recommend the "Member Color Link" mod, it will change the color of each persons link based on membergroup in many places.
Title: Re: Change username link colors
Post by: eliteghost on August 31, 2008, 07:18:03 PM
aww lame, thats not what i wanted lol Dam!

this only colors the members by group, its not user selectable. How can i mod this mod so that users select a color from thier profile, and it applies it thoughout the entire forum?
Title: Re: Change username link colors
Post by: H on September 01, 2008, 10:16:43 AM
Do you know php? If not I suggest you post in Mod Requests (http://www.simplemachines.org/community/index.php?board=79.0).
Title: Re: Change username link colors
Post by: esttecb on November 16, 2009, 07:17:30 PM
Ok, this is pretty old... but this topic is a friend of google...
So, here we go...

Open Source/Load.php
find:
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $profile['ID_MEMBER'] . '" title="' . $txt[92] . ' ' . $profile['realName'] . '">' . $profile['realName'] . '</a>',

replace with:
'link' => '<a style="color: ' . $profile['member_group_color'] . ';" href="' . $scripturl . '?action=profile;u=' . $profile['ID_MEMBER'] . '" title="' . $txt[92] . ' ' . $profile['realName'] . '">' . $profile['realName'] . '</a>',


all done. I know it's by member group, but it doesn't need any mod.
Title: Re: Change username link colors
Post by: Arantor on November 16, 2009, 08:45:30 PM
That assumes everyone has a member_group_color; Regular Member does not, neither do the post count groups by default.