MyBB Community Forums

Full Version: [How to] Add poster avatar and formatted username in editpost!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I asked a while ago in the Support forum a solution for my target which was display the user's basic informations (formatted username and avatar) in editpost view.

I finally succeeded after a long developing period to have a working solution that doesn't affect too much the database. I used the awesome Patches plugin by frostschutz to edit editpost.php file within the following steps: I recommend using it.

1. Edit the query

Since editpost.php executes a simple_select query to fetch the post data from posts table, we have to edit it to include (LEFT JOIN) the users table to fetch the user data relative to the post. Around line 42, find and replace:

$query = $db->simple_select("posts", "*", "pid='$pid'");

with

$query = $db->query("
                  SELECT p.*, u.username AS userusername, u.avatar AS useravatar, u.usergroup AS userusergroup, u.displaygroup AS dispusergroup, u.uid AS useruid
                  FROM ".TABLE_PREFIX."posts p
                  LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid=p.uid)
                  WHERE p.pid='".$pid."'
              ");

2. Format usernames, avatar and dateline

Straight before the last eval (eval("\$editpost = \"".$templates->get("editpost")."\";")Wink, add the following code:

$post['avatar'] = $post['useravatar'];
$post['profilelink'] = format_name($post['userusername'], $post['userusergroup'], $post['dispusergroup']);
$post['profilelink'] = build_profile_link($post['profilelink'], $post['useruid']);
$post['postdate'] = my_date($mybb->settings['dateformat'], $post['dateline']);
$post['posttime'] = my_date($mybb->settings['timeformat'], $post['dateline']);

You can adapt it to your needs.

3. Edit editpost template

Now that we've edited editpost.php we have to include our new variables into our editpost templates. {$post['profilelink']}, for example, will display the formatted username (with link) of the user who posted the post. Just place them where you want them to appear and that's it!

Feel free to contribute to a better method if you have one. I don't know how to avoid querying the database at the moment so this is the best I can do.
I've not tested this, but something like this would be my preferred way (meaning it could in theory easily be converted into a plugin if you wanted to):

function userinfo() { //Not really needed, but if you're planning to plugin it, required.
global $post; //Get access to $post, required to properly work
include_once('inc/functions.php'); //Include the relevant function
$useredit = get_user($post['uid']); //Get the poster's info
}

Then I could use variables like {$useredit['useravatar']} in my templates.

As I say though, I've not tested this, so I could be way off the mark.
That should work fine Seabody, but get_user() function queries for too many informations related to the user which we don't need to use. I suppose it would end up with a performance drop, whereas modifying the core query with only the necessary data is a bit more efficient I guess.
I see your point. However, it also lets us do this without any core edits, meaning we wouldn't need to revert it for 1.6.9 and 1.8's eventual release.

E: And looking back on your OP, I missed the formatted part. Toungue So yours is indeed better.