Where to declare / register custom post types

What’s a good practise in registering a custom post type? I have read the documentation and even tried searching the forums, but I’m afraid the information falls a little short (at least for me). Don’t misunderstand me, I love Sage.

Where can I create a CPT?

And also, template wise, does this 1 year old thread still count as a solution?

https://discourse.roots.io/t/custom-templates-for-post-types/2111

Thanks peeps :smile:

The best way for me is to make a functionality plugin for that kind of stuff. So if you ever need to change theme CPT’s will remain. Here is a more detailed explanation: http://justintadlock.com/archives/2013/09/14/why-custom-post-types-belong-in-plugins

Also this is a useful for registering custom post types, taxonomies etc: https://github.com/justintadlock/content-type-standards

slobich: Thanks for the fast answer. Will look into the URLs you posted. So to get this straight: your least favorite method (and the oldest method) would be to register custom post types in functions.php in Sage root? :hankey:

I used to use my sage theme for registering the post-types, and its fine to do so, but it makes making changes to them difficult, and limits you to using one theme or else edit/duplicating for a second theme or a second site.

Much cleaner to make a plugin for it.

1 Like

first of i would not register the CPT in functions.php, as you can see that file just calls other files from lib so i would either register in extras.php or make a new file like cpt.php and add it there. that is if you want to go with that method of registering CPT’s in a theme.

as for your question about the method. it’s not a huge deal. nothing will go terribly wrong if you decide to do it like that. but adding a plugin will maybe make things a bit easier. let’s say you’re doing some development and you have an issue on an archive page of a certain CPT. and you decide to rule out issues with your theme and decide to activate a different theme just to see if the issue is is still present. well if your CPT is registered in your theme you can’t see it if your theme is not active. or if your client decides to make a new web site/new theme latter. because your CPT is tied in with your theme they’ll have to move the CPT code from your theme to their new theme. but again this is not a huge deal. just a bit cleaner as @evanfuture pointed out.

one more note. sometimes i make a plugin for that and add it to mu-plugins. this is just so no one can accidentally deactivate it. but again that can cause issues latter on for the client if they decide to remove it by themselves if they don’t know how to use ftp or don’t know what mp-plugins are. so i tend to decide that on a case to case basis.

i suggest you read the article from Justin Tadlock i posted in my first post. he does say at the beginning of the post that his article is mainly focused on public themes but i think a lot of it can be applied to custom theme work as well.

The reason why this doesn’t exist in the documentation is because custom post types do not belong in the theme – at all.

Make sure you’re using either a plugin or mu-plugin for custom post types.

2 Likes

Thanks for some well thought and weighted posts, slobich and evanfuture. I’m kind of new to Sage, so I’m just learning the basics for now, just to get the hang of how the template system works, etc. So for starters I will only make a cpt.php and include it in the array in functions.php. But later, when I get it more under my skin, I will make a plugin out of it. Of cource a MU one.

Just for the fun of it I tried to add the portfolio CPT from the Github URL slobich pasted, however the post type wont show in the admin menu. This is exactly what I inserted into the mentioned cpt.php: https://github.com/justintadlock/content-type-standards/blob/master/portfolio/post-types.php

Any wild ideas?

Obviously understood :slight_smile:

Do not include custom post types in the theme! There is no reason why you can’t create a plugin / mu-plugin right now.

Hey, I know the rules :slight_smile: I’m just testing/playing locally to get to know Sage, nothing else. Nothing will be released like this. I appreciate your passion for good coding.