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

Zander Curtis
Zander Curtis
10,634 Points

What's wrong with this code?

Can't seem to solve this one. I'm guessing I'm just not understanding the question and doing too much.

plugin.php
<?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', 'wp_plugin_styles' );

?>

3 Answers

Erik McClintock
Erik McClintock
45,783 Points

Zander,

You've got to make sure your syntax and naming conventions are consistent and appropriate throughout.

Your code is:

// Handle: my_plugin_css
// Plugin folder: my-plugin
// CSS file: my-plugin.css

function my_plugin_styles { // #1

    wp_enqueue_style( 'my-plugin_styles', plugins_url( 'my-plugin/my-plugin.css' )); // #2
}
add_action( 'admin_head', 'wp_plugin_styles' ); // #3

1) "my_plugin_styles" is a function, so you need to have the parenthesis in its declaration. Currently, you have function my_plugin_styles {; notice you go right into the opening curly brace without adding ().

2) The challenge has stated that the handle they're expecting is "my_plugin_css", so the first argument that you pass into the wp_enqueue_style function should be "my_plugin_css", but you have "my-plugin_styles"

3) In your action hook, you're trying to pass in a function called 'wp_plugin_styles', which doesn't exist. Your function is called 'my_plugin_styles', and that is what you need to pass in.

With those corrections, below is what your code should look like at the end of the challenge:

// Handle: my_plugin_css
// Plugin folder: my-plugin
// CSS file: my-plugin.css


// notice we have the () at the end of the function name
function my_plugin_styles() {

  // we have passed in the appropriate 'my_plugin_css' handle, per the instructions
  wp_enqueue_style( 'my_plugin_css', plugins_url('my-plugin/my-plugin.css') );

}
// we have passed in the correct function to our action hook: 'my_plugin_styles'
add_action( 'admin_head', 'my_plugin_styles' );

Erik

Zander Curtis
Zander Curtis
10,634 Points

Wow, thats some amazing feedback. I really messed this one up. :-)

Looks like the video "Adding CSS to WordPress Plugin Settings Pages" at :55 is wrong, or I am misunderstanding. It says to use the function hook.

"Then, we could use the WP enqueue style function. Give it the hook that is going to be the same name as our function name and then use a function called plugins URL. And then inside of that, type in the name of our folder for our plugin followed by the name of the CSS file we want link to."

Hey, I'm not complaining. TeamTreehouse is the most amazing online training on the planet. Primarily because of people like YOU Erik! (and of course Zac)

ZanderCurtis

Erik McClintock
Erik McClintock
45,783 Points

I agree whole-heartedly with the sentiment that Treehouse is the best online training service for all things web development, and I thank you for the compliment paid to me, as well! These are very easy mistakes to make in any language or system, so I wouldn't worry about them too much. Besides, that, WordPress is its own animal, and is very tricky to get used to. As with anything else, though, with enough repetition (both in writing your own code, as well as in rewatching the source material here, and expanding out to other resources [one that I always recommend is the Head First series of books from O'Reilly Media]), you'll become more attuned to spotting these mistakes. I'd like to say it gets easier to not make them in the first place, buuuuut...we'll all always make these kinds of errors (and still have a hell of a time spotting them, sometimes!) for as long as we're coding! It's just the nature of the beast :)

Anyway, I'm glad to have helped, and hope that you'll return to the forums and post again should you have another issue! As you may've found, here on Treehouse, there's always somebody willing to help at a moment's notice.

Happy coding!

Erik

Zander Curtis
Zander Curtis
10,634 Points

You are truly amazing Erik. It's people like you that help us break through the mental walls!

I've only started two months ago, so I guess I need to temper my expectations. :-)

I hope to pay it forward someday just as you have.

ZanderCurtis