Create a New WP Rig Component
This skill provides the recipe to scaffold and register a new theme component.
Step 1: Use the Scaffolding Script
WP Rig provides a dedicated script to create the component directory and initial class file.
npm run create-rig-component "Your Feature Name"
This will:
- Create a folder in
inc/Your_Feature_Name/. - Generate
Component.phpinside it, implementingComponent_Interface. - Automatically Register the new component in
inc/Theme.phpby adding it to theget_default_components()method.
Step 2: Implement Hooks
After scaffolding, the component is already wired and ready. Open your new inc/Your_Feature_Name/Component.php and use the initialize() method to add WordPress hooks.
public function initialize() {
add_action( 'wp_enqueue_scripts', array( $this, 'action_enqueue_scripts' ) );
}
Best Practices
- Always use
npm run create-rig-componentinstead of manual creation. - Ensure the namespace matches
WP_Rig\WP_Rig\{Feature}. - Implement only the interfaces you need (e.g.,
Templating_Component_Interfaceif you provide template tags). - If the scaffolding script fails to auto-wire (check command output), manually add
new Your_Feature_Name\Component(),toinc/Theme.php.