How to add dash(-) to custom post type slug?
-
Other plugins and custom code allow to use “dash(-)” for slug of custom post type slug.
Why this plugin doesn’t allow it? I don’t like underscore. So how can I add “dash(-)” for slug of custom post type.
-
Good day @damithruwan1234
Please visit the following docs page for explanation of why we’ve done things the way we have and how to get the dash in place in regards to the urls.
https://docs.pluginize.com/article/135-dashes-in-post-type-taxonomy-slugs-for-url-seo
Michael, while this makes sense in relation to URL slugs, it causes problems for a particular case: pre-existing CPTs with hyphens for the slugs. In my case, I’m trying to clean up a site and move all the CPTs/CTs into CPT UI. This is easy to do for all the CPTS/CTs that don’t use hyphens, but there are two pre-existing instances that use hyphens.
Because of your forcing of underscores, there is no way for me to add these to CPT UI that I can see…unless there’s a workaround? It just doesn’t seem like it’s the business/function of a CPT plugin to force me to name my CPTs using a certain convention. Even so, while I generally prefer hyphens, I would switch to underscores if there was a way for me to do so. (Note: again, I am not talking about the URL slug. I’m talking about the actual slug for a CPT/CT.)
Am I missing something here? I don’t actually see the benefit of forcing people to use underscores.
- This reply was modified 5 years, 5 months ago by watermelonkid.
- This reply was modified 5 years, 5 months ago by watermelonkid.
- This reply was modified 5 years, 5 months ago by watermelonkid.
@watermelonkid valid questions/concerns.
The biggest thing, at least in my mind as I recall, for why the slugs should have underscores instead of hyphens is that there are various places in WordPress core that utilize the slugs, and that would break if not using an underscore. This is at a code level and around how PHP works.
As an example that goes into some more detail: https://premium.wpmudev.org/forums/topic/why-is-underscore-permitted-in-the-post-type-but-dash-is-disallowed#post-505820
Because of these details, we decided to take the action to help with compatibility and prevent potentially “random” errors and issues.
That said, it is possible to get around, as it’s all keyboard driven for the triggering of the auto-changing. You should be able to get by just fine by using your mouse/trackpad to paste in the slug with the dash instead of the underscore. Then, as long as you’re not touching the slug field, the rest should be fine to update.
I’m trying to clean up some CPTs that some set up before they left. I agree they should not have used a hyphen in the post type, but they did.
Pasting in the hyphen doesn’t work either. Anything I’ve tried to avoid using the keyboard doesn’t seem to work as it auto changes the – to a _.
The hyphen would be the last thing you want to paste before clicking away from the input and making it lose focus.
For instance if you wanted “my-post-type”, you would need to type out “myposttype” and then paste in the two hyphens in the appropriate spots with mouse/trackpad only.
@tw2113 I’m running into the same issue with the slug auto generating underscores. I’m trying to move some custom post types from Toolset Types to CPT UI. I’ve been able to get all post types to move over without any problems except for the one post type slug (social_media_records). I’ve added the correct slug (social-media-records to the “Custom Rewrite Slug” but I’m still having no luck in getting the existing posts (50 of them) to migrate over.
If your old post type slug was “social-media-records” and with your move in to CPTUI is forcing it to “social_media_records” then you may need to actually update/convert the post type slug column in your wp_posts table to match the underscore version. After that, with the rewrite parameter set to still use the dashes, that part should be fine. Never hurts to flush rewrite rules as well, just in case they need it.
Great, thanks for the suggestion. The states are now moving over after updating the links in the wp_posts column. The issue I’m having now is that The links are correct but they are not loading the correct template page. Do you have any suggestions to remedy this issue?
the template hierarchy/files are based on the slug. So if you had
archive-social-media-records.php
orsingle-social-media-records.php
those dashes would also need to be updated toarchive-social_media_records.php
andsingle-social_media_records.php
Thanks again. I’ve been digging through the files and there’s no archives for the social_media_records.php slug. I think its strange that the page is utilizing some of the css styling but there’s a few elements that looks like it’s defaulting to the default template such as the header.
Beyond what I’ve already provided, I can’t really say what’s going on without extr access to see setup and whatnot. For example, you may be using a custom template builder plugin or so that I’m not aware of. I generally assume it’s the standard theme/template setup for sites.
The styling issue ended up being a typo in the stylesheet. Thanks again for your help and recommendations on this subject.
Pesky typos. Glad you got that figured out.
- The topic ‘How to add dash(-) to custom post type slug?’ is closed to new replies.