Writing Extension to change username

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
jpitcher
Registered User
Posts: 5
Joined: Fri Jul 29, 2016 7:57 pm

Writing Extension to change username

Post by jpitcher » Sun Dec 01, 2019 11:49 pm

I am writing an extension to change a username. Board Version 3.2.7

I have added my listener for core.modify_username_string, and the function is calling and I am then modifying the $event['username_string'] inside the function.

Here is where the issue is: If I use the following code, the everything works perfect when the user's name is displayed, but for the link that wraps around the image it all goes out of whack.


$custom_url = '/profile/view/BBID?=' . $event['user_id'];
$event['username_string'] ='<a href="' . $custom_url .'">' . $event['username'] . '</a>';


I am guessing I am missing something, but if it works for one and not the other, what is going on? See attached images for HTML code that is output.
Screen Shot 2019-12-01 at 5.47.22 PM.png
Screen Shot 2019-12-01 at 5.47.22 PM.png (21.48 KiB) Viewed 221 times
Screen Shot 2019-12-01 at 5.47.42 PM.png
Thanks in advance. I am about ready to just go .htaccess way, but I wanted something that was more upgrade proof...

User avatar
pierredu
Registered User
Posts: 970
Joined: Thu Nov 01, 2012 8:04 am
Location: Paris (France)

Re: Writing Extension to change username

Post by pierredu » Mon Dec 02, 2019 8:00 am

Tidy your syntax.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1213
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Writing Extension to change username

Post by mrgoldy » Mon Dec 02, 2019 9:18 am

You’re not adhering to the different modes that function has.
You are putting HTML in the username while it is being called with the username or profile mode.

jpitcher
Registered User
Posts: 5
Joined: Fri Jul 29, 2016 7:57 pm

Re: Writing Extension to change username

Post by jpitcher » Mon Dec 02, 2019 1:16 pm

mrgoldy wrote:
Mon Dec 02, 2019 9:18 am
You’re not adhering to the different modes that function has.
You are putting HTML in the username while it is being called with the username or profile mode.
... I knew it was simple. I was ignoring the mode from the event. I saw the full URL coming in and assumed it was always the same.

Will keep testing, but that takes me further.

Thanks!!

jpitcher
Registered User
Posts: 5
Joined: Fri Jul 29, 2016 7:57 pm

Re: Writing Extension to change username

Post by jpitcher » Mon Dec 02, 2019 1:46 pm

Full working solution just for other people who need the help with this also.

Code: Select all


public function cjpfp_replace_username($event)
    {
        $new_profile_url = '/profile/view/BBID?=' . $event['user_id'];
        
        switch($event['mode'])
        {
            /**
             * Profile just wants the URL string, no HTML
             */
            case 'profile':
                $event['username_string'] = $new_profile_url;
            break;
            
            /**
             * Full wants the URL string and the HTML formatted.
             */
            case 'full':
                $event['username_string'] = "<a href=\"{$new_profile_url}\">{$event['username']}</a>";
            break;
            
            /**
             * Only woried about changing the URL, no need to change any of the following.
             */
            case 'no_profile':
            case 'username':
            case 'colour':
            default:
            
        }
    }


Post Reply

Return to “Extension Writers Discussion”