3

I need to use Bootstrap CSS for better UI in wp-admin but if I enqueue the bootstrap.css, it's affecting the admin default UI by changing background colors, etc.

How can I use bootstrap.css inside wp-admin?

2
  • 1
    That's how CSS works: if selector matches and it's more specific, it will overwrite the other rules. Commented Jun 17, 2016 at 14:15
  • 1
    This is why I dislike Bootstrap - it's not semantic and causes more problems than it's worth. Maybe you can explain why you need to use Bootstrap and we might be able to suggest other solutions? Be careful of the XY problem. Commented Jun 17, 2016 at 21:21

4 Answers 4

1

You can write the content of the bootstrap.css file in a scss file and manipulate it.

Put all the Bootstrap classes in a wrapper container (for example with the .bootstrap-wrapper class).

:root { /* write bootstrap variables here... */ } .bootstrap-wrapper { /* Write the content of bootstrap.css here */ } 

Be careful when changing Bootstrap css.

After compiling the scss file and inserting it into the admin area, you can use Bootstrap as follows:

<div class="bootstrap-wrapper"> <!-- Use Bootstrap here... --> </div> 
1

I upvoted the answer from @امیرحسین منصورکیایی because it works, at least partially.

Here is the approach I followed to reduce the "global" impact from bootstrap and to use Bootstrap's grid system and accordion for a WordPress Admin Widget.

  • Created a new folder in my plugin project and performed "npm init" on it.
  • Installed bootstrap source and sass via "npm install bootstrap sass".
  • Edited the file _reboot.scss in node_modules/bootstrap/scss and basically commented out all rules but left empty rules for h1 - h6, small and mark.
  • Created a new styles.scss file in the root of the folder (see below).
.my-custom-widget { @import "./node_modules/bootstrap/scss/bootstrap"; } 
  • Build and minified the custom version of Bootstrap via "npx sass styles.scss ./../styles.min.css --style=compressed".
  • Enqueued the custom version of Bootstrap in the callback that is hooked up with "wp_dashboard_setup".
  • Wrapped the widget in a div with class "my-custom-widget".
0

As @N00b said, if a selector matches between bootstrap and wordpress, and is more specific, then it will overwrite and you'll see the changes.

1
  • 1
    ...so your best bet is to probably create your own classes (with specific prefixed class names) and support them with your own CSS, copied (and modified) from bootstrap. Commented Jul 6, 2016 at 20:02
0

if you add pages via add_menu_page or add_submenu_page, and only need your css there, those function return a hook, which you can use with the load-$your_hook action. And with said hook, you can load your css only on this specific page. for example:

add_action( 'admin_menu', 'register_page'); function register_page() { $my_hook = add_menu_page( 'tab title', 'page title', 'manage_options', 'my-page-slug', 'callback_function', 'dashicons-cloud', 1 ); add_action( 'load-'.$my_hook, load_scripts ); } function load_scripts () { add_action( 'admin_enqueue_scripts', 'enqueue_bootstrap' ); } function enqueue_bootstrap() { $path = plugins_url( 'my_plugin/inc/css/bootstrap.min.css' ); //use your path of course $dependencies = array(); //add any depencdencies in array $version = false; //or use a version int or string wp_enqueue_style( 'myplugin-bootstrap', $path, $dependencies, $version ); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.