Skip to main content
Home
The Baheyeldin Dynasty
The journey for wisdom starts with knowledge
  • Home
  • About
  • Site Map
  • Contact

Userpoints: How To Display User Points On All Pages

  1. Home

By Khalid on 2005/12/02 - 14:15, last updated 2005/12/02 - 14:28

Lars J wrote me asking how to display points with every user's info on all pages.

This article is the answer to this question.

Prerequisites

Before you can use this method, you have to be using the latest user points module.

You must have a version that supports the new call.

//$Id: userpoints.module,v 1.21 2005/10/30 02:23:02 kbahey Exp $ 

The version should be 1.21 or later. 

Method

Basically, Drupal has a central function for displaying user info. This function is called format_name() in the file includes/common.inc . So the solution relies on changing that function to include the number of points with the user name.

As always, if you are not comfortable editing program files, then do not do it. 

The function reads partially like this:

function format_name($object) {

if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (strlen($object->name) > 20) {
$name = truncate_utf8($object->name, 15) .'...';
}
else {
$name = $object->name;
}

if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $name;
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = '<a href="'. $object->homepage .'">'. $object->name .'</a>';
}
else {
$output = $object->name;
}

$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}

return $output;
} 

 What needs to be done is making it look like this:

function format_name($object) {

if ($object->uid && $object->name) {
// Shorten the name when it is too long or it will break many tables.
if (strlen($object->name) > 20) {
$name = truncate_utf8($object->name, 15) .'...';
}
else {
$name = $object->name;
}

if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
$output = $name;
}

if (module_exist('userpoints')) {
$output .= '(' . userpoints_get_current_points($object->uid) . ')';
}
}
else if ($object->name) {
// Sometimes modules display content composed by people who are
// not registered members of the site (e.g. mailing list or news
// aggregator modules). This clause enables modules to display
// the true author of the content.
if ($object->homepage) {
$output = '<a href="'. $object->homepage .'">'. $object->name .'</a>';
}
else {
$output = $object->name;
}

$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}

return $output;
}

 The only lines we added are the following:

    if (module_exist('userpoints')) {
$output .= '(' . userpoints_get_current_points($object->uid) . ')';
} 

These will cause every instance of users displayed on the site to have the number of points they have in brackets following the name.

You can add a div to the output so you can theme the points anyway you like with CSS in your stylesheets. 

Enjoy ... 

Contents: 
Drupal
  • Add comment

Comments

Lars Jansen (not verified)

Showing "0"

Fri, 2005/12/02 - 21:12

Hi there once more Khalid,

Firstly, thanks for this article. It not only demonstrates a great new trick but also gives me ideas for other things I can do.

It turns out I was using an older version of the module, hence the errors I was recieving.

I still have a problem though. If I add

<?php print userpoints_get_current_points($uid);?>

to my page.tpl.php it always prints "0" as my number of points even though I have 22 points in actual fact. (the points block confirms this) Even if I log in as another user I still get 0 points.

Any ideas why this might be?

Lars

  • reply

Khalid

No $uid

Fri, 2005/12/02 - 22:45

Yes, the $uid variable is not initialized to anything, and hence the query returns zero.

To solve this, you can use:

<?php global $user;print userpoints_get_current_points($user->uid) ?>

However, this will print the points for the current logged in user, and not every user.

Not sure if that is what you want ...

  • reply

Lars Jansen (not verified)

Thats perfect!

Sun, 2005/12/04 - 00:34

Khalid,

Thats PERFECT! Just what I was after, thank you so much for your time with this.

Using your example above I can start to build the member welcome on my pages. Like this:

"Welcome back 'username', you have (x) points."

Then I will use a if/else php thingy so that a non-logged in user see's a "register" link instead!

Awesome stuff....I love PHP:)

Thanks again Khalid!

  • reply

Khalid

My pleasure

Sun, 2005/12/04 - 00:53

You are welcome ...

  • reply

Johnie (not verified)

Not working for me.

Fri, 2009/02/27 - 18:25

