Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building a WordPress Plugin Settings Page Adding CSS to a Plugin Settings Page

Lan Mi
Lan Mi
2,570 Points

Anyone can cast some light on why this code is wrong? Thank you

<?php

// Handle: my_plugin_css // Plugin folder: my-plugin // CSS file: my-plugin.css function my_plugin_styles { wp_enqueue_style('my_plugin_styles',plugins_url('my-plugin/my-plugin.css')); } add_action('admin_head','my_plugin_styles');

?>

6 Answers

Could you tell me what error your getting?

Lan Mi
Lan Mi
2,570 Points

Curtis, Thanks for your help on this. No specific error message.It's just that the my-plugin.css file won't take any effect and I can't pass the code challenge.

Lan Mi, no problem I am going to watch the video and see if I can narrow it down. The enqueue string looks correct from a PHP standpoint.

Looks like your missing an arguments () for your function

// function function name (arguments) or () for no arguments
function my_plugin_styles(){ 
    wp_enqueue_style('my_plugin_styles', plugins_url('my-plugin/my-plugin.css')); 
} 
    add_action('admin_head','my_plugin_styles');
Lan Mi
Lan Mi
2,570 Points

oops, right Curtis, but this time there is an error message saying that "Bummer! The handle "my_plugin_css" should be passed as the first argument to wp_enqueue_style." What does it mean? Thank you

I looked over the function at http://codex.wordpress.org/Function_Reference/wp_enqueue_style

I am thinking that WP may have changed the methodology behind it. You could try

wp_enqueue_style(plugins_url('my-plugin/my-plugin.css'), 'my_plugin_styles');

See if that does it if not Id try downloading Zac's file and see if it works

Lan Mi
Lan Mi
2,570 Points

Thank you Curtis for the message. I tried using the "handle" for this code and it worked (see below). Do you know why the handle impacts the code? <?php

// Handle: my_plugin_css // Plugin folder: my-plugin // CSS file: my-plugin.css function my_plugin_styles() { wp_enqueue_style('my_plugin_css',plugins_url('my-plugin/my-plugin.css')); }

add_action('admin_head','my_plugin_styles');

?>

However when I followed the Zac's wptreehouse badges video and I just could not get the code working. Not sure if it's the "handle" thing? I also tried //wp_enqueue_style(plugins_url('my-plugin/my-plugin.css'), 'my_plugin_styles'); but it still does not work. Any idea? Thank you

<? function wptreehouse_badges_styles() { wp_enqueue_style('wptreehouse_badges_styles',plugins_url('wptreehouse_badges/wptreehouse_badges.css'));

}

add_action('admin_head','wptreehouse_badges_styles');

?>

Its a guess but an educated one, I believe its because with the new WP version you "register" the style sheet handle with another function and refer to it by the handle the rest of the time.

Lan Mi
Lan Mi
2,570 Points

ah, interesting. Thank you Curtis for your generous help.