Support Forum

How to filter Forum Display Name?

45 Answers

New Answer

BL bluantinoo
bluantinoo
Member

Ok,

I’ve looked forward for trying to add that usermeta in places where it is not shown with that awesome filter.

I mean in places like “last post” “topic started”

I see that datas are shown by functions like

sp_TopicIndexFirstPost

sp_TopicIndexLastPost

is there a way to filter theese as well?

YS Yellow Swordfish
Yellow Swordfish
Member

This is where it gets a bit moire tricky.
You would need to use the filter on the code that builds the data object. This would give you an array of topic data along with the posts data which would include the user ID and the display name.
Your code would then need to traverse the array and change all the instances of display_name. You would need to lookup the data you want to prepend for each user as you work through the data array.
No so easy

BL bluantinoo
bluantinoo
Member

Hi Sword,

sorry to persist bothering you with this issue, but I need some more help to achieve this.

Let’s start with the “group view” screen (that I think is the forum home, right?).

Using the wonderful Data Inspector I can see that there are 3 objects in Group View Data that contain the user display name.

1) spThisForum

2) spThisGroup

3) spGroupView

Wich one would be better try to manipulate to filter display name of “last message from” in Group View?

YS Yellow Swordfish
Yellow Swordfish
Member

Well it’s $spThisForum at that point and the last post column has a filter defined of ‘sph_ForumIndexLastPost’ but that is going to contain all of the text for that display element – not just the display name.

However the ‘sph_groupview_stats_records’ filter will pass an array of all the data for the page (the stats data that is) which would be better. You could then loop through the array changing each instance of display_name to what you need. This filter passes two parameters – the data object we are going to use for the final display and a second parameter containing the raw data as retrieved from the database. You would want to operate ion the first of course.

BL bluantinoo
bluantinoo
Member

Hi Swordfish,

I’m sorry but I really don’t understand how to start 🙁

Could you please show me some example code?

YS Yellow Swordfish
Yellow Swordfish
Member

Yes but I need time for that which I am a little short off at the moment. I will do my best to help.

BL bluantinoo
bluantinoo
Member

Thank you Swordfish, take your time I’m not in a hurry.

I think this single topic is something like the best support I have ever had! 🙂

YS Yellow Swordfish
Yellow Swordfish
Member

Actually it is not as difficult as I first thought.

Define a new add-filter and function – just like before – only this one will use the ‘sph_groupview_stats_records’ filter.

In this case the $objData that will be passed in will have the display name available as:

$objData->display_name

so you will also need the $objData->user_id

So – using the $objData->user_id – go and get hold of the item you need from the userneta table and load it into a variable (say $x)

Then set $objData->display_name = $x . $objData->display_name;

Then return $iobjData at the end of the function as we did before (the line I missed first time round).

BL bluantinoo
bluantinoo
Member

Hi Swordfirsh,

thanks to your tips I’ve made it! this works on Group View:

add_filter(‘sph_groupview_stats_records’, ‘sph_addMetatoGroupViewObj’);

function sph_addMetatoGroupViewObj($objData) {
      $cortesiaTitle = get_user_meta($objData->user_id, ‘cortesia’, True);
      $objData->display_name = $cortesiaTitle . ‘ ‘.$objData->display_name;

      return $objData;

}

I must say that is actually easier than the previous one (I do not understand how the 2 add_filter and add_action work together)

Anyway… it seems I have left only “last message from” and “topic started by” on forum view. Could you suggest a further Object to work on?

I tried with just adding the same funciton in a “sph_forumview_stats_records” filter, but it didn’t work

YS Yellow Swordfish
Yellow Swordfish
Member

You DO need to use ‘sph_forumview_stats_records’ but the data items have different names.
In this case you will need to use the following:

$objData->first_user_id
$objData->first_display_name

and

$objData->last_user_id
$objData->last_display_name

However – before you do anything check that $objData->last_user_id exists using:

if(property_exists($objData->last_user_id))

Only perform your trabnsforms if it DOES exist.

BL bluantinoo
bluantinoo
Member

So I’ve made other 2 filters, one for last_user and one for first_user

add_filter(‘sph_forumview_stats_records’, ‘sph_addMetatoLastPostedByForumViewObj’);
function sph_addMetatoLastPostedByForumViewObj($objData) {
if(property_exists($objData->last_user_id)){
    $cortesiaTitle = get_user_meta($objData->last_user_id, ‘cortesia’, True);
    $objData->last_display_name = $cortesiaTitle . ‘ ‘.$objData->last_display_name;
}

    return $objData;
}

add_filter(‘sph_forumview_stats_records’, ‘sph_addMetatoTopicStartedByForumViewObj’);
function sph_addMetatoTopicStartedByForumViewObj($objData) {
if(property_exists($objData->first_user_id)){
    $cortesiaTitle = get_user_meta($objData->first_user_id, ‘cortesia’, True);
    $objData->first_display_name = $cortesiaTitle . ‘ ‘.$objData->first_display_name;
}

    return $objData;
}

but I have errors on the use of property_exist: Wrong parameter count for property_exists()

I’ve searched for that php function, and it actually needs a second parameter, but I don’t understand what to use

SO I’ve tried using isset instead : if(isset($objData->first_user_id))

and it looks like it’s working, but on the  $objData->first_display_name the “cortesia” title is prepend 2 times before display name…odd

YS Yellow Swordfish
Yellow Swordfish
Member

Yes – use isset instead. Probably better.
You should do all of this in just one function. if the last_user_id is set then you have access to all 4 of these data items. That is why you are getting it twice as it was already done first time around.

BL bluantinoo
bluantinoo
Member

Doing in one function is the first thing I’ve thought as well:

add_filter(‘sph_forumview_stats_records’, ‘sph_addMetatoTopicStartedByForumViewObj’);

function sph_addMetatoTopicStartedByForumViewObj($objData) {

if(isset($objData->first_user_id)){
    $cortesiaTitle = get_user_meta($objData->first_user_id, ‘cortesia’, True);
    $objData->first_display_name = $cortesiaTitle . ‘ ‘.$objData->first_display_name;
}

if(isset($objData->last_user_id)){
    $cortesiaTitle = get_user_meta($objData->last_user_id, ‘cortesia’, True);
    $objData->last_display_name = $cortesiaTitle . ‘ ‘.$objData->last_display_name;
}

return $objData;
}

But I still have that cortesia twice on “first_display_name”

YS Yellow Swordfish
Yellow Swordfish
Member

You have still not done as I said. Test the last_user_id. Do NOT test the first_user_id. One test – both transforms within the same if statement.

BL bluantinoo
bluantinoo
Member

You’re right, sorry I’ve missed that point.

this works 🙂

add_filter(‘sph_forumview_stats_records’, ‘sph_addMetatoForumViewObj’);

function sph_addMetatoForumViewObj($objData) {

if(isset($objData->last_user_id)){
    $cortesiaTitle = get_user_meta($objData->last_user_id, ‘cortesia’, True);
    $objData->last_display_name = $cortesiaTitle . ‘ ‘.$objData->last_display_name;
    
    $cortesiaTitle2 = get_user_meta($objData->first_user_id, ‘cortesia’, True);
    $objData->first_display_name = $cortesiaTitle2 . ‘ ‘.$objData->first_display_name;
}

return $objData;
}

Thank you very much for our patience

Let me ask just one last question about this.

Wandering in sp files I’ve found sp-api-filters.php that contains sp_filter_name_display function.

would not be quicker to filter just that?