Support Forum

How to filter Forum Display Name?

BL bluantinoo
bluantinoo
Member

Helllo,

I need to filter user’s names to show a title before name.

I already have this info as a wp usermeta.

I’ve opened the spTopicView.php template, where I can find, for example, the author post column: sp_PostIndexUserName(‘tagClass=spPostUserName spCenter’);

but I’m not able to create a filter to alter that function.

—-

Same problem trying to show custom profile fields. I’ve found this topic https://simple-press.com/support-forum/sp5-general-topics/custom-profile-fields-4/ where Mr Papa tells how to show a custom profile field with sp_CustomProfileFieldsDisplay($name, $userid);

but how do I get the $userid in forum loops?

45 Answers

New Answer

BL bluantinoo
bluantinoo
Member

I’m answering myself to the second question:

How to get user id inside Simple Press Post Loop?

This way: $spThisPostUser->ID

please correct me if I’m wrong.

 

But I still need to filter forum display name adding a title (Prof, Doc, Mr., etc…) before the name.

Is there a way to do this on forum display name or do I need to add this manually on template files?

YS Yellow Swordfish
Yellow Swordfish
Member

Let’s ask something else first. Are you wanting to do this everywhere the display name is used? because that can be a LOT of places… Or are we talking about very specific places?

BL bluantinoo
bluantinoo
Member

I know it’s a lot of places we are talking about.

is for that I’m looking for a filter, because I need that title to be always before display name (if that field is set, obviously)

I asked about the $spThisPostUser just because it was a related issue that I was supposing quick to answer

YS Yellow Swordfish
Yellow Swordfish
Member

OK. so confirm for me. This item of data is in the usermeta table? Correct?
Give me a little time to think abiyut this because there may be a better way.

BL bluantinoo
bluantinoo
Member

Yes is a usermeta that I’ve already asked in wp profile to users.

but if it becomes too difficult I can even ask it again in the forum profile, I do not want to edit SP core files.

YS Yellow Swordfish
Yellow Swordfish
Member

Do you know how to use and code for WP filters?

BL bluantinoo
bluantinoo
Member

Not very well, but I can try if you give me some hint I’m not scared to study wp codex

YS Yellow Swordfish
Yellow Swordfish
Member

Ok Try this. bear in miund I have not really tested this myself!

add_filter('sph_user_class_meta', 'function1');
function function1($objList) {
    $objList['XXX'] = 'title';
}

add_action('sph_user_class', 'function2');
function function2($userObj) {
    $userObj->display_name = $userObj->XXX . $userObj->display_name;
}

Change ‘function1’ and ‘function2’ for your own function names in both the add filter/action calls and in the small functions themselves.

Change the 2 instances of XXX for the NAME of the meta item in the usermeta table.

And this code is probably best placed in the spFunctions.php file of the SP theme.

If this works it isn’t going to change every instance but it should change quite a lot of them.

BL bluantinoo
bluantinoo
Member

I’m having some troubles.

I’ve followed yourvery clear instructions, but maybe I’m done something wrong.

this is what I’ve pasted in the spFunctions.php of my SP theme

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

add_action('sph_user_class', 'sph_insertMetaInDisplayBame');
function sph_insertMetaInDisplayBame($userObj) {
    $userObj->display_name = $userObj->cortesia . $userObj->display_name;
}

I’m having 3 different error messages, first two repeats a lot of times and the page looks like hanging in an infinite loop, at the end loads the third message and the whole site (without the meta before the names)

Theese are the 3 errors:

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /web/problem-with-post-edit-buttontdocs/www.[…]/wp-content/plugins/simple-press/sp-api/sp-api-class-user.php on line 131

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /web/problem-with-post-edit-buttontdocs/www.[…]/wp-content/plugins/simple-press/sp-api/sp-api-class-user.php on line 142

Warning: Invalid argument supplied for foreach() in /web/problem-with-post-edit-buttontdocs/www.sviluppointergraf.it/problem-with-post-edit-buttonome/[…]/wp-content/plugins/simple-press/sp-api/sp-api-class-user.php on line 294

(I’ve added the […] to hide real URL)

THis happens in forum home, forum view and topic view

YS Yellow Swordfish
Yellow Swordfish
Member

Whoops. Sorry – I left out a line of code..

Should be:

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

Needs that return statement.

BL bluantinoo
bluantinoo
Member

Yep it’s working!

it’s missing just on “first post from” and “last post from” labels on forum lists

thank you very much! 🙂

just one little more question, as I do not fully understand you code…
in this line: $objList[‘cortesia’] = ‘title’;

if “cortesia” stands for the name of my usermeta, what “title” stands for?

MP Mr Papa
Mr Papa
Member

title is simple a flag or switch for us… it tells us how to filter the object for display…

BL bluantinoo
bluantinoo
Member

but it could be whatever string, right? I’ve tried with other than “title” and it works as well

YS Yellow Swordfish
Yellow Swordfish
Member

All data that is user entered needs to be filtered and sanitised to ensure there is nothing malicious in it – like an attempt to run SQL or javascript. SP has a series of display filters that does just this and ALL data is passed through the filters before being displayed or used. The reference to ‘title’ tells the system what filter to run that data item through and is probably the best one for your needs. It does not – for example – allow HTML tags within the data.

if you go the forum admin > toolbox > data inspector and turn on the option to display spThisUser when you run, say, the front page of the forum, it will display all of the data that is a part of our user object and you will see how the filters we changed yesterday have become a part of that object.