最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin enqueue style on all pages

programmeradmin2浏览0评论

I need to enqueue a style sheet in my plugin to work on the front end of Wordpress. Here is what I have. Will someone please tell me what I am doing wrong?

function add_my_stylesheet1()  {
    wp_enqueue_style( 'myStyles', plugins_url( 'css/styles.css', __FILE__ ) );

}

add_action('admin_print_styles', 'add_my_stylesheet1');

I need to enqueue a style sheet in my plugin to work on the front end of Wordpress. Here is what I have. Will someone please tell me what I am doing wrong?

function add_my_stylesheet1()  {
    wp_enqueue_style( 'myStyles', plugins_url( 'css/styles.css', __FILE__ ) );

}

add_action('admin_print_styles', 'add_my_stylesheet1');
Share Improve this question edited Aug 29, 2017 at 1:28 Cedon 1,6592 gold badges13 silver badges26 bronze badges asked Aug 29, 2017 at 0:53 cboycboy 371 silver badge5 bronze badges 1
  • you're enquing it on the wrong hook – Tom J Nowell Commented Aug 29, 2017 at 2:01
Add a comment  | 

3 Answers 3

Reset to default 1

You're enquing on the admin_enqueue_scripts hook, which is why it's only showing up on admin pages

If we look at an example from the official documentation:

/**
 * Proper way to enqueue scripts and styles
 */
function wpdocs_theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );

we see the wp_enqueue_scripts hook being used instead. Use that hook instead of admin_print_styles to print on the frontend

You need to use the admin_enqueue_scripts hook. So your function would be like this...

function add_my_stylesheet1() {
    wp_enqueue_style( 'myStyles', plugins_url( __FILE__ ) . ' css/styles.css' );
}
add_action( 'admin_enqueue_scripts', 'add_my_stylesheet1' );

Ok, Here is my final solution:

function my_test() {
    wp_enqueue_style( 'shortcode', plugin_dir_url( __FILE__ ) . 'css/shortcodes.css' );
}

add_action( 'wp_enqueue_scripts', 'my_test' );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论