Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Changing Username URL in threads

edited June 2015 in Vanilla 2.0 - 2.8

My forum is highly integrated into my website and I want the Username links to go to their website profile independant of Vanilla.

I've managed to do this for people replying to a thread but not for the thread starter. Can anyone help?

For the username of those replying, this worked successfully in forum/applications/vanilla/views/discussion/helper_functions.php:

Replaced:

echo UserPhoto($Author);
echo UserAnchor($Author);

With

echo UserPhoto($Author);
$AName =$Sender->EventArguments['Author']->Name;
echo '<a href="http://www.website.com/'. $AName .'">'. $AName .'</a>'; 

However, I tried doing the exact same change for the thread starter (located in discussion.php of same directory) but it just made the username not show.

Replaced:

if ($UserPhoto) {
echo UserPhoto($Author);
echo UserAnchor($Author, 'Username');
} else {
echo UserAnchor($Author, 'Username');
echo UserPhoto($Author);

echo FormatMeAction($Discussion);

With:

echo UserPhoto($Author);
$AName =$Sender->EventArguments['Author']->Name;
echo '<a href="http://www.website.com/'. $AName .'">'. $AName .'</a>';   

I'm no coder... please can someone help?

Comments

  • hgtonighthgtonight ∞ · New Moderator

    I am a little confused as to what you are trying to achieve. I can tell you that you shouldn't be editing core files.

    Assuming you want every profile link to go somewhere else, define a function in your favorite bootstrap file (e.g. /conf/bootstrap.before.php) that is called UserUrl. Here is the default definition:

       /**
        * Return the url for a user.
        * @param array|object $User The user to get the url for.
        * @param string $Px The prefix to apply before fieldnames. @since 2.1
        * @param string $Method Optional. ProfileController method to target.
        * @return string The url suitable to be passed into the Url() function.
        */
       function UserUrl($User, $Px = '', $Method = '', $Get = FALSE) {
          static $NameUnique = NULL;
          if ($NameUnique === NULL)
             $NameUnique = C('Garden.Registration.NameUnique');
    
          $UserName = GetValue($Px.'Name', $User);
          $UserName = preg_replace('/([\?&]+)/', '', $UserName);
    
          $Result = '/profile/'.
             ($Method ? trim($Method, '/').'/' : '').
             ($NameUnique ? '' : GetValue($Px.'UserID', $User, 0).'/').
             rawurlencode($UserName);
    
          if ($Get)
             $Result .= '?'.http_build_query($Get);
    
          return $Result;
       }
    

    Modify that to return whatever URL you want and this will automatically be used everywhere.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • edited June 2015

    Thanks I've just created a new file /conf/bootstrap.before.php and inside put your code with some dummy url to test:

    <?
         function UserUrl($User, $Px = '', $Method = '', $Get = FALSE) {
              static $NameUnique = NULL;
              if ($NameUnique === NULL)
                 $NameUnique = C('Garden.Registration.NameUnique');
              $UserName = GetValue($Px.'Name', $User);
              $UserName = preg_replace('/([\?&]+)/', '', $UserName);
              $Result = '/asdsadsa/'.
                 ($Method ? trim($Method, '/').'/' : '').
                 ($NameUnique ? '' : GetValue($Px.'UserID', $User, 0).'/').
                 rawurlencode($UserName);
              if ($Get)
                 $Result .= '?'.http_build_query($Get);
              return $Result;
           }
    ?>
    

    But the url didn't change

  • Ok got it sorted. Using what you said, I found someone else mention something similar and this works for me:

    <?php if (!defined('APPLICATION')) exit();
    function UserUrl($User, $Px = '', $Method = '', $Get = FALSE){
        $UserName = GetValue('Name', $User);
        $Result = 'http://yoursite.com/profile/'.$UserName;
        if ($Get)
            $Result .= '?'.http_build_query($Get);
        return $Result;
    }
    

    Thank you!

  • edited June 2015

    That function doesn't appear to replace the URL when someone quotes a username like so @Mark

    Can anyone help there?

  • hgtonighthgtonight ∞ · New Moderator

    You would have to install a mentions formatter that would handle the mentions formatting.

    This is because the mentions formatting doesn't have access to the user object, just the user reference. This is for scalability reasons, I assume.

    Add the following to your /conf/bootstrap.early.php file:

    <?php if (!defined('APPLICATION')) exit();
    Gdn::FactoryInstall('MentionsFormatter', 'CustomMentions', PATH_CONF . '/custommentions.php');
    

    Then you need to create a class called CustomMentions in the /conf/custommentions.php file. The class must define a FormatMentions method that accepts a single mixed type argument. E.g.:

    <?php if (!defined('APPLICATION')) exit();
    class CustomMentions {
    
        public function FormatMentions($Body) {
            return preg_replace(
                   '/(^|[\s,\.>])@(\w{1,50})\b/i', //{3,20}
                   '\1'.Anchor('@\2', 'http://example.com/customprofile/\\2'),
                   $Body
                );
        }
    }
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Thank you - that worked :)

  • edited June 2015

    The custommentions code is causing an issue where upon creating a new dicussion, it doesn't redirect the user back to the forum but instead just hangs on the same page with following error:

    Fatal error: Call to undefined method CustomMentions::GetMentions() in /home/mysite/site/public_html/forum/library/core/functions.general.php on line 1405

  • Anyone?

  • BleistivtBleistivt Moderator

    Do you still have the /conf/bootstrap.early.php file?

  • edited June 2015

    Yes, currently has:

    <?php if (!defined('APPLICATION')) exit();
    Gdn::FactoryInstall('MentionsFormatter', 'CustomMentions', PATH_CONF . '/custommentions.php');
    
  • BleistivtBleistivt Moderator

    Are you using 2.2a? Looks like you need to implement getMentions.

    Copy over this:
    https://github.com/vanilla/vanilla/blob/master/library/core/class.format.php#L1472-L1503

    to a new function called getMentions($Mixed) in the custommentions.php

  • I'm using 2.1.9

  • I tried doing the following in custommentions.php anyway but no luck:

    <?php if (!defined('APPLICATION')) exit();
    class CustomMentions {
        public function FormatMentions($Body) {
            return preg_replace(
                   '/(^|[\s,\.>])@(\w{1,50})\b/i', //{3,20}
                   '\1'.Anchor('@\2', 'http://www.cuddlecomfort.com/\\2'),
                   $Body
                );
        }
    
    
    public function getMentions($Mixed) {
    
      // Handle @mentions.
                if (C('Garden.Format.Mentions')) {
                    $urlFormat = str_replace('{name}', '$2', self::$MentionsUrlFormat);
                    // Unicode includes Numbers, Letters, Marks, & Connector punctuation.
                    $Pattern = (unicodeRegexSupport()) ? '[\pN\pL\pM\pPc]' : '\w';
                    $Mixed = Gdn_Format::replaceButProtectCodeBlocks(
                        '/(^|[\s,\.>\(])@('.$Pattern.'{1,64})\b/i', //{3,20}
                        '\1'.anchor('@$2', $urlFormat),
                        $Mixed
                    );
                }
                // Handle #hashtag searches
                if (C('Garden.Format.Hashtags')) {
                    $Mixed = Gdn_Format::replaceButProtectCodeBlocks(
                        '/(^|[\s,\.>])\#([\w\-]+)(?=[\s,\.!?<]|$)/i',
                        '\1'.anchor('#\2', '/search?Search=%23\2&Mode=like').'\3',
                        $Mixed
                    );
                }
                // Handle "/me does x" action statements
                if (C('Garden.Format.MeActions')) {
                    $Mixed = Gdn_Format::replaceButProtectCodeBlocks(
                        '/(^|[\n])(\/me)(\s[^(\n)]+)/i',
                        '\1'.wrap(wrap('\2', 'span', array('class' => 'MeActionName')).'\3', 'span', array('class' => 'AuthorAction')),
                        $Mixed
                    );
                }
                return $Mixed;
    
        }
    
    }
    
  • Yep that code worked... thank you @Bleistivt :)

  • This worked wonders on 2.1 but I don't think I can get it to work on 2.2. Are there any updates? Thanks :)

  • thanks all. Here's what I was looking for

Sign In or Register to comment.