0

i want to add my css file bootstrap.min.css on top of all css file. I used below code

wp_enqueue_style( 'bootstrap_css', 'bootstrap.min.css' );

but it add css below theme css file .

Issue is that i do not want to modify theme file and want to do from plugin only. I there way i can add my css after tag using some wp_head or after_title hook.

Please help me with same

2 Answers 2

0
/**
 * Enqueue stylesheet before others
 */    
class EnqueueStylesheetEarly {

    function __construct() {
        add_action( 'wp_enqueue_scripts', [ $this, 'load_before_other_stylesheets' ], 5 );
    }

    /**
     * Register and enqueue stylesheet
     */
    function load_before_other_stylesheets() {
        wp_register_style(
            'bootstrap-css',
            plugin_dir_url( __FILE__ ) . 'bootstrap.min.css',
            [],
            '3.3.7'
        );

        wp_enqueue_style( 'bootstrap-css' );
    }

}

Then you can load this with your other frontend classes in your 'plugin.php' Like so...

if ( ! is_admin() ) {
    require 'class-enqueue-stylesheet-early.php';
}

At a 5 priority, this should solve your problem. The only thing that usually loads before this is WordPress SEO JSON-LD scripts and meta.

0

You can dequeue your theme's style and then enqueue both of your styles in your plugin. Take a look at this:

function wp_dequeue_script() {
   wp_dequeue_style( 'theme-scss' );
   wp_enqueue_style( 'bootstrap_css', 'bootstrap.min.css' );
   wp_enqueue_style( 'theme-css', 'YOUR THEME CSS URL' );
}
add_action( 'wp_print_scripts', 'wp_dequeue_script', 100 );

You might want to change the hook depending on the hook your theme is using, or increase it's priority.

1
  • IMHO unless you specify an explicit dependency you can not trust the way the CSS files are output to be in any specific order. They might be in enquing order today and something else in the next wp version, or depend on PHP implementation of arrays Commented Apr 9, 2017 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.