skip to Main Content

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?

2

Answers


  1. 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' ); 
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search