MyBB Community Forums

Full Version: How to add CSS Files with plugins + how to use settings?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am pretty stumped on how can I add css files with a code that changes according to the settings in the AdminCP.

First Doubt: How do you add CSS files to the forums first of all,
Second Dount: How do you make Admin Settings, the MyBB Wiki is not working for me

Waiting for support,
Aniruddh
To add CSS files, use the following code in headerinclude template;
<link rel="stylesheet" type="text/css" href="PATH TO THE CSS FILE" />

To add Admin Settings, use the following code as a sample;
$TEMPLATE_group = array(
		"gid"			=> "NULL",
		"name"			=> "TEMPLATE",
		"title" 		=> "TEMPLATE",
		"description"	=> "Settings for the plugin.",
		"disporder"		=> "1",
		"isdefault"		=> "no",
	);
	$db->insert_query("settinggroups", $TEMPLATE_group);
	$gid = $db->insert_id();
	
	$TEMPLATE_setting_1 = array(
		"sid"			=> "NULL",
		"name"			=> "TEMPLATE_power",
		"title"			=> "Power",
		"description"	=> "Select Yes if you really wants this plugin to run.",
		"optionscode"	=> "yesno",
		"value"			=> "1",
		"disporder"		=> "1",
		"gid"			=> intval($gid),
	);

	$db->insert_query("settings", $TEMPLATE_setting_1);
(2012-06-12, 03:52 PM)Yaldaram Wrote: [ -> ]To add CSS files, use the following code in headerinclude template;
<link rel="stylesheet" type="text/css" href="PATH TO THE CSS FILE" />
I know that, but when I want to add CSS files directly from the PHP file without having the user to upload a new CSS File?
Plus when the user changes the theme or something like that, this might now work!
Try something along these lines. Replace "PLUGINNAME" with your plugin's name of course:

global $db;

$stylesheet = ".someClass{
    background:#000000;
}";
    
$insert_array = array(
    'name' => 'PLUGINNAME.css',
    'tid' => '1',
    'stylesheet' => $db->escape_string($stylesheet),
    'cachefile' => 'PLUGINNAME.css',
    'lastmodified' => TIME_NOW
);

require_once MYBB_ADMIN_DIR."inc/functions_themes.php";
    
$sid = (int) $db->insert_query("themestylesheets", $insert_array);
    
if(!cache_stylesheet($theme['tid'], 'PLUGINNAME.css', $stylesheet))
{
        $db->update_query("themestylesheets", array('cachefile' => "css.php?stylesheet={$sid}"), "sid='{$sid}'", 1);
}
    
$query = $db->simple_select("themes", "tid");
while($theme = $db->fetch_array($query))
{
    update_theme_stylesheet_list($theme['tid']);
}

Not guaranteeing it will work and when users install a new theme they'll have to add the stylesheet manually anyway.
I do almost verbatim what euantor posted. It is possible to add a stylesheet to the master template so that all themes (current and new) will have it inherited. Editing the stylesheet in ACP will then break inheritance as needed. This way when a new theme is added, the default stylesheet that your plugin added to the Master Template will be available.

Example code:

function myplugin_install()
{
    global $db;
    require_once(MYBB_ROOT."admin/inc/functions_themes.php");

    // Add stylesheet to the master template so it becomes inherited.
    $stylesheet = @file_get_contents(MYBB_ROOT.'inc/plugins/myplugin/myplugin.css');
    $myplugin_stylesheet = array(
        'sid' => NULL,
        'name' => 'myplugin.css',
        'tid' => '1',
        'stylesheet' => $db->escape_string($stylesheet),
        'cachefile' => 'myplugin.css',
        'lastmodified' => TIME_NOW,
    );
    $db->insert_query('themestylesheets', $stylesheet);
    cache_stylesheet(1, "myplugin.css", $stylesheet);
    update_theme_stylesheet_list("1");
}

On uninstall, need to check for the existence of the stylesheets and remove them from the cache directory.

function myplugin_uninstall()
{
    global $db;
    require_once(MYBB_ROOT."admin/inc/functions_themes.php");

    // Remove myplugin.css from the theme cache directories if it exists
    $query = $db->simple_select("themes", "tid");
    while($tid = $db->fetch_field($query, "tid"))
    {
        $css_file = MYBB_ROOT."cache/themes/theme{$tid}/myplugin.css";
        if(file_exists($css_file))
            unlink($css_file);
    }

    update_theme_stylesheet_list("1");
}
You can use PluginLibrary to Add new stylesheets to MyBB with plugins

Example code:
Global (top of your plugin):
if (!defined("PLUGINLIBRARY"))
{
    define("PLUGINLIBRARY", MYBB_ROOT."inc/plugins/pluginlibrary.php");
}

Activate:
if (!file_exists(PLUGINLIBRARY))
{
    flash_message($lang->myalerts_pluginlibrary_missing, "error");
    admin_redirect("index.php?module=config-plugins");
}

$PL or require_once PLUGINLIBRARY;

if ((int) $PL->version < 9)
{
    flash_message('This plugin requires PluginLibrary 9 or newer', 'error');
    admin_redirect('index.php?module=config-plugins');
}

$stylesheet = 'body { background: black; }
/* Some more CSS... */';

$PL->stylesheet('alerts.css', $stylesheet);

Deactivate:
if (!file_exists(PLUGINLIBRARY))
{
    flash_message($lang->myalerts_pluginlibrary_missing, "error");
    admin_redirect("index.php?module=config-plugins");
}

$PL or require_once PLUGINLIBRARY;

$PL->stylesheet_deactivate('alerts.css');

Uninstall:
if (!file_exists(PLUGINLIBRARY))
{
    flash_message($lang->myalerts_pluginlibrary_missing, "error");
    admin_redirect("index.php?module=config-plugins");
}

$PL or require_once PLUGINLIBRARY;

$PL->stylesheet_delete('alerts.css');
Obviously you can rename "alerts.css" to whatever you wish your stylesheet to be called.