Custom post types are one of the main reasons why WordPress is ahead of its game in terms of CMS market share.
But with this power, comes the effect of making it really easy to misuse! This doesn’t only go for Custom Post Types, but for all PHP snippets as well. Since it is easy to say ‘place your custom PHP functions in your theme’s
But you must think: why not anywhere else?
Why Setting Custom Post Types in Themes Is Bad
Think of a theme as a style or design of your website. Your theme has one duty and that is to output a great layout and design for your website.
Yes, it is okay to create a few templates like a custom Homepage template, or a custom Contact template.
But still we must draw a clear line between functionality and design.
This will create a very clear separation between: WordPress themes (design) and the WordPress core (functionality).
Definition of Design for WordPress Themes
WordPress themes are meant to display styles and layouts for the website to enhance the user experience.Click To Tweet
WordPress themes are meant to display styles and layouts for the website to enhance the user experience.
Therefore, hard-coded blocks of content are bad in WordPress themes. However, you are alright to use a few lines of text in your theme e.g. button texts like Return to Homepage.
Instead, you have to place your paragraphs of content in the page itself, rather than the theme template.
Definition of Functionality for WordPress Core
WordPress core functionality are salient components of the website that have to work regardless of the theme being used. Think of the big picture, years ahead of the game.Click To Tweet
WordPress core functionality are salient components of the website that have to work regardless of the theme being used. Think of the big picture, years ahead of the game.
The WordPress core functionality consists of plugins. And this is where WordPress shines amongst other CMS platforms — the WordPress Plugin Community.
So going back to the example with Custom Post Types and where they must be placed, we will consider them as functionality that have to work even if the WordPress theme were to be changed in the future.
PHP Snippets Can Live Outside the Functions.php File
There are many articles that state you can place this PHP snippet in your
functions.php file. But please check to make sure the PHP snippet is either design-oriented or layout-oriented.
For the most part, the article will be correct, since most of the time you will have to place your PHP snippet in your theme’s
functions.php file as it is likely to be design-oriented.
But remember too that there are times when the PHP snippet is better outside your theme. For example, plugin-dependent PHP snippets (like this) must work even if you change themes. So it is good to always be aware and ask if it can be placed outside a WordPress theme.
The reason is that will usually be tied to a plugin, for example. Therefore must be placed in a plugin themselves, and not in your WordPress theme. But you can’t really edit a plugin, so the next best thing is to place the PHP snippet outside of your WordPress theme.
How to Place PHP Snippets Outside a WordPress Theme
To place PHP snippets outside of a WordPress theme, you can use these PHP snippet plugins:
Why Use PHP Snippets Plugins Instead of Functions.php File
You will find that it is way more organised as you progress in your theme development, as well as in your WordPress web development and WordPress maintenance!
Want to deactivate a certain PHP snippet? Then you can simply deactivate it as if it were a plugin of its own. For example, if you use the Code Snippets plugin, you can accomplish deactivating certain PHP snippets.
Using PHP Snippets in plugins meant for them instead of in a
functions.php file is a lot more organised when necessary.
- There are two kinds of PHP snippets: Design-oriented or Functionality-oriented
- You have to decide whether or not a PHP snippet must be placed within the WordPress theme, or not
- Think about the long-term standing of the PHP snippet if it must be in a WordPress theme or not
Hope you enjoyed this article! Please comment on what you consider to be placed within a
functions.php file or not. Happy to hear your suggestions too!
If you liked this article you might also want to check out the 100 Reasons to Learn WordPress Theme Development, even if you are a great web developer!