Inserisci HTML subito dopo il tag <body>
3 risposta
- voti
-
- 2016-03-07
Twenty Twelvenon ha hook che si attivanoimmediatamente dopoiltag di apertura
<body>
.Pertanto,neltuo childtheme cheestendeiltemagenitore Twenty Twelve,copia
header.php
nella directory deltuo childtheme.Apriilfile
header.php
neltuo childthemee subito dopoiltagbody di apertura aggiungi un action hook a cuipuoi agganciartitramiteiltuofilefunctions.php
.Adesempioneltuofile
twenty-twelve-child/header.php
:<body <?php body_class(); ?>> <?php do_action('after_body_open_tag'); ?>
Quindineltuofile
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');
Questo verrà quindi visualizzatoneltuo codice HTML come:
<body> <div>My Custom Content</div>
Lettura consigliata:
https://developer.wordpress.org/reference/functions/do_action/
AGGIORNAMENTO: LUGLIO 2019
Come commentato da Junaid Bhura da WordPress 5.2 unanuovafunzione di supporto deltema
wp_body_open
è statointrodotto che èintesoper l'uso comeper altrefunzioni di supportowp_head
ewp_footer
.Adesempio:
<html> <head> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <!-- BODY CONTENT HERE --> <?php wp_footer(); ?> </body> </html>
Neltuofilefunctions.php deltema (o opportunamente altrove)
function custom_content_after_body_open_tag() { ?> <div>My Custom Content</div> <?php } add_action('wp_body_open', 'custom_content_after_body_open_tag');
IMPORTANTE
Dovresti assicurarti che l'hookesista all'interno deltemain cui desideriiniettare,poichépotrebbenonessere ancora ampiamente adottato dalla comunità.
Se NON ,dovrai comunque seguireilprincipio diestendereiltema con un childtheme con l'eccezione che TU useresti :
<?php wp_body_open(); ?>
...invece di OR oltre a:
<?php do_action('after_body_open_tag'); ?>
Lettura consigliata:
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/
-
Grazie.Loproverò. Qualche altromodoperfarlo senza aggiungere header.php? Come qualcosa che usapreg_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
-
Potresti,peresempio,agganciarti a `template_include` o simili,tuttavia èfortemente sconsigliato difarloperchénon solo èinefficiente,mapotrebbeesseremoltoinaffidabile se qualcosa cambianelgenitore a causa di un aggiornamento.L'estensione deltemagenitore utilizzando untemafiglio è lamigliorepratica,èprevedibilee previsto,inoltreti dà ungrande controllo,come usare l'esempiomostrato sopra.Tuttavia,se vuoi usare `preg_replace`,dipende date ...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
-
Grazie ancora. Sto usandoiltuo codicee funzionabene. In realtà lamia domandaera solo dalpunto di vista di unbambino,ma semplicemente usandofunctions.php deltemafiglio. Ma comunque dopo averimplementatoiltuo codice,mi rendo conto che è diretto,leggeroe semplice.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
-
Apartire da WordPress 5.2,è ora disponibile untag standard: 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
-
@JunaidBhuragrazieperiltuo suggerimento su questo vecchiothread.Ho aggiornato l'esempio sopraperelaborare lanuovafunzione di supportoprincipalee hook chepossonoesserepresentinei temie,si spera,ampiamente adottatiin futuro.@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
Una soluzionemolto,molto,molto sporca sarebbe:
/* 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; }
-
+1grazieperiltrucco+1 thanks for trick
- 0
- 2019-10-02
- Vaibhav Gupta
-
- 2016-07-11
Aggiungi questo codicein functions.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');
-
Questo verràprodotto all'interno di ""non "".This will output inside the `` not ``.
- 13
- 2016-07-11
- cjbj
-
Bene,wp sembraessere abbastanzaintelligente,in realtà è aggiunto al corpo!Se è unmeta,verràiniettato all'interno dell'intestazione altrimenti verrà visualizzatonel corpo.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
-
Credo che @cjbj sia corretto.Sembra che siailbrowser a spostarlonel corpo,non WordPress.Visualizza la sorgentein unapaginain cui haiinserito latestataed è lì che latroverai.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
Sto usandoiltema Wordpress Twenty Twelve (un suofiglioperessereprecisi).
Voglio sapere comeinserire unpo 'di HTML subito dopo l'apertura del corpo,soloin functions.phpe non usando header.php.
Èpossibile?