WordPress 在 body 标签后直接挂钩 [英] WordPress hook directly after body tag
问题描述
我在为我的插件寻找合适的钩子时遇到问题.我试图通过让我的插件添加一个功能来在每个页面的顶部添加一条消息.最好用的钩子是什么?我想在 标签之后插入内容.
I'm having problems finding the right hook to use for my plugin. I'm trying to add a message to the top of each page by having my plugin add a function. What's the best hook to use? I want to insert content right after the <body>
tag.
我知道现在是三年后,但这里有一张 Trac 票任何感兴趣的人:http://core.trac.wordpress.org/ticket/12563一个>
I know it's three years later now, but here is a Trac ticket for anyone who is interested: http://core.trac.wordpress.org/ticket/12563
2019 年 7 月 31 日
链接的 Trac Ticket 已关闭,因为 WordPress 5.2 中添加了此功能.您可以在此处找到此功能的开发人员说明(需要启用 JavaScript 才能显示):
The linked Trac Ticket was closed as this feature was added in WordPress 5.2. You will find the Developer notes for this feature here (requires JavaScript enabled to display):
由于历史原因,我不会更新提到 5.2 的正确答案",但请放心,我知道并且内置钩子是正确使用的.
I will not update the "correct answer" to one that mentions 5.2 for historical reasons, but rest assured that I'm aware and that the built-in hook is the correct one to use.
推荐答案
WordPress 5.2 或更新版本:
使用 wp_body_open 钩子.
这有点困难...大多数主题在该区域没有任何挂钩.您可以将 javascript/html 解决方案挂钩到 wp_footer
中,并将其显示在页面顶部……类似于 Stack Overflow 的做法,或者 Twitter 的通知方式.
That's kinda difficult... Most themes don't have any hooks in that area. You could hook a javascript/html solution into wp_footer
and display it at the top of the page... sort of how Stack Overflow does it, or how Twitter does their notifications.
这是 WordPress 中包含的所有钩子的最佳参考:http://adambrown.info/p/wp_hooks/
This is the best reference for all the hooks included in WordPress: http://adambrown.info/p/wp_hooks/
这篇关于WordPress 在 body 标签后直接挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!