Thread Rating:
  • 5 Vote(s) - 3.6 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plugin Authoring For Beginners [Part 1] / How to create plugins
#51
(08-02-2018, 02:09 AM)kingsolomon Wrote: How do i make my plugin variable to show on index.php only



Just to give you a rough idea via previously shared code, lets comment out  code to remove portal functionality like so:

Now for example lets look at the non localized simplenewsbars.php that I first shared:
Code:
<?php
/*
* MyBB: Simple Newsbars
*
* File: simplenewsbars.php
*
* Authors: Vintagedaddyo
*
* MyBB Version: 1.8
*
* Plugin Version: 1.0
*
*/

if (!defined("IN_MYBB")) {
   die("You cannot access this file directly. Please make sure IN_MYBB is defined.");
}

$plugins->add_hook('index_start', 'simplenewsbars_index_start');
//$plugins->add_hook('portal_start', 'simplenewsbars_portal_start');

function simplenewsbars_info()
{
   return array(
       "name" => "Simple Newsbars",
       "description" => "Adds simple newsbars to the index and portal page of your forum",
       "website" => "http://community.mybb.com/user-6029.html",
       "author" => "Vintagedaddyo",
       "authorsite" => "http://community.mybb.com/user-6029.html",
       "version" => "1.0",
       "guid" => "",
       "compatability" => "18*"
   );
}

function simplenewsbars_activate()
{
   global $settings, $mybb, $db, $lang;
   
   $simplenewsbars_group = array(
       'gid' => '0',
       'name' => 'simplenewsbars',
       'title' => 'Simple Newsbars',
       'description' => 'Settings for the Simple Newsbars plugin',
       'disporder' => "1",
       'isdefault' => "0"
   );
   
   $db->insert_query('settinggroups', $simplenewsbars_group);
   
   $gid = $db->insert_id();
   
   $simplenewsbars_setting_1 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_enable_index',
       'title' => 'Do you want to enable Simple Newsbars Plugin on Index?',
       'description' => 'If you set this option to yes, this plugin be active on your Index.',
       'optionscode' => 'yesno',
       'value' => '1',
       'disporder' => '1',
       'gid' => intval($gid)
   );
   
//    $simplenewsbars_setting_2 = array(
//        'sid' => '0',
//        'name' => 'simplenewsbars_enable_portal',
//        'title' => 'Do you want to enable Simple Newsbars Plugin on Portal?',
//        'description' => 'If you set this option to yes, this plugin be active on your Portal.',
//        'optionscode' => 'yesno',
//        'value' => '1',
//        'disporder' => '2',
//        'gid' => intval($gid)
//    );
   
   $simplenewsbars_setting_3 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_input_1',
       'title' => 'Alert Display',
       'description' => 'Enter the text you want to display in Alert.',
       'optionscode' => 'textarea',
       'value' => '<strong>Latest MyBB Release:</strong> <a href="http://blog.mybb.com/2018/03/15/mybb-1-8-15-released-security-maintenance-release/">MyBB 1.8.15 Released — Security & Maintenance Release</a> <span class="date">(March 15, 2018)</span>',
       'disporder' => '3',
       'gid' => intval($gid)
   );
   
   $simplenewsbars_setting_4 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_enable_input_1',
       'title' => 'Do you want to enable Simple Newsbars Alert?',
       'description' => 'If you set this option to yes, Alert will be active on your board.',
       'optionscode' => 'yesno',
       'value' => '1',
       'disporder' => '4',
       'gid' => intval($gid)
   );
   
   $simplenewsbars_setting_5 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_input_2',
       'title' => 'Notice 1 Display',
       'description' => 'Enter the text you want to display in Notice 1.',
       'optionscode' => 'textarea',
       'value' => '<strong>Latest news from the MyBB Blog:</strong> <a href="http://blog.mybb.com/2018/01/07/mybb-support-policy-changes/">MyBB Support Policy Changes</a> <span class="date">(January 7, 2018)</span>',
       'disporder' => '5',
       'gid' => intval($gid)
   );
   
   $simplenewsbars_setting_6 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_enable_input_2',
       'title' => 'Do you want to enable Simple Newsbars Notice 1?',
       'description' => 'If you set this option to yes, Notice 1 will be active on your board.',
       'optionscode' => 'yesno',
       'value' => '1',
       'disporder' => '6',
       'gid' => intval($gid)
   );
   
   $simplenewsbars_setting_7 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_input_3',
       'title' => 'Notice 2 Display',
       'description' => 'Enter the text you want to display in Notice 2.',
       'optionscode' => 'textarea',
       'value' => '<strong>Are you on the </strong><a href="http://community.mybb.com/member.php?action=register">MyBB Community Forums</a><strong>&nbsp;?</strong> - Sign up for notification of new MyBB releases and updates.',
       'disporder' => '7',
       'gid' => intval($gid)
   );
   
   $simplenewsbars_setting_8 = array(
       'sid' => '0',
       'name' => 'simplenewsbars_enable_input_3',
       'title' => 'Do you want to enable Simple Newsbars Notice 2?',
       'description' => 'If you set this option to yes, Notice 2 will be active on your board.',
       'optionscode' => 'yesno',
       'value' => '1',
       'disporder' => '8',
       'gid' => intval($gid)
   );
   
   $db->insert_query('settings', $simplenewsbars_setting_1);
