Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

php - 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.


EDIT: 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


EDIT: July 31st, 2019

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):

Miscellaneous Developer Updates in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Alternatively, if you are creating the theme yourself and/or can modify it, you can create an action yourself using WordPress' do_action function. This is also how they create their other hooks. So basically in your theme, you would go where you want to, right after the <body> tag, and do something like:

do_action('after_body');

You can also pass arguments to the action callback, see the linked documentation for information.

Then afterwards, you would simply use the add_action function to hook onto it.

add_action('after_body', 'my_callback');

Hope that helps. Sorry if I misunderstood.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...