我已经为 wordpress 完成了插件,但我想将 twitter bootstrap css 样式添加到该插件的设置页面中。我的想法是添加这段代码:
function my_plugin_admin_styles() {
wp_register_style( 'bootstrap-style', plugins_url('style/bootstrap.min.css', __FILE__) );
wp_enqueue_style( 'bootstrap-style' );
}
if (isset($_GET['page']) && $_GET['page'] == 'my_plugin') {
add_action( 'admin_print_styles', array($instance, 'my_plugin_admin_styles'));
}
但它会影响一切,它也会更改左侧的菜单。可以只为我的设置页面应用 css 吗?
最佳答案
您需要创建一个自定义的 CSS 文件,该文件仅针对您在插件设置页面部分中的内容。例如,您可以使用如下包装器构建插件设置页面:
<section id="mypluginidentifier-settings">
<!-- all the plugin settings html here -->
</section>
然后,在 CSS 中,只需在所有内容前加上 #mypluginidentifier-settings
前缀。
其中 mypluginidentifier = 您的插件独有的东西。
即
#mypluginidentifier-settings .button {/*button styles*/}
否则你将面临与 wordpress CSS 冲突的风险。
不过,如果你想包含所有 Bootstrap ,这确实是一个很大的麻烦。我还绝对建议您在插件中本地包含 CSS,而不是调用文件的托管版本(无论如何,您都必须这样做)。
关于css - 为插件设置页面添加样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805423/