MyBB Community Forums

Full Version: Am I understanding plugin hooks?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So instead of requesting a plugin to be made, I decided I'd try my hand at creating my own for the site I help run. My background is in Python and django for work and figured why not try my hand at some php as well. Though before I dive way deep into what I want to start making I want to make sure I understand how creating plugin hooks work.

So basically what I want to do is add a custom profile field (which is already supported) I want to create a plugin that will pull the custom field from the database and use it to show the person's twitch. I've read over the following page and just want to know if I am heading in the right direction. with how hooks work. The randomstreamer_promotion function is most likely completely wrong. Any help on or point to the right direction for the function would be great. From what I understand what I have would replace the entire portal page. I'm still looking up how to only do a certain area of the page. And yes I know I still have to set up the rest of the plugin. I also the know the query I have right now would select all the twitchaccounts form the profilefields and only return the first one. I'm going to create a random number based on how many accounts have the field filled in and have the random number be the selection of which user to pick.


<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}

$plugins->add_hook('portal_start', 'randomstreamer_promotion');

function otgs_info()
{
	return array(
		"name"			=> "OTGS",
		"description"	=> "Team G Twitch Streamer",
		"website"		=> "example.com",
		"author"		=> "TheCrzyDoctor",
		"authorsite"	=> "example.com",
		"version"		=> "1.0",
		"guid" 			=> "",
		"codename"		=> "",
		"compatibility" => "18*"
	);
}

function otgs_install()
{

}

function otgs_is_installed()
{

}

function otgs_uninstall()
{

}

function otgs_activate()
{

}

function otgs_deactivate()
{

}

function randomstreamer_promotion($page){
	// This is most likely completely all wrong (still reading up on how to do this..)

	// pull text from custom field	
	$query = $db->simple_select("profilefields", "*", "name='TwitchAccount'");
	
	$streamer_to_promote = $db->fetch_field($query))
	
	$page = str_replace(
		'<html>
			<body>
				<!-- Add a placeholder for the Twitch embed -->
				<div id="twitch-embed"></div>

				<!-- Load the Twitch embed script -->
				<script src="https://embed.twitch.tv/embed/v1.js"></script>

				<!-- Create a Twitch.Embed object that will render within the "twitch-embed" root element. -->
				<script type="text/javascript">
					new Twitch.Embed("twitch-embed", {
					width: 854,
					height: 480,
					channel: $streamer_to_promote
				});
				</script>
			</body>
		</html>'
	);
	return $page;

}
If you are using single quotes you cannot use PHP variables directly in the string. You have to concatenate them, just like python.

"portal_start" doesn't send any parameter so "$page" is undefined.
Thanks for pointing that out. So after a little more research I saw that you are right. portal_start had no parameter. Since that was the case I searched how to use variables and send them to templates. This is what I found. Hopefully i'm getting closer to this. After work today, I'll be making the custom profile field and uploading the plugin. I just want to make sure my hook will work correctly the first time around.

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}

$plugins->add_hook('portal_start', 'randomstreamer_promotion');

function otgs_info()
{
	return array(
		"name"			=> "OTGS",
		"description"	=> "Team G Twitch Streamer",
		"website"		=> "example.com",
		"author"		=> "TheCrzyDoctor",
		"authorsite"	=> "example.com",
		"version"		=> "1.0",
		"guid" 			=> "",
		"codename"		=> "otgs",
		"compatibility" => "18*"
	);
}

function otgs_install()
{

}

function otgs_is_installed()
{

}

function otgs_uninstall()
{

}

function otgs_activate()
{

}

function otgs_deactivate()
{

}

function randomstreamer_promotion($page){
	// This is most likely completely all wrong (still reading up on how to do this..)

	// Bring in templates to use templates
	global $templates, $db;
	
		
	// pull text from custom field	
	$query = $db->query("SELECT * FROM profilefields WHERE twitchaccount IS NOT NULL");
	
	// lets get how many rows are in the query
	$max_number = $db->num_rows($query);
	
	// lets generate a random number to select what streamer to promote.
	$streamer_num = rand(1, $max_number);		
	
	// set text from query to variable.
	$var = $db->fetch_field($query)) // still need to figure out how to get the $treamer_num row from the query.
	
	// get template to use variable in.
	eval('$streamer = "'.$template->get('portal').'";');
	
	echo $streamer; // now if i use {{streamer}} inside my portal template I should get $va?
	
	
}
You do not have to use eval, because you are hooking into an existing template, and $page are not available in that hook.

Only use
function randomstreamer_promotion(){
//your code...
}

Then globalize vars that you will use and add it into portal templates where that hook are loaded.

That way you have this working on.

Or use pre_output_page hook to load it into a new page and use your own customized templates beside actual ones.

And you may require a new page with the content to load to use that hook propertly, or add it into all posible places due is a global hook to work with. But portal_start and portal_end only works into portal templates where are called.

Cheers.
(2018-01-23, 08:13 PM)Whiteneo Wrote: [ -> ]You do not have to use eval, because you are hooking into an existing template, and $page are not available in that hook.

Only use
function randomstreamer_promotion(){
//your code...
}

Then globalize vars that you will use and add it into portal templates where that hook are loaded.

That way you have this working on.

Or use pre_output_page hook to load it into a new page and use your own customized templates beside actual ones.

And you may require a new page with the content to load to use that hook propertly, or add it into all posible places due is a global hook to work with. But portal_start and portal_end only works into portal templates where are called.

Cheers.

I meant to remove the $page variable. That is my mistake. Thank you for pointing that out. I only want this plugin to work with my portal as there is no other place on my site I will need it. I'll be testing this out with a hard coded twitch username later tonight. Still reading up on MyBB does sql queries. Thanks for all the help.