Skip to main content

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

ResponsibilityPreferWhy
Conditional placementElements display rulesUI-driven, fast to adjust
Shared CSS tokensChild theme CSS filesVersion control and reuse
Helper logicChild theme/site pluginReviewable code
One-off snippetsHook elementQuick 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

TypeExample name
Hook elementHook: Top notice (Home only)
Layout elementLayout: Landing no sidebar
Child CSS fileassets/css/elements.css

Best Practices

PracticeWhy
Put CSS in the child themeBetter caching and reuse
Keep Elements smallEasier audits
Document element IDs and scopesFaster rollback
Prefer block elements for complex sectionsBetter editing UX
Avoid PHP in Elements when possibleKeeps code reviewable

Troubleshooting

SymptomCauseFix
Element shows twiceTwo elements match same ruleAudit inventory and disable one
CSS not appliedStyles not enqueuedConfirm elements.css is enqueued
Changes not visibleCachePurge LSCache and re-test
Shortcodes render as textShortcodes disabledEnsure shortcodes are enabled and avoid nested shortcodes

Hands-On

  1. Create an element inventory snapshot with WP-CLI.
  2. Create one Hook Element with a single CSS class.
  3. Add styles for that class in assets/css/elements.css.
  4. Disable the element and confirm rollback.
  5. 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