简体   繁体   中英

Wordpress Plugin wont load css

My add_action() isn't working and i have no clue why.

This is my php

function mapstyle() {
    wp_register_style('mapstyle', plugins_url('assets/css/mapstyle.css' , __FILE__ ));
    wp_enqueue_style('mapstyle');
}

add_action( 'admin_init','mapstyle');

My css is in 'plugin_folder_name/assets/css/mapstyle.css'

Try changing your code to the following. Also ensure that you do a hard refresh with CTRL + F5 or CTRL + SHIFT + R to clear cache, sometimes changes will not show up unless cache is cleared.

function mapstyle() {
     //last parameters set to true loads css in the footer instead of header
     wp_enqueue_style('mapstyle', plugins_url('assets/css/mapstyle.css' , __FILE__ ), array(), false, true);

}
add_action( 'wp_enqueue_scripts','mapstyle');

Here is link to documentation of wp_enqueue_style.

Edit: My bad, half way through writing answer forgot you wanted to enqueue css and not js :D

Depends on where you want to enqueue the styles--admin vs. front-end:

Admin:

add_action('admin_init', 'mapstyle');

Front-end:

add_action('wp_enqueue_scripts', 'mapstyle');

Reference: https://developer.wordpress.org/reference/functions/wp_enqueue_style/

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