php WordPress 在 body 标签后直接挂钩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3581510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WordPress hook directly after body tag
提问by Brooke.
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.
我在为我的插件找到合适的钩子时遇到问题。我试图通过让我的插件添加一个功能来在每个页面的顶部添加一条消息。最好用的钩子是什么?我想在<body>
标签之后插入内容。
EDIT: I know it's three years later now, but here is a Tracticket for anyone who is interested: http://core.trac.wordpress.org/ticket/12563
编辑:我知道现在已经三年后了,但这里有一张Trac票给任何有兴趣的人:http: //core.trac.wordpress.org/ticket/12563
EDIT: July 31st, 2019
编辑:2019 年 7 月 31 日
The linked Trac Ticketwas 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):
由于此功能已在 WordPress 5.2 中添加,链接的Trac 票已关闭。您可以在此处找到此功能的开发人员说明(需要启用 JavaScript 才能显示):
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.
由于历史原因,我不会将“正确答案”更新为提到 5.2 的答案,但请放心,我知道并且内置挂钩是正确使用的。
采纳答案by jdp
WordPress 5.2 or newer:
WordPress 5.2 或更新版本:
Use the wp_body_openhook.
使用wp_body_open钩子。
WordPress 5.1 or older:
WordPress 5.1 或更高版本:
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.
这有点困难...大多数主题在该区域没有任何钩子。您可以将 javascript/html 解决方案挂钩wp_footer
并显示在页面顶部......类似于 Stack Overflow 的做法,或者 Twitter 的通知方式。
This is the best reference for all the hooks included in WordPress: http://adambrown.info/p/wp_hooks/
这是 WordPress 中所有钩子的最佳参考:http: //adambrown.info/p/wp_hooks/
回答by Jorge Israel Pe?a
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:
或者,如果您自己创建主题和/或可以修改它,您可以使用 WordPress 的do_action
功能自己创建一个动作。这也是他们创建其他钩子的方式。所以基本上在你的主题中,你会去你想去的地方,就在<body>
标签之后,然后做一些类似的事情:
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
函数来挂钩它。
add_action('after_body', 'my_callback');
Hope that helps. Sorry if I misunderstood.
希望有帮助。对不起,如果我误解了。
回答by Stan
Creating custom hook is really easy in WordPress.
in header.php
(or anywhere you may need a hook) locate:
在 WordPress 中创建自定义钩子真的很容易。在header.php
(或任何您可能需要挂钩的地方)找到:
<body <?php body_class(); ?>>
<div id="body-container">
and make it:
并使它:
<body <?php body_class(); ?>>
<?php body_begin(); ?>
<div id="body-container">
That's our hook, now let's make it work.
In functions.php
add:
这是我们的钩子,现在让我们让它工作。另外functions.php
:
function body_begin() {
do_action('body_begin');
}
Now hook is ready to use, simply add any actions you need in functions.php
:
现在钩子可以使用了,只需在 中添加您需要的任何操作functions.php
:
function my_function() {
/* php code goes here */
}
add_action('body_begin', 'my_function');
or JavaScript (tracking code etc - this is not the perfect way though, it is a better practice to load JavaScript from .js
files, but it is definitely better than adding JavaScript directly to template files):
或 JavaScript(跟踪代码等 - 虽然这不是完美的方式,从.js
文件加载 JavaScript 是更好的做法,但绝对比将 JavaScript 直接添加到模板文件更好):
function my_function() { ?>
<script>
<!-- JavaScript goes here --!>
</script>
<?php
}
add_action('body_begin', 'my_function');
回答by Krzysiek Dró?d?
Changes, changes, changes. So it appears that since March 2019 (from WP 5.2) we have a little nicer way to do this.
变化,变化,变化。因此,自 2019 年 3 月(来自 WP 5.2)以来,我们似乎有了更好的方法来做到这一点。
There is a new function wp_body_open()
. To support it, your theme has to call this function right after <body>
opening tag:
有一个新功能wp_body_open()
。为了支持它,您的主题必须在<body>
打开标签后立即调用此函数:
<html>
<head>
..
..
<?php wp_head(); ?>
</head>
<body>
<?php wp_body_open(); ?>
..
..
<?php wp_footer(); ?>
</body>
</html>
And then you can use it in the same way you use wp_head
or wp_footer
hooks to print anything just after <body>
.
然后你可以像使用wp_head
或wp_footer
钩子一样使用它来打印<body>
.
回答by De Paragon
I searched the internet for answers to the same question but found nothing. I figured out away to to work around it. My plugin infinite Ad Payis based on this method.
我在互联网上搜索了同一个问题的答案,但一无所获。我想办法解决它。我的插件无限广告支付就是基于这种方法。
You need two hooks wp_headand wp_footerhook
你需要两个钩子 wp_head和wp_footer钩子
add_action( 'wp_head', 'my_buffer_holder_fxn');
function my_buffer_holder_fxn(){
ob_start()
}
function my_buffer_pour_out_fxn(){
$get_me_buffers = ob_get_clean();
$pattern ='/<[bB][oO][dD][yY]\s[A-Za-z]{2,5}[A-Za-z0-9 "_=\-\.]+>|<body>/';
ob_start();
if(preg_match($pattern, $get_me_buffers, $get_me_buffers_return)){
$d_new_body_plus =$get_me_buffers_return[0]."<div class='my_below_body_code'> This is below the body text or image or anything you want </div>";
echo preg_replace($pattern, $d_new_body_plus, $get_me_buffers);
}
ob_flush();
}
}
add_action( 'wp_footer', 'my_buffer_pour_out_fxn');
// You can also use the method above to place anything in other sections of WordPress
//No Javascript used
回答by Martin from WP-Stars.com
A very, very, very dirty solution would be:
一个非常、非常、非常脏的解决方案是:
/* Insert tracking code or others directly after BODY opens */
add_filter('body_class', 'wps_add_tracking_body', PHP_INT_MAX); // make sure, that's the last filter in the queue
function wps_add_tracking_body($classes) {
// close <body> tag, insert stuff, open some other tag with senseless variable
$classes[] = '"><script> /* do whatever */ </script><noscript></noscript novar="';
return $classes;
}
回答by josoroma
In this scenario, what i do is: Use Jquery to append or prepend things:
在这种情况下,我要做的是:使用 Jquery 附加或预先添加内容:
回答by Sabir Abdul Gafoor Shaikh
I could not find any working example online, but I got one solution and I hope it might help anyone. It is quite simple, just add jquery and do whatever you want. The below example might help anyone.
我在网上找不到任何有效的例子,但我得到了一个解决方案,我希望它可以帮助任何人。这很简单,只需添加 jquery 并随心所欲。下面的例子可能对任何人都有帮助。
jQuery(document).ready( function($) {
$('body').prepend('<h1>Hello world</h1>');
});
Here is the website url https://wordpress.org/support/topic/add_action-right-after-ltbodygt-tag
这是网站网址 https://wordpress.org/support/topic/add_action-right-after-ltbodygt-tag
With the help of the above answer, I have made a plugin which directly adds content after the body tag. Here is the sample code :
借助上述答案,我制作了一个插件,直接在 body 标签后添加内容。这是示例代码:
var wcs_thankyou_msg = " Your coupon code is <?php echo $post_title; ?>."+"<?php echo get_option('wcs_thankyou_msg'); ?>"
var wcs_scratch_after_text = "<?php echo $wcs_scratch_after_text;?>"
var discount_text = "<?php echo $post_excerpt;?>"
var offer_message = "<?php echo get_option('wcs_offer_text'); ?>"
var id = "<?php echo $post_id; ?>"
$('body').prepend('<div id="scratch_main"><div class="scratch_open">'+offer_message+'</div><div id="scratchmain" style="overflow:hidden;"><div class="scratchinnermain"><div class="scratch_text"><div class="scratch_close"></div><div class="scratchtxtstl">'+wcs_top_text+'</div><div class="scratchtxtstl2">'+wcs_top_text_h2+'</div><div id="wscratchpad" class="scratch_img"><div id="scratchBg" class="scratchpad"></div><div class="scratch_offer" style="display:none">'+discount_text+'</div></div></div><div class="scratch_form"><div id="thankYouDiv" style="display:none"><div class="scratch_close"></div><div class="form_txt1">'+wcs_thankyou_msg+'</div></div><div class="scratchinnermain" id="scratchinnermain" style="display:none"><div class="form_txt1">'+wcs_scratch_after_text+'</div><div class="scratch_form_main"><div id="returnmessage"></div><div id="ajax-loading-wp">Sending, Please wait...</div><form id="mycontactform" action="" method="post"><div class="clear_input"><div class="label">Name :</div><div class="wc_input"><input id="name" type="text" name="name"/></div></div><div class="clear_input"><div class="label">Email :</div><div class="wc_input"><input id="email" type="text" name="email"/><input id="submit" type="button" value="send" class="register_btn"/></div></div></form></div></div></div></div></div></div>');
});
The most amazing thing is that I don't know how I have done the above code in an easy way, pulling the data dynamic right after the body tag. I hope my answer will help someone and give a better idea. Live working example : http://codecanyon.net/item/woocommerce-coupon-scratch-discount/9235287
最神奇的是,我不知道我是如何以简单的方式完成上述代码的,在body标签之后立即动态拉取数据。我希望我的回答能帮助某人并提供更好的想法。现场工作示例:http: //codecanyon.net/item/woocommerce-coupon-scratch-discount/9235287
回答by Allen Gingrich
WordPress has now addressed this by adding the wp_body_openhook in version 5.2. You can now hook or inject into HTML body by doing:
WordPress 现在通过在 5.2 版中添加wp_body_open钩子来解决这个问题。您现在可以通过执行以下操作来挂钩或注入 HTML 正文:
<?php add_action('wp_body_open', function() { //some code to fire or inject HTML here }); ?>
Putting this in your functions.php file in your theme might be best for most basic users.
将其放在主题中的functions.php 文件中可能最适合大多数基本用户。