Home > OS >  I just want to run the wp_enqueue_style function in my own plugin
I just want to run the wp_enqueue_style function in my own plugin

Time:08-03

I'm new to wordpress plugin development so I'm a beginner. I am developing a plugin. I want to use bootstrap 5 for plugin.

I have added a code like this for bootstrap 5 CDN.

public function enqueue_styles() {

    /**
     * This function is provided for demonstration purposes only.
     *
     * An instance of this class should be passed to the run() function
     * defined in Info_Master_Bk_Loader as all of the hooks are defined
     * in that particular class.
     *
     * The Info_Master_Bk_Loader will then create the relationship
     * between the defined hooks and the functions defined in this
     * class.
     */

    wp_enqueue_style($this->plugin_name.'-bst-cdn', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css', array(), $this->version);

}

But when I added this, it affected the entire wordpress admin panel. How do I make it only affect my plugin?

CodePudding user response:

Use a hook, for admin side you can use: "admin_enqueue_scripts"

public function enqueue_styles() {
        wp_register_style( 'name_your_style_something', plugin_dir_url( __FILE__ ) . 'rest of the path', false, '1.0.0' );
        wp_enqueue_style( 'name_your_style_something' );
}

add_action( 'admin_enqueue_scripts', 'enqueue_styles' ); 

CodePudding user response:

Please follow the correct enqueue procedure. You can use it this way:

        <?php
             function your_script_enqueue() {
                wp_enqueue_script( 'bootstrap_js', 'https://stackpath.bootstrapcdn.com/bootstrap/5/js/bootstrap.min.js', array('jquery'), NULL, true );
                wp_enqueue_style( 'bootstrap_css', 'https://stackpath.bootstrapcdn.com/bootstrap/5/css/bootstrap.min.css', false, NULL, 'all' );
              
             }
             
             add_action( 'wp_enqueue_scripts', 'your_script_enqueue' );
        ?>

Thank you

  • Related