Insérez du HTML juste après la balise <body>
3 réponses
- votes
-
- 2016-03-07
Twenty Twelven'a aucun hook qui se déclencheimmédiatement après labalise d'ouverture
<body>
.Par conséquent,dans votrethèmeenfant qui étend lethème Twenty Twelveparent,copiez le
header.php
dans le répertoire de votrethèmeenfant.Ouvrez lefichier
header.php
dans votrethèmeenfantet juste après labalisebody d'ouverture,ajoutez un crochet d'action que vouspouvezensuite accrocher via votrefichierfunctions.php
.Parexemple dans votrefichier
twenty-twelve-child/header.php
:<body <?php body_class(); ?>> <?php do_action('after_body_open_tag'); ?>
Puis dans votrefichier
twenty-twelve-child/functions.php
:function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
Cela s'afficheraensuite dans votre HTML sous laforme:
<body> <div>My Custom Content</div>
Lecture recommandée:
https://developer.wordpress.org/reference/functions/do_action/
MISE À JOUR: JUILLET 2019
Tel que commentépar Junaid Bhura de WordPress 5.2 unenouvellefonction d'assistance dethème
wp_body_open
a étéintroduitet est destiné à être utilisé comme d'autresfonctions d'assistancewp_head
etwp_footer
.Parexemple:
<html> <head> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <!-- BODY CONTENT HERE --> <?php wp_footer(); ?> </body> </html>
Dans votrefichier dethèmefunctions.php (ou convenablement ailleurs)
function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('wp_body_open', 'custom_content_after_body_open_tag');
IMPORTANT
Vous devez vous assurer que le hookexiste dans lethème que vous souhaitezinjecter cariln'estpeut-êtrepasencore largement adoptépar la communauté.
Si NON ,vous devreztoujours suivre leprincipe de l'extension duthème avec unthèmeenfant ,à l'exception que VOUS utiliseriez :
<?php wp_body_open(); ?>
... au lieu de ORen plus de:
<?php do_action('after_body_open_tag'); ?>
Lecture recommandée:
https://developer.wordpress.org/reference/functions/wp_body_open/
Twenty Twelve does not have any hooks that fire immediately after the opening
<body>
tag.Therefore you in your child theme which extends the parent Twenty Twelve theme, copy the
header.php
across to your child theme directory.Open the
header.php
file in your child theme and just after the opening body tag add an action hook which you can then hook onto via yourfunctions.php
file.For example in your
twenty-twelve-child/header.php
file:<body <?php body_class(); ?>> <?php do_action('after_body_open_tag'); ?>
Then in your
twenty-twelve-child/functions.php
file:function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('after_body_open_tag', 'custom_content_after_body_open_tag');
This will then render in your HTML as:
<body> <div>My Custom Content</div>
Recommended reading:
https://developer.wordpress.org/reference/functions/do_action/
UPDATE: JULY, 2019
As commented by Junaid Bhura from WordPress 5.2 a new theme helper function
wp_body_open
has been introduced that is intended for use as per the likes of other helper functionswp_head
andwp_footer
.For example:
<html> <head> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <!-- BODY CONTENT HERE --> <?php wp_footer(); ?> </body> </html>
In your theme functions.php file (or suitably elsewhere)
function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('wp_body_open', 'custom_content_after_body_open_tag');
IMPORTANT
You should ensure that the hook exists within the theme that you are wanting to inject-into as this may not be widely adopted by the community, yet.
If NOT, you will still need to follow the principle of extending the theme with a child theme with the exception that YOU would use:
<?php wp_body_open(); ?>
...instead of OR in addition to:
<?php do_action('after_body_open_tag'); ?>
Recommended reading:
https://developer.wordpress.org/reference/functions/wp_body_open/
-
Merci.J'essaierai ceci. Une autrefaçon de lefaire sans ajouter le header.php? Comme quelque chose qui utilisepreg_replace?Thanks. I will try this. Any other way to do it without adding the header.php? Such as something using preg_replace?
- 0
- 2016-03-07
- Ramanan
-
Vouspourriez,parexemple,vous accrocher à `template_include` ou similaire,maisilestfortement déconseillé de lefaire carnon seulement c'estinefficace,mais celapourrait êtretrèspeufiable si quelque chose change dans leparenten raison d'unemise àjour.L'extension duthèmeparent à l'aide d'unthèmeenfantest lameilleurepratique,elleestprévisibleet attendue,en pluselle vous donnebeaucoup de contrôle,commeen utilisant l'exemple ci-dessus.Cependant,si vous voulez utiliser `preg_replace`,c'est à vous de décider ...You could, if for example you hook onto `template_include` or similar, however it is strongly advised against doing that because not only is it inefficient, it could be very unreliable if something changes in the parent due to an updatge. Extending the parent theme using a child theme is the best practice, it is predictable and expected, plus it gives you a great deal of control, such as using the example shown above. However if you want to use `preg_replace` that's up to you...
- 1
- 2016-03-07
- Adam
-
Encoremerci. J'utilise votre codeet fonctionnebien. Enfait,ma question était uniquement dupoint de vue de l'enfant,mais simplementen utilisantfunctions.php duthèmeenfant. Mais detoutefaçon après avoirimplémenté votre code,je me rends compte que c'est simple,légeret simple.Thanks once again. I am using your code and works nice. Actually my question was from a child viewpoint only but simply using functions.php of the child theme. But anyway after implementing your code, I realize it's straightforward, lightweight and simple.
- 0
- 2016-03-07
- Ramanan
-
Depuis WordPress 5.2,unebalise standardest désormais disponible: wp_body_open (): https://developer.wordpress.org/reference/functions/wp_body_open/Since WordPress 5.2, a standard tag is now available: wp_body_open() : https://developer.wordpress.org/reference/functions/wp_body_open/
- 2
- 2019-07-26
- Junaid Bhura
-
@JunaidBhuramercipour votre suggestion sur cet ancienfil.J'aimis àjour l'exemple ci-dessuspourpréciser lanouvellefonction d'assistance debaseet accrocher quemau soitprésent dans lesthèmeset,espérons-le,largement adopté à l'avenir.@JunaidBhura thank you for your suggestion on this old thread. I have updated the example above to elaborate as to the new core helper function and hook that mau be present in themes and hopefully widely adopted in the future..
- 0
- 2019-07-26
- Adam
-
- 2017-07-21
Une solutiontrès,très,très sale serait:
/* Insert tracking code or other stuff 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; }
A very, very, very dirty solution would be:
/* Insert tracking code or other stuff 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; }
-
+1mercipour l'astuce+1 thanks for trick
- 0
- 2019-10-02
- Vaibhav Gupta
-
- 2016-07-11
Ajoutez ce code dansfunctions.php
function my_function() { echo'<div id="from_my_function"></div>'; } add_action('wp_head', 'my_function');
Add this code in functions.php
function my_function() { echo'<div id="from_my_function"></div>'; } add_action('wp_head', 'my_function');
-
Cela affichera à l'intérieur du ``pas ``.This will output inside the `` not ``.
- 13
- 2016-07-11
- cjbj
-
Ehbien wp semble être assezintelligent,ilesten fait ajouté au corps!S'il s'agit d'uneméta,elle serainjectée à l'intérieur de l'en-tête,sinonelle sera affichée dans le corps.Well wp seems to be smart enough, it is actually added to the body! If it's a meta then it will be injected inside the header otherwise it will be displayed in the body.
- 0
- 2018-02-01
- Mohamed Salem Lamiri
-
Jepense que @cjbjest correct.On dirait que c'est lenavigateur qui le déplace vers le corps,pas WordPress.Affichez la source sur unepage où vous l'avezinsérée dans latêteet c'est là que vous latrouverez.I believe @cjbj is correct. It's looks like it's the browser that's moving it to the body, not WordPress. View source on a page where you've inserted in to the head and that's where you'll find it.
- 1
- 2018-03-29
- Danger
J'utilise lethème Wordpress Twenty Twelve (unenfant de celui-cipour êtreprécis).
Je veux savoir commentinsérer du HTMLjuste après l'ouverture du corps,dansjustefunctions.phpet sans utiliser header.php.
Est-cepossible?