What is a theme component?
Theme components are bite sized code snippets that can be reused across multiple themes. Using theme components makes it easier to reuse the same code across multiple themes.
What is the difference between theme components and plugins?
Good question! The main difference is context. Plugins tend to be abstracted away from themes and are made in a theme agnostic way. Theme components are usually helper functions that can be used by themes. They serve to make the reuasability of plugins available to code that belongs in a theme.
How do you prepare a theme for theme components?
- Download the components helper and drop the
components.php
file into your theme directory. - Include the
components.php
file in your themefunctions.php
file (or other PHP file).
If including the file using a callback, make sure it is hooked ontoafter_setup_theme
no later than priority of -1000. - Voila!
How do you install theme components?
Simply copy a component folder into the theme-components
folder in your theme directory.
How do you create a theme component?
- Create a folder inside the
theme-components
with the name of your component. - Inside your component folder, create a
component.php
file. This is your main component file (similar to afunctions.php
file). - As a best practice, declare the namespace
WP_Theme_Components\My_Component_Name
- From there, you can add your code snippets.