//    $db->insert_query('settings', $simplenewsbars_setting_2);
   $db->insert_query('settings', $simplenewsbars_setting_3);
   $db->insert_query('settings', $simplenewsbars_setting_4);
   $db->insert_query('settings', $simplenewsbars_setting_5);
   $db->insert_query('settings', $simplenewsbars_setting_6);
   $db->insert_query('settings', $simplenewsbars_setting_7);
   $db->insert_query('settings', $simplenewsbars_setting_8);
   
   rebuild_settings();
   
   $insertarray = array(
       "title" => "simplenewsbars_1",
       "template" => "<style>
.alert {
   background: #FFF6BF;
   border-top: 1px solid #FFD324;
   border-left: 1px solid #FFD324;
   border-right: 1px solid #FFD324;
   border-bottom: 1px solid #FFD324;
   text-align: center;
   margin: 10px auto;
   padding: 5px 20px;
   border-radius: 6px;
   color: #404040;
}
</style><div id=\"alert\"><p class=\"alert\">{\$mybb->settings[\'simplenewsbars_input_1\']}</p></div>",
       "sid" => -1,
       "dateline" => TIME_NOW
   );
   
   $db->insert_query("templates", $insertarray);
   
   $insertarray = array(
       "title" => "simplenewsbars_2",
       "template" => "<style>
.notice1 {
   background: #D6ECA6;
   border-top: 1px solid #8DC93E;
   border-left: 1px solid #8DC93E;
   border-right: 1px solid #8DC93E;
   border-bottom: 1px solid #8DC93E;
   text-align: center;
   margin: 10px auto;
   padding: 5px 20px;
   border-radius: 6px;
   color: #404040;
}
</style><div id=\"notice1\"><p class=\"notice1\">{\$mybb->settings[\'simplenewsbars_input_2\']}</p></div>",
       "sid" => -1,
       "dateline" => TIME_NOW
   );
   
   $db->insert_query("templates", $insertarray);
   
   $insertarray = array(
       "title" => "simplenewsbars_3",
       "template" => "<style>
.notice2 {
   background: #ADCBE7;
   border-top: 1px solid #0F5C8E;
   border-left: 1px solid #0F5C8E;
   border-right: 1px solid #0F5C8E;
   border-bottom: 1px solid #0F5C8E;
   text-align: center;
   margin: 10px auto;
   padding: 5px 20px;
   border-radius: 6px;
   color: #404040;
}
</style><div id=\"notice2\"><p class=\"notice2\">{\$mybb->settings[\'simplenewsbars_input_3\']}</p></div>",
       "sid" => -1,
       "dateline" => TIME_NOW
   );
   
   $db->insert_query("templates", $insertarray);
   
   include MYBB_ROOT . "/inc/adminfunctions_templates.php";
   
   // activate on index
   
   find_replace_templatesets("index", "#" . preg_quote("{\$header}") . "#i", "{\$header}\r\n
       {\$simplenewsbars_1}
       {\$simplenewsbars_2}
       {\$simplenewsbars_3}");
   
   // activate on portal
   
//    find_replace_templatesets("portal", "#" . preg_quote("{\$header}") . "#i", "{\$header}\r\n
//        {\$simplenewsbars_1}
//        {\$simplenewsbars_2}
//        {\$simplenewsbars_3}");
}

