简体   繁体   English

CSS bug wordpress插件

[英]CSS bug wordpress plugin

hi My problem is that the call of CSS in my plugin acts on all wordpress. 嗨,我的问题是我插件中的CSS调用作用于所有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. 它修改了其他扩展名的CSS,页面登录等。这是我封锁的几个星期,如果可以的话,我会很高兴。

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): 您需要像这样使用admin_enqueue_scripts (这是示例的副本):

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. 您可以在函数内执行var_dump($hook)来获取特定页面的钩子字符串,并用其中的'toplevel_page_mypluginname'替换。

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. 我看到您找到了一个解决方案,但是您正在使用wp_enqueue_css作为操作,而且很好,它将不必要地在前端执行。 The best practice is to use admin_enqueue_scripts . 最佳实践是使用admin_enqueue_scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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