Hello, I have tried this code, and for some reason it is still not working on my site.
This is the 5th post that I have read.

Can anyone help me out?
Thanks!

  • reply

Current

Pandemic

  • COVID-19
  • Coronavirus

Search

Site map

Contents

  • Family
    • Khalid
    • Ancestry
    • Extended
  • Friends
  • Nokat نكت
  • Writings
    • Cooking
    • Culture
    • Science
    • History
    • Linguistics
    • Media
    • Literature
    • Politics
    • Humor
    • Terrorism
    • Business
    • Philosophy
    • Religion
    • Children
  • Technology
    • Linux
    • Arabization
    • Drupal
      • Association
    • Software
    • Internet
    • Technology in Society
    • Digital Archeology
    • NCR History
    • MidEast Internet
    • Programming
    • Saudi ISPs
    • Miscellaneous
  • Places
    • Canada
      • Weather
    • Egypt
      • Cuisine
      • Alexandria
      • E.G.C.
    • USA
    • Saudi Arabia
  • Interests
    • Astronomy
    • Fishing
    • Photography
    • Snorkeling
    • Nature
    • Photomicroscopy
  • Miscellany

In Depth

  • al-Hakim bi Amr Allah: Fatimid Caliph of Egypt الحاكم بأمر الله
  • Alexandria, Egypt
  • Arabic on the Internet
  • Articles on the history of Muslims and Arabs in the Iberian Peninsula تاريخ المسلمين و العرب في الأند
  • DIY GOTO Telescope Controller With Autoguiding and Periodic Error Correction
  • E.G.C. English Girls College in Alexandria, Egypt
  • Egyptian Cuisine, Food and Recipes مأكولات مصرية
  • George Saliba: Seeking the Origins of Modern Science?
  • Internet Scams and Fraud
  • Mistaken for an Arab or Muslim: Absurdities of being a victim in the War on Terror
  • Mistaken Identity: How some people confuse my site for others
  • One People's Terrorist Is Another People's Freedom Fighter
  • Overview of Google's Technologies
  • Photomicroscopy
  • Pseudoscience: Lots of it around ...
  • Resources for using Google Adsense with Drupal
  • Rockwood Conservation Area, Southern Ontario
  • Selected Symbolic Novels And Movies
  • Snorkeling the Red Sea near Jeddah
  • Updates and Thoughts on the Egyptian Revolution of 2011

Recent Content

Most recent articles on the site.

  • Origin Of COVID-19: Natural Spillover, Lab Leak Or Biological Weapon?
  • Kamal Salibi and the "Israel from Yemen" theory
  • How To Upgrade HomeAssistant Core In A Python Venv Using uv
  • Ancestry - Paternal Side
  • Review of Wait Water Saver For Whole House Humidifiers
more

Most Comments

Most commented on articles ...

  • Another scam via Craigslist: offering more than asking price
  • Warning to female tourists thinking of marrying Egyptians
  • Craigslist classified for used car: Cheque fraud scam
  • Winning the lottery scam email: World Cup South African lottery
  • Email Scam: BMW 5 Series car and lottery winning
more

About Khalid

Various little bits of information ...

  • Khalid Baheyeldin: brief biography
  • Presentations and Talks
  • Youtube Videos
  • GitHub Projects
  • Drupal.org Profile
  • Astrophotography @ Flickr

Sponsored Links

Your Link Ad Here

Tags

Android Mobile Ubuntu Sony OnStep OpenWRT Router Ericsson COVID-19 Rogers Coronavirus Arabic Kubuntu Home Assistant GSM Telescope tablet Spectrum Scam Python 419 Laptop Firefox DIY CPU Conspiracy Comet Balkanization backup App
More

© Copyright 1999-2025 The Baheyeldin Dynasty. All rights reserved.
You can use our content under the Terms of Use.
Please read our privacy policy before you post any information on this site.
All posted articles and comments are copyright by their owner, and reflect their own views and opinions, which may not necessarily be consistent with the views and opinions of the owners of The Baheyeldin Dynasty.

Web site developed by 2bits.com Inc.