Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Accessing Active Elements/Overlays in “template_redirect” action?

  • Howdy,

    I’m trying to programmatically determine which GeneratePress Elements (and also GB Pro overlays) are active for a given front-end request. I’ve tried looking through the documentation, and even some of the backend code to see if I could unearth some filters or hooks, but so far I’m coming up short.

    Goal
    For a given page request, I need a reliable list of the active Element post IDs (e.g. Hook Elements, Layout Elements, etc.) that will be applied to that request. Same goes for Overlay Post types.

    Timing constraint
    Here’s the tricky part. I need this information during template_redirect (very early in the request lifecycle). I’m running a “preflight” pass before output starts, and I need to inspect the post_content of any active Elements for some custom functions I’m running. So I’m wanting to inspect not just page/post elements (which I could do with the render_block filters), but also things like my site headers, footers, overlays, etc..

    So the key question is…
    At template_redirect time, is there a reliable way to retrieve the list of Element IDs that will be applied to the current request?

    I’ve looked at:

    • The global $generate_elements
    • The Elements Helper class
    • General querying of gp_elements post type

    But I haven’t found a stable API or filter that provides the resolved list of active elements for the current request.

    What I’m looking for is ideally something like:

    $active_element_ids = some_generatepress_function();

    Or possibly a filter I can hook into that provides the resolved element array before rendering begins potentially?

    • I do not need rendered HTML — just the post IDs.
    • I assume GP already evaluates display rules internally, so I don’t think I need conditions evaluated manually.
    • I really just need access to the final resolved list of IDS for the current request (for Elements & Overlays)

    If there’s a supported function, global, or filter I should be using for this, that would be super helpful! If not, I’m open to getting my hands dirty with some creative workarounds. Hopefully it’s even possible to achieve this at all with the weird timing constraint I have.

    Thanks so much for any ideas or insights you might have to share!

  • Hi Michael,

    Yes you can do this, and you don’t need to manually evaluate any display rules.

    GP resolves Elements on the wp action, and GB Pro resolves overlays there as well. Since template_redirect runs afterward, the final list of items that matched the current request is already available at that point.

    GP stores active Elements in the global $generate_elements, and GB Pro stores active overlays in $gb_overlays. These globals only contain posts whose display conditions passed for the current request, so they effectively represent the resolved set you’re looking for.

    So within template_redirect, you can reliably retrieve the IDs like this:

    add_action( 'template_redirect', function() {
        global $generate_elements, $gb_overlays;
    
        $element_ids = array_keys( (array) $generate_elements );
        $overlay_ids = array_keys( (array) $gb_overlays );
    
        $all_ids = array_merge( $element_ids, $overlay_ids );
    
        foreach ( $all_ids as $id ) {
            $content = get_post_field( 'post_content', $id );
            // Run your inspection logic here.
        }
    });

    Because the globals are populated after GP/GB have already evaluated their conditions, you don’t need to duplicate any of that logic yourself.

    If you’d rather intercept things at the moment they are approved for display, you could also hook into generate_element_display, which fires once per Element after its conditions pass. But if your goal is simply to gather the final IDs during template_redirect, reading the globals is the simplest and most reliable option.

  • Alvind,

    Thanks so much for this! This worked very well. I think I was actually very close initially to getting this working, but clearly I just had a mistake somewhere. Seeing your full code did the trick though, and I’m back on track with my project.

    Much appreciated, as always. You guys are the BEST!

    Have an excellent weekend 🙂

  • You’re welcome, Michael. Glad to hear that! 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.