• Resolved petshwark

    (@petshwark)


    I’m writing a plugin with the goal of it outputting HTML before the theme’s header template part and after the theme’s footer template part – essentially a branding header and footer on the page that appears regardless of what the theme supplies. We have an existing plugin that is not working well, and instead of trying to diagnose it (the original developer is not available for helping), I’m hoping to scrap it and write a new one.

    The original plugin uses the ‘shutdown’ hook to hijack the page output and inject HTML after the ‘<body>’ for the header and before the ‘</body>’ for the footer. That didn’t seem all that elegant so I went looking for more appropriate hooks to use.

    After a lot of rooting around in the WordPress docs, I stumbled upon the ‘wp_body_open’ hook, and it seems to fit the bill for outputting some heading HTML before the theme outputs its header template part, but I can’t seem to find an appropriate one for the footer. I found ‘wp_footer’, but the docs indicate that the theme has to support it. I’m using a modified version of Twenty Twenty-Four, and it does work with the ‘wp_footer’ hook, but I was hoping to make the plugin more universal.

    If anyone has some suggestions for a good hook that fires near the end of the page output, that would be great. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Sahil Gidwani

    (@sahilgidwani)

    Hey @petshwark ,

    For Output Before the Header:

    You’ve already identified the wp_body_open hook, which is perfect for injecting HTML right after the opening <body> tag. This hook is a solid choice because it is widely supported in modern themes and ensures your content is output early in the page load process.

    For Output After the Footer:

    For the footer, you’re correct that wp_footer is theme-dependent, but most well-coded themes do support it, including Twenty Twenty-Four. Unfortunately, WordPress doesn’t have a direct equivalent to wp_body_open at the very end of the document. However, you can reliably use wp_footer, which fires just before the closing </body> tag, assuming the theme properly calls wp_footer() in the footer.php file.

    A Universal Alternative:

    If you want to ensure your plugin is more universal and works regardless of theme support, you could hook into shutdown. This hook is fired at the very end of the WordPress lifecycle, after all output has been sent. While it’s not as elegant as wp_footer, it guarantees the footer content is added universally across themes.
    The shutdown hook would output your footer just before the response is sent back to the browser, though it may not always be perfectly placed right before the </body> tag, depending on how the theme handles output buffering.

    Hope this helps!

    Moderator bcworkz

    (@bcworkz)

    If you generate output from the “shutdown” action, it’ll end up outside the closing </body></html> tags. While I imagine most browsers will happily display your content at the very bottom of the page anyway, it is technically invalid HTML. Just sayin’ ??

    I suggest relying on “wp_footer” and using “shutdown” as a fallback in case your “wp_footer” callback fails to run. For example, in “wp_footer” callback, define a unique constant and generate output. In “shutdown”, only output content if that constant is not defined. I agree with sahilgidwani that most decent themes will fire “wp_footer”. The few that do not would not be very performant since WP would then need to reference all script files in <head>, even those that’d be better off deferred until “wp_footer”. Thus it behooves any theme author to implement “wp_footer”.

    Thread Starter petshwark

    (@petshwark)

    @bcworkz , thanks for the insight. Yeah, the original plugin that we’re using used the ‘shutdown’ hook and then grabbed the output buffer to make changes on it before outputting it. That just seems kludgey to me, so that’s why I was on a hunt for a better hook or pair of hooks. Before looking into this, I kinda imagined that there would be filter hooks in WP for the header and footer content, and I was surprised to see that there wasn’t. Using the ‘wp_open_body’ and ‘wp_footer’ hooks will have to do for now.

    Thread Starter petshwark

    (@petshwark)

    Thanks @sahilgidwani for the insight!

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