简体   繁体   中英

CSS bug wordpress plugin

hi My problem is that the call of CSS in my plugin acts on all wordpress. It modifies the CSS of the other extensions, the page login etc. Here are several weeks that I block, if you could help me it would be with pleasure.

function carla_stylesheet() {

  wp_enqueue_style( 'carla-style', plugins_url('css/bootstrap.min.css', __FILE__) );
  wp_enqueue_style( 'carla-style2', plugins_url('css/stylesheet.css', __FILE__) );
}       


carla_stylesheet();


add_action( 'wp_enqueue_css', 'carla_stylesheet' );

I just found the solution foolishly ... I have set conditions:

function carla_stylesheet() {
    // Respects SSL, Style.css is relative to the current file
    if($_GET['page'] == "Carla"){
        wp_enqueue_style( 'carla-style', plugins_url('css/bootstrap.min.css', __FILE__) );
        wp_enqueue_style( 'carla-style2', plugins_url('css/stylesheet.css', __FILE__) );
    }
}   

You need to use admin_enqueue_scripts like this (this is a copy of the example):

function load_custom_wp_admin_style($hook) {
        // You can do a "var_dump($hook)" here to grab the hook string of your specific page
        if($hook != 'toplevel_page_mypluginname') {
                return;
        }
        wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );

You can do a var_dump($hook) inside the function to grab the hook string of your specific page and replace the 'toplevel_page_mypluginname' that is there with yours.

I see you found a solution but you are using wp_enqueue_css as the action and well, that will execute in the front-end too unnecessarily. The best practice is to use admin_enqueue_scripts .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM