Kenn Nielsen Kenn Nielsen - 7 months ago 17
PHP Question

Using Bootstrap within Wordpress admin - a hack by Rush Frisby

Building my first plugin to Wordpress I came across Rush Frisby's Bootstrap hack, so that you can work with Bootstrap within the admin panel, without conflicting with the Wordpress admin core styles. You'll find it here: https://rushfrisby.com/using-bootstrap-in-wordpress-admin-panel/

I have implemented it in my plugin the way he explained.
You can review my code online at https://github.com/kennnielsen/wordpress_dev

There is just one problem with this part:

.bootstrap-wrapper {
@import (less) url('bootstrap.min.css');
}


The error below will disappear if I remove the (less) from above code, but then the hack is not working as expected - the bootstrap is not loaded within the bootstrap-wrapper.

First of all, I have never really worked with LESS, but I know the basic idea of LESS and how it can ease the work with your CSS.

Nevertheless, when running my plugin, and going to Settings - Custom Login I see the following error:

Wordpress error

I have no idea what to do. I have searched the web for answers, but I can't really find a solution nor a fix for this.

Does anyone have an idea on how to solve this?
- and a brief explanation about .map files?

Thank you all in advance!

For you guys that don't want to go to Github, see below code used for the admin page.

<?php

// Meaning of abbreviations:
// clsc = Custom login shortcode

// Runs when plugin is activated
register_activation_hook( PLUGIN_MAIN_FILE, 'clsc_install');
// Create new database fields
function clsc_install() {
$clsc_options = array(

'Login_link' => 'log-in/',
'Login_string' => 'Log in',
'Login_class' => '', // Default is empty to inherit theme styles
'Logout_link' => wp_logout_url( home_url()),
'Logout_string' => 'Log out',
'Logout_class' => '', // Default is empty to inherit theme styles
'Account_link' => 'my-account/',
'Account_string' => 'My Account',
'Account_class' => '' // Default is empty to inherit theme styles

);
add_option('clsc_options_array', $clsc_options, '', 'yes');
}

// Register settings for wordpress to handle all values
function admin_init_register_setting()
{
register_setting('wp_plugin_template-group', 'clsc_options_array');

}
add_action('admin_init','admin_init_register_setting');


// Create admin option page
function add_clsc_option_page() {
add_options_page(
'Custom Login', // The text to be displayed in the title tag
'Custom Login', // The text to be used for the menu
'administrator', // The capability required to display this menu
'custom-login-shortcodes', // The unique slug name to refer to this menu
'clsc_html_page'); // The function to output the page content
}
/* Call the html code */
add_action('admin_menu', 'add_clsc_option_page');

// Enqueue admin styles and scripts
function clsc_enqueue_scripts() {
global $wpdb;
$screen = get_current_screen();

if ( $screen->id != 'settings_page_custom-login-shortcodes' ) {
return; // exit if incorrect screen id
}

wp_enqueue_style( 'custom-shortcodes-styles', plugins_url( 'admin/css/admin_styles.css', dirname(__FILE__) ) );
wp_enqueue_style( 'bootstrap', plugins_url('admin/css/bootstrap.css', dirname(__FILE__) ) );
wp_enqueue_script('admin_js_bootstrap_hack', plugins_url('admin/scripts/bootstrap-hack.js', dirname(__FILE__) ) );
wp_enqueue_script('jquery', plugins_url('admin/scripts/jquery.min.js', dirname(__FILE__) ) );

}
add_action('admin_enqueue_scripts', 'clsc_enqueue_scripts' );

function clsc_html_page()
{
if(!current_user_can('manage_options'))
{
wp_die( __('You do not have sufficient permissions to access this page.','clsc') );
}

?>

<script type="text/javascript">

var default_logout = <?php echo json_encode( wp_logout_url( home_url()) ); ?>;

$(document).ready(function(){
$("#logout-default").click(function(){
$("#logout-field").val(default_logout);
});
});
</script>

<div class="wrap">

<form method="post" action="options.php">
<?php

$options = get_option('clsc_options_array');
settings_fields('wp_plugin_template-group');
do_settings_fields('wp_plugin_template-group');

?>
<div class="bootstrap-wrapper">
<div class="row">
<div class="col-md-12">
<h1><?php _e('Custom Login Shortcode','clsc'); ?></h1>
<p><?php _e('To use for shortcode:','clsc'); ?><br/><span class="shortcode-preview">[custom_login]</span></p>
</div>
</div>
<div class="row" id="login-content">
<div class="col-md-4">
<h5><?php _e('Log in link:','clsc'); ?></h5>
<input name="clsc_options_array[Login_link]" placeholder="<?php _e('Example: log-in/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in string:','clsc'); ?></h5>
<input name="clsc_options_array[Login_string]" placeholder="<?php _e('Example: Log in', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log in class:','clsc'); ?></h5>
<input name="clsc_options_array[Login_class]" placeholder="<?php _e('Example: login_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Login_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="logout-content">
<div class="col-md-4">
<h5><?php _e('Log out link:','clsc'); ?></h5>
<input id="logout-field" name="clsc_options_array[Logout_link]" placeholder="<?php _e('Example: log-out/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_link']; ?>" />
<input class="btn btn-default btn-xs" type="button" name="logout-default" id="logout-default" value="<?php _e('Use default logout link','clsc') ?>"/>
</div>
<div class="col-md-4">
<h5><?php _e('Log out string:','clsc'); ?></h5>
<input name="clsc_options_array[Logout_string]" placeholder="<?php _e('Example: Log out', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Log out class:','clsc'); ?></h5>
<input name="clsc_options_array[Logout_class]" placeholder="<?php _e('Example: logout_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Logout_class']; ?>" />
</div>
</div>
<div class="row top-buffer" id="account-content">
<div class="col-md-4">
<h5><?php _e('Account link:','clsc'); ?></h5>
<input name="clsc_options_array[Account_link]" placeholder="<?php _e('Example: my-account/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_link']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account string:','clsc'); ?></h5>
<input name="clsc_options_array[Account_string]" placeholder="<?php _e('Example: My Account', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_string']; ?>" />
</div>
<div class="col-md-4">
<h5><?php _e('Account class:','clsc'); ?></h5>
<input name="clsc_options_array[Account_class]" placeholder="<?php _e('Example: account_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['Account_class']; ?>" />
</div>
</div>
</div>

<?php submit_button( __('Save Changes', 'clsc') ); ?>

</form>

</div>

<?php
}
?>

Answer

Suggestion : It's not a good idea to use minified CSS for developmental purposes also it's hard to debug when there are errors in your CSS Code & more likely sometimes gives parse errors due the fact that the whole code is converted into one line which seems to be not ending at all...!

Problem : Inability of LESS to compile a minified version of Bootstrap is a known problem: http://github.com/less/less.js/issues/2207.

The parser may fail at certain non-CSS-conformant browser-specific hacks (the error message may vary depending on the Bootstrap and/or Less versions). The usual workaround is just to compile non-minified version (note that compiling a minified version does not make too much sense since the result of compilation is not minified CSS anyway)

@Credit Goes To @seven-phases-max For The Reference URL

Solution : Well,The problem seems to be with your Minified bootstrap.min.css file,Try to use the unminified version bootstrap.css and you will have no problem.!