MyBB Community Forums

Full Version: Plugin Authoring For Beginners [Part 1] / How to create plugins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Okay, here's the whole file.

<?php
	if(!defined("IN_MYBB"))
		{
			die("You cannot access this file directly. Please make sure IN_MYBB is defined.");
		}
	$plugins->add_hook('global_start', 'simplenewsbar_global_start');
	function simplenewsbar_info()
		{
			return array(
				"name" => "Simple News Bar",
				"description" => "Adds a simple news bar to the top of every page on your forum",
				"website" => "http://seabtech.empirehostings.net",
				"author" => "Seabody",
				"authorsite" => "http://seabtech.empirehostings.net",
				"version" => "1.0",
				"guid" => "",
			);
		}
	function simplenewsbar_activate()
		{
			global $db;
			
			$simplenewsbar_group = array(
				'gid' => 'NULL',
				'name' => 'simplenewsbar',
				'title' => 'Simple News Bar',
				'description' => 'Settings for the Simple News Bar plugin',
				'disporder' => "1",
				'isdefault' => "0",
			);
			$db->insert_query('settinggroups', $simplenewsbar_group);
			$gid = $db->insert_id();
			
			$simplenewsbar_setting = array(
				'sid' => 'NULL',
				'name' => 'simplenewsbar_input',
				'title' => 'Text to Display',
				'description' => 'Enter the text you want to display',
				'optionscode' => 'textarea',
				'value' => 'It lives',
				'disporder' => '1',
				'gid' => intval($gid),
			);
			$db-insert_query('settings', $simplenewsbar_setting);
			rebuild_settings();
		}
	function simplenewsbar_deactivate()
		{
			global $db;
			$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('myfirstplugin_enable')");
			$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='myfirstplugin'");
			rebuild_settings();
		}
	function simplenewsbar_global_start()
		{
			global $mybb;
			echo $mybb->settings['simplenewsbar_input'];
		}
?>

If there's nothing wrong, I'll do it manually (maybe it's something about Plugin Uploader).
I've corrected and commented out a few small errors below. Make sure you're saving the file as simplenewsbar.php

<?php
    if(!defined("IN_MYBB"))
        {
            die("You cannot access this file directly. Please make sure IN_MYBB is defined.");
        }
    $plugins->add_hook('global_start', 'simplenewsbar_global_start');
    function simplenewsbar_info()
        {
            return array(
                "name" => "Simple News Bar",
                "description" => "Adds a simple news bar to the top of every page on your forum",
                "website" => "http://seabtech.empirehostings.net",
                "author" => "Seabody",
                "authorsite" => "http://seabtech.empirehostings.net",
                "version" => "1.0",
                "guid" => "",
            );
        }
    function simplenewsbar_activate()
        {
            global $db;
            
            $simplenewsbar_group = array(
                'gid' => 'NULL',
                'name' => 'simplenewsbar',
                'title' => 'Simple News Bar',
                'description' => 'Settings for the Simple News Bar plugin',
                'disporder' => "1",
                'isdefault' => "0",
            );
            $db->insert_query('settinggroups', $simplenewsbar_group);
            $gid = $db->insert_id();
            
            $simplenewsbar_setting = array(
                'sid' => 'NULL',
                'name' => 'simplenewsbar_input',
                'title' => 'Text to Display',
                'description' => 'Enter the text you want to display',
                'optionscode' => 'textarea',
                'value' => 'It lives',
                'disporder' => '1',
                'gid' => intval($gid),
            );
           // Changed a typo here. - should have been ->
            $db->insert_query('settings', $simplenewsbar_setting);
            rebuild_settings();
        }
    function simplenewsbar_deactivate()
        {
            global $db;
            // Forgot to change 'myfirstplugin' to 'simplenewsbar'
            $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_input')");
            $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='simplenewsbar'");
            rebuild_settings();
        }
    function simplenewsbar_global_start()
        {
            global $mybb;
            echo $mybb->settings['simplenewsbar_input'];
        }
?>
Thanks for the tutorial mate.

However, I'd still suggest that users have a basic understanding of PHP before trying to learn it here.

But for users that have a basic understanding of PHP, this tutorial is perfect.

Great share!
Thanks for the feedback, JaySF. I've updated the OP and provided some useful PHP tutorial links :-)
any one make this plugin for me to find location of user and it will displayed in post

<?php
$ip='94.219.40.96';
print_r(geoCheckIP($ip));
//Array ( [domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen )

//Get an array with geoip-infodata
function geoCheckIP($ip)
{
//check, if the provided ip is valid
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP is not valid");
}

//contact ip-server
$response=@file_get_contents('http://www.netip.de/search?query='.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Error contacting Geo-IP-Server");
}

//Array containing all regex-patterns necessary to extract ip-geoinfo from page
$patterns=array();
$patterns["domain"] = '#Domain: (.*?)&nbsp;#i';
$patterns["country"] = '#Country: (.*?)&nbsp;#i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';

//Array where results will be stored
$ipInfo=array();

//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}

return $ipInfo;
}

