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');
  • you're enquing it on the wrong hook – Tom J Nowell Aug 29 '17 at 2:01
0

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' );
  • I tried that hook earlier and then again since you suggested it, but I still can't get it to work. – cboy Aug 29 '17 at 1:13
  • I just edited my answer to change how wp_enqueue_style() is called. – Cedon Aug 29 '17 at 1:18
0

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

  • So are you saying to add it like this? It does not work. function wpdocs_theme_name_scripts() { wp_enqueue_style( 'style-name', get_stylesheet_uri() ); wp_enqueue_script( 'script-name', get_template_directory_uri() . 'css/styles.css', array(), '1.0.0', true ); } add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' ); – cboy Aug 29 '17 at 11:34
  • That function isn't a copy-paste fix, it's the example out of the official docs to demonstrate normal enquing. You need to use the wp_enqueue_scripts action, not the admin_print_styles action to show on the frontend – Tom J Nowell Aug 29 '17 at 12:09
0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.