Skip to main content

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 conceptWhat it meansExample
ActionRuns code at a point in executionadd_action( 'generate_before_header', ... )
FilterModifies a value and returns itadd_filter( 'generate_sidebar_layout', ... )
PriorityControls orderingadd_action( ..., 5 ) vs 10
Remove callbackDisable default behaviorremove_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.

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:

  1. Find add_action() in the parent theme (hook + callback + priority).
  2. 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​

PracticeWhy
Escape outputPrevent XSS
Keep callbacks smallEasier rollback
Prefer filters for decisionsDeterministic behavior
Use actions for markupClear output boundaries
Document hook name + scopeAvoid mystery behavior

Troubleshooting​

SymptomCauseFix
Hook output not visibleWrong hook or ruleConfirm hook exists with grep and test logged-out
Duplicate outputRegistered twiceSearch child theme for duplicate add_action
White screenPHP syntax errorCheck /usr/local/lsws/logs/error.log
Layout differs on some pagesConditional mismatchVerify is_page() slug/ID and rule scope

Hands-On​

  1. Discover 5 hook points in inc/structure/header.php.
  2. Attach a banner to one header hook.
  3. Move the banner to a different hook and document the placement difference.
  4. 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​