function simplenewsbars_deactivate()
{
   global $db;
   
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_enable_index')");
//    $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_enable_portal')");
   
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_enable_input_1')");
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_enable_input_2')");
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_enable_input_3')");
   
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_input_1')");
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_input_2')");
   $db->query("DELETE FROM " . TABLE_PREFIX . "settings WHERE name IN ('simplenewsbars_input_3')");
   
   $db->query("DELETE FROM " . TABLE_PREFIX . "settinggroups WHERE name='simplenewsbars'");
   
   $db->delete_query("templates", "title = 'simplenewsbars_1'");
   $db->delete_query("templates", "title = 'simplenewsbars_2'");
   $db->delete_query("templates", "title = 'simplenewsbars_3'");
   
   rebuild_settings();
   
   include MYBB_ROOT . "/inc/adminfunctions_templates.php";
   
   // deactivate on index
   
   find_replace_templatesets("index", "#" . preg_quote("\r\n
       {\$simplenewsbars_1}
       {\$simplenewsbars_2}
       {\$simplenewsbars_3}") . "#i", "", 0);
   
   // deactivate on portal
   
//    find_replace_templatesets("portal", "#" . preg_quote("\r\n
//        {\$simplenewsbars_1}
//        {\$simplenewsbars_2}
//        {\$simplenewsbars_3}") . "#i", "", 0);
}

//function simplenewsbars_portal_start()
//{
//    global $db, $mybb, $templates, $simplenewsbars_1, $simplenewsbars_2, $simplenewsbars_3;
//    
//    if ($mybb->settings['simplenewsbars_enable_portal'] == 1) {
//        
//        if ($mybb->settings['simplenewsbars_enable_input_1'] == 1) {
//            eval("\$simplenewsbars_1 = \"" . $templates->get("simplenewsbars_1") . "\";");
//        }
//        
//        if ($mybb->settings['simplenewsbars_enable_input_2'] == 1) {
//            eval("\$simplenewsbars_2 = \"" . $templates->get("simplenewsbars_2") . "\";");
//        }
//        
//        if ($mybb->settings['simplenewsbars_enable_input_3'] == 1) {
//            eval("\$simplenewsbars_3 = \"" . $templates->get("simplenewsbars_3") . "\";");
//        }
//    }
//}

function simplenewsbars_index_start()
{
   global $db, $mybb, $templates, $simplenewsbars_1, $simplenewsbars_2, $simplenewsbars_3;
   
   if ($mybb->settings['simplenewsbars_enable_index'] == 1) {
       
       if ($mybb->settings['simplenewsbars_enable_input_1'] == 1) {
           
           eval("\$simplenewsbars_1 = \"" . $templates->get("simplenewsbars_1") . "\";");
       }
       
       if ($mybb->settings['simplenewsbars_enable_input_2'] == 1) {
           eval("\$simplenewsbars_2 = \"" . $templates->get("simplenewsbars_2") . "\";");
       }
       
       if ($mybb->settings['simplenewsbars_enable_input_3'] == 1) {
           eval("\$simplenewsbars_3 = \"" . $templates->get("simplenewsbars_3") . "\";");
       }
   }
}
?>

*same can be done on the localized version of the file if that gives you a better working example to get the general idea
MyBB Themes Team - Making more professional themes by the hour. (Retired)
Reply
#52
It works..

Thank you for sharing !
Reply
#53
@gina f, @skr1107, @rajkapoor

No worries. Glad it may of been of some help.
MyBB Themes Team - Making more professional themes by the hour. (Retired)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)