?>

function geoCheckIP($ip)

{

//check, if the provided ip is valid

if(!filter_var($ip, FILTER_VALIDATE_IP))

{

throw new InvalidArgumentException("IP is not valid");

}

//contact ip-server

$response=@file_get_contents('http://www.netip.de/search?query='.$ip);

if (empty($response))

{

throw new InvalidArgumentException("Error contacting Geo-IP-Server");

}

//Array containing all regex-patterns necessary to extract ip-geoinfo from page

$patterns=array();

$patterns["domain"] = '#Domain: (.*?)&nbsp;#i';

$patterns["country"] = '#Country: (.*?)&nbsp;#i';

$patterns["state"] = '#State/Region: (.*?)<br#i';

$patterns["town"] = '#City: (.*?)<br#i';

//Array where results will be stored

$ipInfo=array();

&nbsp;

//check response from ipserver for above patterns

foreach ($patterns as $key => $pattern)

{

//store the result in array

$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';

}
/*I've included the substr function for Country to exclude the abbreviation (UK, US, etc..)
To use the country abbreviation, simply modify the substr statement to:
substr($ipInfo["country"], 0, 3)
*/
$ipdata = $ipInfo["town"]. ", ".$ipInfo["state"].", ".substr($ipInfo["country"], 4);

return $ipdata;

}
Hi guys,

Just a thought (not sure if it will make any senseSmile).

There is a comma (",") after last element in each array in the code-could it be a reason for failure?

(2012-09-22, 09:55 AM)Seabody Wrote: [ -> ]Okay, here's the whole file.

<?php
	if(!defined("IN_MYBB"))
		{
			die("You cannot access this file directly. Please make sure IN_MYBB is defined.");
		}
	$plugins->add_hook('global_start', 'simplenewsbar_global_start');
	function simplenewsbar_info()
		{
			return array(
				"name" => "Simple News Bar",
				"description" => "Adds a simple news bar to the top of every page on your forum",
				"website" => "http://seabtech.empirehostings.net",
				"author" => "Seabody",
				"authorsite" => "http://seabtech.empirehostings.net",
				"version" => "1.0",
				"guid" => "",
			);
		}
	function simplenewsbar_activate()
		{
			global $db;
			
			$simplenewsbar_group = array(
				'gid' => 'NULL',
				'name' => 'simplenewsbar',
				'title' => 'Simple News Bar',
				'description' => 'Settings for the Simple News Bar plugin',
				'disporder' => "1",
				'isdefault' => "0",
			);
			$db->insert_query('settinggroups', $simplenewsbar_group);
			$gid = $db->insert_id();
			
			$simplenewsbar_setting = array(
				'sid' => 'NULL',
				'name' => 'simplenewsbar_input',
				'title' => 'Text to Display',
				'description' => 'Enter the text you want to display',
				'optionscode' => 'textarea',
				'value' => 'It lives',
				'disporder' => '1',
				'gid' => intval($gid),
			);
			$db-insert_query('settings', $simplenewsbar_setting);
			rebuild_settings();
		}
	function simplenewsbar_deactivate()
		{
			global $db;
			$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('myfirstplugin_enable')");
			$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='myfirstplugin'");
			rebuild_settings();
		}
	function simplenewsbar_global_start()
		{
			global $mybb;
			echo $mybb->settings['simplenewsbar_input'];
		}
?>

If there's nothing wrong, I'll do it manually (maybe it's something about Plugin Uploader).
No, that's perfectly OK. I've built a couple of working plugins now with commas after the array. Smile I believe (though I could be wrong) that arrays close the statement, otherwise I could concatenate the strings into one string if I was so inclined.
You're absolutely right! just checked it in PHP specs - it's officially allowed to put commas at the end Big Grin

Are you still having the problem? (and just to confirm) Is the problem only related to Plugin Uploader or the plugin doesn't want to work at all?

(2012-11-26, 05:43 AM)Seabody Wrote: [ -> ]No, that's perfectly OK. I've built a couple of working plugins now with commas after the array. Smile I believe (though I could be wrong) that arrays close the statement, otherwise I could concatenate the strings into one string if I was so inclined.
No, I've abandoned that plugin. I was getting headaches because I knew it'd be a simple answer that I just couldn't figure out. Toungue
Great guide. definitely something to help people get started Smile
Pages: 1 2 3 4 5 6