Skip to main content

Header & Navigation

warning

Avoid editing parent theme header templates. Use hooks/filters or a child theme to keep updates safe.

Header & Navigation Explained

ComponentWhat it controlsWhere to look
Header regionSite branding and header layoutinc/structure/header.php
Navigation regionMenu markup and mobile behaviorinc/structure/navigation.php (or similar)
Hook pointsInjection points around header/navdo_action( 'generate_*' )
Menu assignmentWhich menu is attached to a locationwp theme mod get nav_menu_locations

Why It Matters

  • The header is the most reused component across the site.
  • Navigation affects accessibility and performance (DOM size, JS behavior).
  • Clean header customization prevents future regressions when the theme updates.

How It Works

GeneratePress constructs the header and navigation via structure functions and fires hook points before/after regions. Your child theme can inject markup or adjust layout decisions without copying templates.

flowchart TD
TPL[templates/*.php] --> HEADER[inc/structure/header.php]
HEADER --> HOOKS[generate_* header hooks]
HOOKS --> CHILD[Child theme callbacks]
HEADER --> NAV[inc/structure/navigation.php]
NAV --> ASSETS[assets/js/*]

Practical Walkthrough

Step 1: Find Header and Navigation Files

locate-header-navigation-files.sh
cd /var/www/html/wp-content/themes/generatepress
ls inc/structure | grep -E "header|navigation" || true

Step 2: List Hook Points Around the Header

list-header-hooks.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "do_action( 'generate_" -n inc/structure/header.php templates 2>/dev/null | head -n 50

Step 3: Validate Menu Locations via WP-CLI

check-nav-menu-locations.sh
cd /var/www/html
wp theme mod get nav_menu_locations --format=json 2>/dev/null || echo "Theme mod command not available"

Expected output (example):

{
"primary": 12
}

Step 4: Find Default Header/Nav Callbacks

find-default-header-callbacks.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "add_action( 'generate_" -n inc/structure | head -n 60

Step 5: List Menus (Optional)

list-wp-menus.sh
cd /var/www/html
wp menu list 2>/dev/null || echo "No wp menu command found"

Practical Examples

Example 1: Add a Simple Top Notice Above the Header

wp-content/themes/generatepress-child/functions.php
<?php

add_action( 'generate_before_header', function() {
echo '<div class="gp-top-notice">Free shipping over $50</div>';
} );

Example 2: Find the Navigation Script Reference

find-navigation-js.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "navigation" -n assets inc functions.php | head -n 40
wp-content/themes/generatepress-child/functions.php
<?php

add_action( 'wp_body_open', function() {
echo '<a class="skip-link" href="#main">Skip to content</a>';
} );

Best Practices

PracticeWhy
Keep header markup minimalReduces DOM cost and improves INP
Ensure menu toggle is accessibleKeyboard and screen-reader usability
Prefer hooks for injected contentAvoids template forks
Document custom header behaviorPrevents future surprises
Keep header assets smallHeader is visible on every page

Troubleshooting

SymptomCauseFix
Menu not showingNo menu assigned to locationAssign a menu to the primary location and re-check theme mods
Mobile menu brokenJS/CSS conflictDisable minify/optimization plugins on staging to isolate
Notice shows on admin onlyCached vs uncached viewsTest logged-out + purge LSCache
Skip link not visibleCSS missingAdd minimal CSS for .skip-link and test with keyboard

Hands-On

  1. List hook points in inc/structure/header.php.
  2. Add a top notice with generate_before_header.
  3. Verify the notice renders on mobile and does not cause CLS.
  4. Add a skip link and confirm it works with keyboard navigation.

Quick Reference

header-nav-cheatsheet.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "do_action( 'generate_" -n inc/structure/header.php | head

What's Next