MyBB Community Forums

Full Version: Sticky post icon
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I'm working on a very small plugin per request which sets sticky threads to have a specific post icon. I was able to edit this directly in the core and it worked just fine. But I can't get this to work as a plugin at all, the post icon is just not updated. Here is the hook bit and the function I'm using:

$plugins->add_hook("forumdisplay_thread", "stickyposticon_run");

function stickyposticon_run()
{
	global $thread, $icon;
	
	if($thread['sticky'] == "1")
	{
		$icon = '<img src="http://community.mybb.com/images/offlock.gif" />';
	}
}

The offlock image is just to make sure the post icon is updated. I'll be inserting some settings later when I get this to work. Does anyone have any ideas as to why this isn't working?
If you look at the source of forumdisplay.php, you'll see that the icon is set after the hook is run - meaning any icon you set will just be replaced.

$plugins->run_hooks("forumdisplay_thread");

		$moved = explode("|", $thread['closed']);

		if($thread['visible'] == 0)
		{
			$bgcolor = "trow_shaded";
		}
		else
		{
			$bgcolor = alt_trow();
		}
		
		if($thread['sticky'] == 1)
		{
			$thread_type_class = " forumdisplay_sticky";
		}
		else
		{
			$thread_type_class = " forumdisplay_regular";
		}

		$folder = '';
		$prefix = '';

		$thread['author'] = $thread['uid'];
		if(!$thread['username'])
		{
			$thread['username'] = $thread['threadusername'];
			$thread['profilelink'] = $thread['threadusername'];
		}
		else
		{
			$thread['profilelink'] = build_profile_link($thread['username'], $thread['uid']);
		}
		
		// If this thread has a prefix, insert a space between prefix and subject
		if($thread['prefix'] != 0)
		{
			$thread['threadprefix'] .= '&nbsp;';
		}

		$thread['subject'] = $parser->parse_badwords($thread['subject']);
		$thread['subject'] = htmlspecialchars_uni($thread['subject']);

		if($thread['icon'] > 0 && $icon_cache[$thread['icon']])
		{
			$icon = $icon_cache[$thread['icon']];
			$icon = "<img src=\"{$icon['path']}\" alt=\"{$icon['name']}\" />";
		}
		else
		{
			$icon = "&nbsp;";
		}

Problem is, there don't seem to be any hooks run later that'll let you set the icon.
Hook is fine, try this function;
function stickyposticon_run()
{
    global $thread, $icon, $templates;
    
    if ($thread['sticky'] == 1)
    {
        $icon = '<img src="http://community.mybb.com/images/offlock.gif" />';
    }
}
@Yaldaram: That's exactly the same except you've added $templates to the globals...
Thanks for the replies. Smile

I've tried that before Yaldaram, along with a ton of other silly things, with no success. Even so I gave it another shot but as expected, it didn't work. I believe what euantor said is correct because no matter what I do, the little bit of code that sets the post icons will override whatever I did in my plugin.

I tried the other two available hooks, forumdisplay_start and forumdisplay_end, but those didn't work either. So unless there is a workaround for this, I think I'll just give up on this plugin.
Short of altering the templates to use a different variable than $icon, I can't think of a workaround I'm afraid.
Amazing.. Undecided I've used the same hook in my plugin: http://yaldaram.com/showthread.php?tid=362 and its working.

Give it a try too;
function stickyposticon_run()
{
    global $mybb, $thread, $icon, $templates;
    
    if ($thread['sticky'])
    {
        $icon = '<img src="http://community.mybb.com/images/offlock.gif">';
    }
}

I have tried that before too Yaldaram, and once again, it did not work. Your plugin doesn't mess with post icons, only other things which are covered by the forumdisplay_thread hook.
Can you please PM me your plugin code ? I'll test and try to resolve it.
I'm pretty sure euantor is right, but here is the code anyway:

<?php
/**
 * Sticky Post Icon 1.0
 * Copyright 2011 Fábio Maia, All Rights Reserved
 */
 
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("forumdisplay_thread", "stickyposticon_run");

function stickyposticon_info()
{
	return array(
		"name"			=> "Sticky Post Icon",
		"description"	=> "A plugin which sets a specific post icon to all sticky threads.",
		"website"		=> "http://mybb.com",
		"author"		=> "Fábio Maia",
		"authorsite"	=> "http://community.mybb.com/user-16693.html",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "14*,16*"
	);
}

function stickyposticon_run()
{
    global $thread, $icon;
    
    if ($thread['sticky'])
    {
        $icon = '<img src="http://community.mybb.com/images/offlock.gif">';
    }
}
?>
Pages: 1 2