Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
coding-topic
How to filter Forum Display Name?
Avatar
bluantinoo
ITA
Member
Free Members
sp_UserOfflineSmall Offline
May 25, 2012 - 7:50 pm

@Swordfish, really take your time and no worry at all, I can leave cloned functions for the moment my forum will not have a huge traffic 😉

@Both: next time you come by, be sure to leave a pm, there are plenty of places out of tourist guides 😉 no joke! I owe you folks! 🙂

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 26, 2012 - 4:29 am

OK - I am now going to probably delight and annoy you at the same time! I am going to recommend a small change to SP core code that will help you and is a reasonable and useful change. That's the good bit. The annoying bit is that it will probably mean you can throw away much of the code we have done so far!

So - first - the SP core code change. I will ensure that this is applied to version 5.1 due out some time next week so you can make this change with that assurance.

Open the file /simple-press/sp-api/sp-api-common-display.php

Locate in that file the function sp_build_name_display() - which is about three-quarters of the way through it.

Immediately after the 'global' statement at the top - add the following line of code:

$username = apply_filters('sph_build_name_display', $username, $userid);

Now comment out ALL of the other filter functions you created earlier. Do NOT delete them yet as you may still need one or two. Now paste in the following code:

global $nameCache;
$nameCache = array();

add_filter('sph_user_class_meta', 'sph_addMetatoObj');
function sph_addMetatoObj($objList) {
    $objList['cortesia'] = 'title';
    return $objList;
}

add_action('sph_user_class', 'sph_insertMetaInDisplayName');
function sph_insertMetaInDisplayName($userObj) {
    global $nameCache;
    if(!isset($userObj->ID) || $userObj->ID == 0) return;
    if(array_key_exists($userObj->ID, $nameCache)) {
        $userObj->display_name = $nameCache[$userObj->ID];
    } else {
        $name = $userObj->cortesia . $userObj->display_name;
        $userObj->display_name = $name;
        $nameCache[$userObj->ID] = $name;
    }
}

add_filter('sph_build_name_display', 'sph_insertMetaInBuildName', 1, 2);
function sph_insertMetaInBuildName($name, $id) {
    global $nameCache;
    if($id == 0) return $name;
    if(array_key_exists($id, $nameCache)) {
        return $nameCache[$id];
    } else {
        $name = get_user_meta($id, 'cortesia', True) . $name;
        $nameCache[$id]=$name;
        return $name;
    }
}

This worked for me in tests but of course I have had to adapt it to read a usermeta item I do not have so I was unable to test it to 100%.

This should cover 90%+ of what you want. I have also included here an array cache to ensure that each user on any given page only gets processed once - save db reads and extra overhead.

You need to now go back through all display instances to see if they are covered. If not then re-introduce the appropriate code from before but see if you can also build in that cache check.

One area that is NOT covered and may be a real problem is Private Messaging. There is no way that the autocomplete of display names can be changed in this way I am afraid.

Anyway - try this and let me know how it goes. I will be in and out all day.

andy-signature.png
YELLOW
SWORDFISH
Avatar
bluantinoo
ITA
Member
Free Members
sp_UserOfflineSmall Offline
May 29, 2012 - 12:52 pm

Hi Swordfish,

sorry for delay in my answer but I had to drop the project for a couple of days.

Thank you for your efforts, this is definitly an epic support!

I've tryed the new code, actally it covers quite every instance of the Display Name.

There are just a couple of places left:

1) the Name near the avatar on the forum heder (But actually that's not important, or even better witout)

2) on the stats block, last members (I'll clone the function sp_NewMembers)

3) I'm not sure about the Most active members (always on stats block, function sp_TopPostersStats) because it's now empty... I think I have to wait a cron job 😉

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 29, 2012 - 2:57 pm

Odd. That code, for me, covered the login name beside the avatar and all of the stats - which it should do...

andy-signature.png
YELLOW
SWORDFISH
Avatar
bluantinoo
ITA
Member
Free Members
sp_UserOfflineSmall Offline
May 29, 2012 - 3:35 pm

You are right, we have 2 false alarms. I'm doing too many things together!

- It's shown near the avatar, I was using an account that actually did not have that meta set

- It's shown on the last members list, just last members as well have not still that meta set

I still cannot see any user on the "most active" list, so I cannot say about that... but it looks like it's everywhere, so I'm quite confident that you're done all, and I'm evidently tired!

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 29, 2012 - 4:33 pm

most active??

thanks for the update...

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 621
Members: 17368
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10128
Posts: 79627