Using Elements with Child Themes
warning
Avoid duplicating logic in both an Element and the child theme. Define one source of truth, or you will debug "double output" issues later.
Using Elements with Child Themes Explained
| Responsibility | Prefer | Why |
|---|---|---|
| Conditional placement | Elements display rules | UI-driven, fast to adjust |
| Shared CSS tokens | Child theme CSS files | Version control and reuse |
| Helper logic | Child theme/site plugin | Reviewable code |
| One-off snippets | Hook element | Quick changes (document them) |
Why It Matters
- Elements reduce template overrides and keep work UI-driven.
- Child themes keep code and CSS audit-friendly.
- Combining both gives you a professional workflow (config + code).
How It Works
An Element renders content at a hook location (or region) when rules match. The child theme provides stable CSS and helpers that the element content can reference.
flowchart LR
ELEM[Element output] --> CSS[Child theme CSS]
CSS --> OUT[Final UI]
CHILD[Child theme helpers] --> ELEM
Practical Walkthrough
Step 1: Verify Elements Are Available
verify-elements-availability.sh
cd /var/www/html
wp plugin is-active gp-premium && echo "gp-premium active" || true
ls -lah wp-content/plugins/gp-premium/elements 2>/dev/null | head -n 60 || true
Step 2: Inventory Elements (Optional)
inventory-elements.sh
cd /var/www/html
wp post list --post_type=gp_elements --fields=ID,post_title,post_status --format=table 2>/dev/null || true
Step 3: Create a Stable CSS Target for Elements
wp-content/themes/generatepress-child/assets/css/elements.css
.gp-element-banner {
padding: 12px 16px;
background: #f8fafc;
border: 1px solid #e2e8f0;
}
Step 4: Enqueue elements.css
wp-content/themes/generatepress-child/inc/enqueue.php
<?php
add_action( 'wp_enqueue_scripts', function() {
$css = get_stylesheet_directory() . '/assets/css/elements.css';
if ( file_exists( $css ) ) {
wp_enqueue_style(
'gp-child-elements',
get_stylesheet_directory_uri() . '/assets/css/elements.css',
array(),
filemtime( $css )
);
}
} );
Practical Examples
Example 1: Hook Element + Child Theme CSS
Element content:
Hook Element content
<div class="gp-element-banner">Free shipping over $50</div>
Child theme provides the CSS for .gp-element-banner.
Example 2: Element Content + Helper Shortcode
Child theme shortcode:
wp-content/themes/generatepress-child/functions.php
<?php
add_shortcode( 'year', function() {
return esc_html( gmdate( 'Y' ) );
} );
Element content:
Element content
<small>© [year] Example Co.</small>
Example 3: Avoid Duplicates with a Naming Convention
| Type | Example name |
|---|---|
| Hook element | Hook: Top notice (Home only) |
| Layout element | Layout: Landing no sidebar |
| Child CSS file | assets/css/elements.css |
Best Practices
| Practice | Why |
|---|---|
| Put CSS in the child theme | Better caching and reuse |
| Keep Elements small | Easier audits |
| Document element IDs and scopes | Faster rollback |
| Prefer block elements for complex sections | Better editing UX |
| Avoid PHP in Elements when possible | Keeps code reviewable |
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Element shows twice | Two elements match same rule | Audit inventory and disable one |
| CSS not applied | Styles not enqueued | Confirm elements.css is enqueued |
| Changes not visible | Cache | Purge LSCache and re-test |
| Shortcodes render as text | Shortcodes disabled | Ensure shortcodes are enabled and avoid nested shortcodes |
Hands-On
- Create an element inventory snapshot with WP-CLI.
- Create one Hook Element with a single CSS class.
- Add styles for that class in
assets/css/elements.css. - Disable the element and confirm rollback.
- Rename the element to include scope and hook name.
Quick Reference
elements-plus-child-cheatsheet.sh
cd /var/www/html
wp post list --post_type=gp_elements --fields=ID,post_title,post_status --format=table 2>/dev/null || true
What's Next
- Next: Customizer Integration
- Related: Global Styles & Presets