Why is underscore permitted in the post type but dash is disallowed?

Why is underscore permitted in the post type but dash is disallowed. Dash is the default character for permalinks. Post Types created using the underscore are not found! I fixed this by updating the validate-field function in core/content-types.php. My question is why is underscore permitted and dash disallowed? My second question is can you add support for dash so I don't need to patch this plugin on every upissue. Thanks in advance.

  • timhibberd

    Thanks Patrick. Your response reminds me to clarify the problem I encountered.

    I have a multi-word custom post type to create (e.g. working group). When I set it to working_group I received a 404 for posts created using this custom post type. When I modified the validate-field function in core/content-types.php to support dash and set the custom post type to working-group everything works fine.

    On review of my 404 error when using underscore I likely tripped over the long-running quirk in Wordpress that requires the permalink to be changed then changed back and saved to resolve 404s.

    (Extract from http://codex.wordpress.org/Post_Types)."Note: In some cases, the permalink structure must be updated in order for the new template files to be accessed when viewing posts of a custom post type. To do this, go to Administration Panels > Settings > Permalinks, change the permalink structure to a different structure, save the changes, and change it back to the desired structure."

    On further checking the default separator in multi-word post type names appears to be underscore (see http://codex.wordpress.org/Post_Types).

    My Question for the developer is "Why are dashes excluded from the post type label?" The register-post function (see http://codex.wordpress.org/Function_Reference/register_post_type) clearly states that only capital letters and spaces are not allowed.

    " $post_type
    (string) (required) Post type. (max. 20 characters, can not contain capital letters or spaces)"

  • Arnold

    It's a matter of rules. Wordpress designed it that way.

    Primarily because the php language allows underscores in variable names and not dashes. A dash would look like a minus sign and trigger subtraction which is not hat you want.

    And be sure of your nomenclature. the post type/taxonomy name must use underscores, but the Labels that are defined for it can have dashes in them.

    Dashes are allowed in URLS (permalinks) because W3C defined it that way.

    Also there are various "bugs" in WP and sevral themes which cause problems.



    Now I have no problem creating a post type with the post type name "working_group" and I don't get errors when retrieving pages made by it so maybe we should look for the reason you're having the 404 errors.

    By properly setting the rewrite rule you can have hyphens in the urls and use underlines in the post type name. Set the post type name to "working_group" and the rewrite slug to "working-group" and the has_archive slug to "working-groups"

  • timhibberd

    Thanks Arnold for considering my request. Much appreciated. I would ask you to re-consider your position though...respectfully.

    Although your position is not to make the change to allow dashes your evidence supporting that position actually appears to me to refute that position...again, respectfully. Happy to be corrected if I'm reading it incorrectly.

    The core wordpress ticket 15970 you refered to states "Since sanitize_key() allows hyphens, we should probably handle this.". It then refers to the fix via dup 18958 that , in response to the suggestion to follow php convenvention which excludes the dash, was countered with the comment " Sounds fair, Except that dashes are universally the standard for slugs in WordPress (URL's, Filenames, CSS classes, etc). Underscores are a PHP/JS Language construct, which shouldn't make a difference here, as slugs should never be hardcoded or expressed as a variable. It's a shame that dash was ever sanitized to an underscore really, I still can't see a good reason for it in this context." It then goes on to refer to Changeset 21322 which deletes the old code that converted dash to underscore "Fix per_page screen option for custom post types and taxonomies. Don't convert taxonomy and post type slugs from hyphen to underscore when saving the per_page usermeta.".

    Regarding the issue with Headway the link you referred to states "I think this is probably a Headway issue. The only restriction on the slug is "max. 20 characters, can not contain capital letters or spaces""

    My request is simply to relax your code rule to allow dashes. This won't break Headway if people don't use dashes and if they did want to use dashes then that is between them and headway to resolve.

    Regarding working_group...I did say above that I got that to work once I deduced that I had been the victim of a known long-running quirk in Wordpress requiring the permalink to be changed then changed back and saved to resolve 404s.

    Thanks again for your consideration. I hope you can help me to eliminate my only running patch on a great plugin I enjoy using :slight_smile:

  • Arnold

    Again I ask. Why do you need a patch? The slug for the URL is defined in the rewrite and has_archive part of the definition. So your URL slug can be exactly what you want with the dashes.

    The problem with saying it's a Headway or any other Theme or plugin problem is that they won't know that and since it's CustomPress that appears to be the problem, they ask us first, just as you did :slight_smile:

  • timhibberd

    Thanks for the quick reply Arnold.

    The issue is that when an "Add New" command is executed involving a custom post the permalink is automatically given a prefix which is based on the post_type name not the label. Since CustomPress disallows the dash from being entered in the post-type name via the U.I. the result is permalinks with underscored multi-word prefixes. I want permalinks with the dash character only. Even the wordpress team seems to prefer dashes to underscores in their permalinks... see http://codex.wordpress.org/Using_Permalinks :slight_smile:

    Regarding your comment on increased support burden because of coding errors by other plugin designers I can empathize but you don't seem like the kind of guy who would make code changes to cover for errors other designers should be fixing themselves in their plugins / themes :slight_smile:

    In summary:
    It is my humble contention that CustomPress currently does not honour permalink support for dashes by blocking dashes in the post_type name definition and auto-generating the permalink prefix based on the post_type name.

    Options for consideration:
    1) Leave plugin as is in which case I will need to carry a patch forever :slight_frown: Note: I consider a mod-rewrite rule to be another form of patch.

    2) Allow dashes to be entered in the post_type name

    3) Continue to restrict the post-type name to underscores only but auto-generate the permalink prefix on creation of a new custom post based on the post-type label instead of the post-type name.

    NOTE: Not sure what the implications of option 3 might be. You are probably better qualified to determine if option 3 would not break WordPress design conventions (I'm not sure what the general operational relationship between a post-type name and permalink prefix is post-creation or what the implications are if the permalink prefix for a custom post type does not match the name of the post-type it was created from...would the url-resolver not be able to locate it perhaps...for example!).

    Regardless of your decision I am very impressed by your scrutiny. I like the fact that you are arguing the point...the sign of an excellent coder and thus robust code. Makes me like the CustomPress plugin even more :slight_smile:

  • Arnold

    OK you're not listening to what I'm saying. In the post type definition the Rewrite and Has Archive settings control the slug in the permalink. Not the name of the post type. That's just a default if you don't set anything else. Take a look at the setting in the image below. I've collapsed a lot of the non-germane fields so I can get the important ones on the image.

    Name = "working_group"
    Has Archive = "working-groups"
    Rewrite slug = " working-group"

    Exactly what you asked for. See the sample post for the resulting permalink.

    This is what you're trying to achieve?? Or am I missing something?

  • timhibberd

    Big, Big BIG apology Arnold for chewing up time in your life you would probably have preferred spending watching footie or catching some rays. When you referred to the rewrite rules earlier I had thought you were suggesting adding a mod_rewrite rule to the htaccess file. I had not twigged to it being a referral to the "how did you miss it Tim it's staring you in the face" rewrite option in your plugin.

    You missed nothing...I missed the forest for the trees. Mea culpa. I'm ashamed to admit that I am a long-time ex-coder who spent too long on the business side and is on the steep climb to re-build rusty coding skills...obviously rustier than I thought. I'm going to have to claim old age, bad glasses prescription, and momentary senility attack in an attempt to salvage a modicum of dignity on this one :slight_smile:

    Top marks for patience.
    Top marks for quality of plugin
    Sorry about overlooking the obvious

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.