Working with GeneratePress Hooks
warning
Hook callbacks run on every request that reaches the hook. Keep callbacks small and safe, and always escape dynamic output.
Working with GeneratePress Hooks Explained
| Hook concept | What it means | Example |
|---|---|---|
| Action | Runs code at a point in execution | add_action( 'generate_before_header', ... ) |
| Filter | Modifies a value and returns it | add_filter( 'generate_sidebar_layout', ... ) |
| Priority | Controls ordering | add_action( ..., 5 ) vs 10 |
| Remove callback | Disable default behavior | remove_action( 'hook', 'callback', 10 ) |
Why It Matters
- Hooks are the update-safe customization path for GeneratePress.
- Hooks reduce template overrides (and therefore upgrade debt).
- You can debug hooks with grep and with small, labeled outputs.
How It Works
GeneratePress defines hook points with do_action() and apply_filters(). Default theme behavior is attached via add_action() and add_filter(). Your child theme registers additional callbacks.
flowchart LR
HOOK[do_action / apply_filters] --> DEF[Default callbacks]
HOOK --> CHILD[Child callbacks]
CHILD --> OUT[Output change]
Practical Walkthrough
Step 1: Discover Hook Points (Theme Side)
discover-hook-points.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "do_action( 'generate_" -n inc templates | head -n 80
Step 2: Discover Default Callbacks
discover-default-callbacks.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "add_action( 'generate_" -n inc | head -n 80
Step 3: Add a Child Theme Hook (Safe Test)
wp-content/themes/generatepress-child/inc/hooks.php
<?php
add_action( 'generate_before_header', function() {
echo '<div class="gp-hook-test">Hook test: before header</div>';
} );
Step 4: Use a Filter for Layout Decisions
wp-content/themes/generatepress-child/inc/filters.php
<?php
add_filter( 'generate_sidebar_layout', function( $layout ) {
if ( is_page( 'landing' ) ) {
return 'no-sidebar';
}
return $layout;
} );
Practical Examples
Example 1: Use Priorities to Control Ordering
wp-content/themes/generatepress-child/inc/hooks.php
<?php
add_action( 'generate_before_header', function() {
echo '<div class="notice notice--early">Priority 5</div>';
}, 5 );
add_action( 'generate_before_header', function() {
echo '<div class="notice notice--default">Priority 10</div>';
}, 10 );
Example 2: Remove a Default Callback (Pattern)
Workflow:
- Find
add_action()in the parent theme (hook + callback + priority). - Remove it in the child theme with the same values.
wp-content/themes/generatepress-child/functions.php
<?php
add_action( 'after_setup_theme', function() {
// Pattern only: replace with real hook/callback/priority from grep.
// remove_action( 'generate_footer', 'generate_some_callback', 10 );
} );
Example 3: Build a Hook Map for a Region
map-header-hooks.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "do_action( 'generate_" -n inc/structure/header.php | head -n 120
Best Practices
| Practice | Why |
|---|---|
| Escape output | Prevent XSS |
| Keep callbacks small | Easier rollback |
| Prefer filters for decisions | Deterministic behavior |
| Use actions for markup | Clear output boundaries |
| Document hook name + scope | Avoid mystery behavior |
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Hook output not visible | Wrong hook or rule | Confirm hook exists with grep and test logged-out |
| Duplicate output | Registered twice | Search child theme for duplicate add_action |
| White screen | PHP syntax error | Check /usr/local/lsws/logs/error.log |
| Layout differs on some pages | Conditional mismatch | Verify is_page() slug/ID and rule scope |
Hands-On
- Discover 5 hook points in
inc/structure/header.php. - Attach a banner to one header hook.
- Move the banner to a different hook and document the placement difference.
- Remove the banner and confirm rollback.
Quick Reference
hooks-cheatsheet.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "do_action( 'generate_" -n inc templates | head
What's Next
- Next: Using Elements with Child Themes
- Related: Hook Elements