justintadlock / content-type-standards

WordPress community-curated standards for common post types, taxonomies, and metadata.
303 stars 26 forks source link

Underscores in Content Types #23

Open robneu opened 9 years ago

robneu commented 9 years ago

I realize I'm a little late here, but I just wanted to say that having templates like single-portfolio_project.php in a theme makes me very sad. :(

In addition to these less-than-awesome template names, using underscores also forces you to use underscores in your CSS when targeting things like singular-portfolio_project. This makes me want to die a little every time I have to do it. I'm sure there are probably workarounds for both of these issues, but couldn't we just avoid them entirely? Is there a reason why underscores are being used?

It may be too late for portfolio, although I think it would be great if we could revisit that one as well, but unless there's a reason I'm not aware of, can we please not use underscores when registering the content types in the future?

kraftner commented 9 years ago

What is your issue with this and how else would you solve it? We need some kind of multi-word separator and singular-portfolio-project is way harder to read if you ask me.

robneu commented 9 years ago

This article is a bit old, and mainly dealing with CamelCase in CSS, but the same thing applies to using underscores: http://csswizardry.com/2010/12/css-camel-case-seriously-sucks/

All standard WordPress templates are hyphen-delimited and I don't see any reason to shift away from what core does for custom content types unless there's some technical thing with CPTs that I'm unaware of. I can't think of anywhere in core where an underscore is being used within a file or template name.

It may be a little harder to read in the context of a PHP file, but everywhere else an underscore seems to be the odd man out.

Stephen-Cronin commented 9 years ago

Hi Rob,

I totally agree that hyphens are the standard way of separating file names. I would never choose to use an underscore in a file name. Mixing hyphens and underscores in the same file name is even worse.

But....

Using underscores is pretty much standard for separating parts of a CPT name. I would never choose to use a hyphen in a CPT name (or a function name). That'd be equally as weird.

In this case something's gotta give.

All the code I've ever seen to do with CPTs goes with the using underscores when registering the CPT and getting the mixed hyphen / underscore file name as a result. That's what's used in egs on the Codex, both on the Post Types and Post Type Templates pages.

It does look ugly in the filename, but like it or not, it's pretty much standard. I think we should follow suit here rather than doing our own thing.

justintadlock commented 9 years ago

Yep, it's best to follow core's example here. It uses underscores for naming CPTs and CTs. It seems to a be pretty well accepted standard.

This project doesn't deal with template names or CSS classes (it has an indirect effect though). Those things are easily changed if you choose to do so.

If you look at it from the other side, using a hyphen has issues as well. Core has hooks built from the post type name that will have a hyphen instead of an underscore. That will technically work just like an underscore will work in template names. But, on the whole, it's a lot more future-proof to work with underscores in PHP.

robneu commented 9 years ago

I see. Well, in that case, underscores sound like the way to go. Could we perhaps provide some example code for mapping the templates and CSS classes to a hyphen-delimited version somewhere? Probably in the wiki would be best. Are there any areas where this is done in core that I could reference?

GaryJones commented 9 years ago

See how WP handles the core CPT of nav_menu_item?

robneu commented 9 years ago

I've taken my first stab at dealing with this in a plugin that I'm working on for our upcoming theme release. It still needs some work, but so far what I have there seems to be working correctly. Template Hierarchy Changes and CSS Class Changes

If anyone has any suggestions that would improve how I'm handling this, I'd love to hear them. I'd also be happy to add some example code to the Wiki once it's assured that I'm not _doing_it_wrong :)