Support Forum

How to filter Forum Display Name?

45 Answers

New Answer

YS Yellow Swordfish
Yellow Swordfish
Member

Two problems with that. Firstly that function will filter ALL display name plus, potentially other odd bits of data if appropriate and secondly you have no access to the users ID via that route.

BL bluantinoo
bluantinoo
Member

I’m not aware of potential oddities, but actually I need to filter display names everywhere.

About the user ID absence… maybe it can be retrieved with a custom query on wpdv object?

speaking about performances, what do you think it’s better approach?

 

ps: how do I paste code correctly here?

YS Yellow Swordfish
Yellow Swordfish
Member

if you tried that your performance hit would be noticeable. The approach we are taking is already a little expensive with all those usermeta database reads – but that would be made a lot worse.

So where else is left then?

Paste your code into the post. Select it with your mouse cursor taking care to ONLY select the code. When selected, click on the the Syntax Highlighter toolbar button (far right) and select the language.

BL bluantinoo
bluantinoo
Member

this is where it’s left:

1) Recent Post list

2) Watches List

3) subscribed list

4) forum stats

I think that’s all

BL bluantinoo
bluantinoo
Member

Ok I’m lucky (and well-educated by your advices)

So in one move I’ve manages points 1, 2 and 3

with this:

add_filter('sph_topic_list_record', 'sph_addMetatoListLastPostObj');
function sph_addMetatoListLastPostObj($objData) {
    $cortesiaTitle = get_user_meta($objData->user_id, 'cortesia', True); 
    $objData->display_name = $cortesiaTitle . ' '.$objData->display_name;
    return $objData;
}

And I found out even the Members List:

add_filter('sph_members_list_records', 'sph_addMetatoMembersListObj');
function sph_addMetatoMembersListObj($objData) {
    $cortesiaTitle = get_user_meta($objData->user_id, 'cortesia', True); 
    $objData->display_name = $cortesiaTitle . ' '.$objData->display_name;
    return $objData;
}

 

I’ve just the Forum stats left now… I really can’t find it!

BL bluantinoo
bluantinoo
Member

Well, after a deeper look I do not think there’s a way to change display names in stats as we did i previous snippets.
I cannot find any class or object with stats records.

the only way I’m able to do it is just

1) copy/paste entire fuctions (sp_OnlineStats, sp_TopPostersStats, sp_NewMembers, sp_AdminsList) in my sp theme functions.php

2) rename them (sp_OnlineStats2, sp_TopPostersStats2, sp_NewMembers2, sp_AdminsList2)

3) adding my meta in a procedural way when theese functions build the Display name

4) Use theese cloned functions in my theme spFoot.php

am I correct?

YS Yellow Swordfish
Yellow Swordfish
Member

You will have to give me a little time as I need to disappear for a few hours but I will look closely later. we can work something out – don’t worry.
By the way – whereabouts in Italy are you if you don’t mind me asking?

BL bluantinoo
bluantinoo
Member

no problem Swordfish, take your time.

for the moment I’ll clone the functions, if you will come up with a new wonder I’ll take it happily

I’m from Rome (actually I can see San Peter’s dome from my window) are you thinking to visit me? 🙂

YS Yellow Swordfish
Yellow Swordfish
Member

Sorry but it is going to have to be in the morning now. I have a plan to help you with an easy way to fix up the rest but need to run a few tests first. I will check this out in the morning.

Well a visit to Rome would be fun actually. We have been coming to Italy a couple of times each year recently but I have still not got to Rome. We are both (my wife and I) big fans of your country. I had a few days in Venice just a couple of weeks back in fact.

MP Mr Papa
Mr Papa
Member

Ah, Roma!  love it there!  Have been maybe 10 or 12 times…  All but one on business, but always spend extra time there… Last trip a couple years ago and got to take the whole family!  great city!

BL bluantinoo
bluantinoo
Member

@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! 🙂

YS Yellow Swordfish
Yellow Swordfish
Member

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.

BL bluantinoo
bluantinoo
Member

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 😉

YS Yellow Swordfish
Yellow Swordfish
Member

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

BL bluantinoo
bluantinoo
Member

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!