php Wordpress 通过插件加载样式表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21759642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress load a stylesheet through plugin
提问by fightstarr20
I have written a WordPress plugin and want to make it include some css stylesheets, I have tried to use the process I usually use in a themes functions.php file...
我写了一个 WordPress 插件,并想让它包含一些 css 样式表,我尝试使用我通常在主题 functions.php 文件中使用的过程...
add_action('wp_enqueue_script','register_my_scripts');
function register_my_scripts(){
$dir = plugin_dir_path( __FILE__ );
wp_enqueue_style( $dir . 'css/style1.css');
wp_enqueue_style( $dir . 'css/style2.css');
}
But this is not loading anything, what am I doing wrong?
但这并没有加载任何东西,我做错了什么?
回答by Nathan Dawson
The hook you need to use is wp_enqueue_scripts, you were missing the 's'.
您需要使用的钩子是 wp_enqueue_scripts,您错过了“s”。
You're getting the directory path when what you need is the directory URL.
当您需要的是目录 URL 时,您将获得目录路径。
wp_enqueue_style's first parameter is a handle and not the URL.
wp_enqueue_style 的第一个参数是句柄而不是 URL。
function wpse_load_plugin_css() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_style( 'style1', $plugin_url . 'css/style1.css' );
wp_enqueue_style( 'style2', $plugin_url . 'css/style2.css' );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_plugin_css' );
回答by Rahil Wazir
You are using plugin_dir_pathwhich outputs filesystem directory path. Instead you need URL.
您正在使用plugin_dir_path输出文件系统目录路径。相反,您需要 URL。
Also the first parameter of wp_enqueue_styleis $handlername.
wp_enqueue_style的第一个参数也是$handler名称。
Use plugins_url
wp_enqueue_style( 'style1', plugins_url( 'css/style1.css' , __FILE__ ) );
Full code:
完整代码:
add_action('wp_enqueue_scripts','register_my_scripts');
function register_my_scripts(){
wp_enqueue_style( 'style1', plugins_url( 'css/style1.css' , __FILE__ ) );
wp_enqueue_style( 'style2', plugins_url( 'css/style2.css' , __FILE__ ) );
}
回答by Obmerk Kronen
try :
尝试 :
wp_enqueue_style('custom-style', plugins_url( '/css/my-style.css', __FILE__ ), array(),'all');where plugins_urlis relative to plugin base without slash.
wp_enqueue_style('custom-style', plugins_url( '/css/my-style.css', __FILE__ ), array(),'all');whereplugins_url相对于没有斜线的插件库。
回答by Super Model
Load styles from wp plugin folder using plugin url
使用插件 url 从 wp 插件文件夹加载样式
function add_plugin_stylesheet()
{
wp_enqueue_style( 'style1', plugins_url( '/css/styleFileName1.css', __FILE__ ) );
wp_enqueue_style( 'style2', plugins_url( '/css/styleFileName2.css', __FILE__ ) );
}
add_action('admin_print_styles', 'add_plugin_stylesheet');

