Hook build_forumbits_forum and Templates
#1
Hi!

I'm writing a little plugin for my forum, to show a random thread between two categories. In the attachement it's the part "Eigener Inhalt" (Translation: Kategorie = Category, Eigener Inhalt = own content (like a random thread)).

Now I began writing my plugin, install/deinstall/activate/deactivate works great - but the main function creats an Error 500 as soon as I try to include a new template that I created during installation of the plugin.

The working format by now is:

$plugins->add_hook('build_forumbits_forum', 'wanted_index');

function wanted_index(&$forum)
{
  global $mybb, $db, $templates;

// Reading the settings - at which category should it be shown?
// In the settings this is a selectforumsingle
  $wanted_show = $mybb->settings['wanted_show'];

// If the ForumID isn't the selected forum, then ..
  if ($forum['fid'] != $wanted_show) return;


// In other cases
  $forum['wanted_index'] = "Test text";
}


As soon as I try calling a template with
eval('$forum['wanted_index'] = "'.$templates->get('wanted_index').'";');
it shows Error 500.

With
eval('$wanted_index = "'.$templates->get('wanted_index').'";'); 
and including {$wanted_index} in forumbit_depth1_cat (instead of {$forum['wanted_index']}), it shows nothing.


Yes, there is missing a lot of stuff, but before I write this, my question is: Is there any possibility to include a template at any way using that hook?
I'm working with MyBB 1.8.27

Thank you! Smile
Please ask if you don't understand everything. English isn't my native language so mistakes and misunderstandings happen!


Attached Files Thumbnail(s)
   
Reply
#2
You'll need to globalise $wanted_index, and I think escape some characters in the eval itself:

function wanted_index(&$forum)
{
	global $mybb, $db, $templates, $wanted_index;

	...
	eval("\$wanted_index = \"".$templates->get('wanted_index')."\";");
}

See if that works.

The reason this breaks and causes a 500:

eval('$forum['wanted_index'] = "'.$templates->get('wanted_index').'";');

is because you're using single quotes in the eval, and in the array key, so it closes the quotes and would result in a parse error. If you do it that way, you need to escape them, or use double quotes:

eval("\$forum['wanted_index'] = \"".$templates->get('wanted_index')."\";");

or:

eval('\$forum[\'wanted_index\'] = "'.$templates->get('wanted_index').'";');
MyReactions - All Plugins

Can you still feel the butterflies?

Free never tasted like pudding.
Reply
#3
Hi Matt!

Thank you very very much. Smile Now it is working! 

With the $wanted_index in global it still showed nothing.
But with the corrected eval it works fine! Smile



Thank you! Smile
Reply
#4
You're welcome Smile
MyReactions - All Plugins

Can you still feel the butterflies?

Free never tasted like pudding.
Reply
#5
$wanted_index = eval($templates->render('wanted_index'));
Reply
#6
(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

:o Since when?!
MyReactions - All Plugins

Can you still feel the butterflies?

Free never tasted like pudding.
Reply
#7
(2021-08-22, 11:48 PM)Matt Wrote:
(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

:o Since when?!

Since 1.8.0 😅 The core just wasn't updated to make use of it so it isn't as used as it should be by now.
Reply
#8
(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

I tried it (and of course make $wanted_index global again) - but nothing. I think it is because of that special hook that I need to work with the array $forum Smile
Reply
#9
$wanted_index = eval($templates->render('wanted_index'));

Is just a replacement of :

eval("\$wanted_index = \"".$templates->get('wanted_index')."\";");

It should simply work.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)