Come aggiungere codice a Header.php in un tema figlio?
3 risposta
- voti
-
- 2012-02-27
Vorrei collegarmi all'azione
wp_head
.Lomettereiin unpluginin modo da astrarlo daltuo livello dipresentazione.Ciò consente la scalabilitàe lamodifica deitemi.Ciòpreviene anche qualsiasi danno collaterale all'analisi se viene saltato unpassaggionellamigrazione da untema a quello successivo.add_action('wp_head', 'wpse_43672_wp_head'); function wpse_43672_wp_head(){ //Close PHP tags ?> ADD YOUR PLAIN HTML CODE HERE <?php //Open PHP tags }
I would hook into the
wp_head
action. I would place this in a plugin so as to abstract it from your presentation layer. This allows for scalability and changing of themes. This also prevents any analytics collateral damage if a step is missed in migration from one theme to the next.add_action('wp_head', 'wpse_43672_wp_head'); function wpse_43672_wp_head(){ //Close PHP tags ?> ADD YOUR PLAIN HTML CODE HERE <?php //Open PHP tags }
-
Grazie.Non ho alcunaesperienzanella creazione di unplug-in. Ho circa 5pezzi di codice che vorrei aggiungerenell'intestazione.Avreibisogno di collegare ciascuno diessi?Thanks. I don't have any experience in making a plug in. I have about 5 pieces of code I'd like to add into the header. Would I need to make a plug in for each of them?
- 0
- 2012-02-27
- Rick Smith
-
@ RickSmith ho aggiuntoilformato delplugin alpost sopra@RickSmith I added the plugin format to the post above
- 0
- 2012-02-27
- Brian Fegter
-
Sia che si utilizzi untemapadre,untemafiglio o qualsiasi altrotema,l'approccio * corretto * consistenell'inseriretale codicein una richiamata,agganciato all'action hook appropriato.Potrestimettere questo codicenelfile `functions.php` deltema,o,ancorameglioe come ha suggerito Brian,metterloin [un **plug-in specificoperil sito **] (http://ottopress.com/2011/creazione-di-un-plug-in-snippet-specificoperil sito/).Whether using a Parent Theme, Child Theme, or any Theme whatsoever, the *correct* approach is to put such code in a callback, hooked into the appropriate action hook. You could put this code in the Theme's `functions.php` file, or, even better and as Brian has suggested, put it in [a **site-specific Plugin**](http://ottopress.com/2011/creating-a-site-specific-snippets-plugin/).
- 4
- 2012-02-27
- Chip Bennett
-
@BrianFegter Graziemille.Lavorare coni temi Php/child ènuovoperme,quindi stoimparandomanmano cheprocedo. Grazieper lapazienza. :) Nelmio vecchiotema,sonoentratoin wordpress admin,poinell'editore l'ho aggiunto all'intestazione.
Èilmodomiglioreper aggiungeretutto questo alplug-in? @BrianFegter Thanks so much. Working with Php / child themes is new to me, so I"m learning as I go. Thanks for your patience. :) In my old theme, I went into wordpress admin, than to the editor and added this to the header. Is the better way to add all of that to the plug in?- 0
- 2012-02-27
- Rick Smith
-
@RickSmith Nessunproblema.Eccoperchéesiste questoforum.:)@RickSmith No problem. That's why this forum exists. :)
- 0
- 2012-02-27
- Brian Fegter
-
@BrianFegter Ok,quindi ho appenainseritoil codice sopramenzionatoin quelplugine quandotenta di attivarsi ottengo questoerrore.Cosapensi che stia sbagliando?Errore di analisi:errore di sintassi,"<"imprevistoin/home/downsynd/public_html/wpdallas.com/test/wp-content/plugins/Ricks-Code/Rick-Widget.php alla riga 22@BrianFegter Ok, so I just placed the above mentioned code into that plugin, and when it tries to active I get this error. What do you think I'm doing wrong? Parse error: syntax error, unexpected '<' in /home/downsynd/public_html/wpdallas.com/test/wp-content/plugins/Ricks-Code/Rick-Widget.php on line 22
- 0
- 2012-02-27
- Rick Smith
-
@RickSmithpuoipostare *tutto * di `Rick-Widget.php`in un [PasteBin] (http://pastebin.com)e collegarlo?@RickSmith can you post *all* of `Rick-Widget.php` into a [PasteBin](http://pastebin.com), and link it?
- 0
- 2012-02-27
- Chip Bennett
-
@RickSmith come Chip hapostato,qualsiasi cosa specifica del sito dovrebbe andarein quell'unicoplugin.@RickSmith like Chip posted, anything site specific should go in that one plugin.
- 0
- 2012-02-27
- Brian Fegter
-
@BrianFegter Ecco qua.http://pastebin.com/cLLkdiDv@BrianFegter Here ya go. http://pastebin.com/cLLkdiDv
- 0
- 2012-02-27
- Rick Smith
-
@BrianFegter Quelpastbin è scaduto.eccone unonuovo.http://pastebin.com/kmmKkSDS@BrianFegter That pastbin expired. here is a new one. http://pastebin.com/kmmKkSDS
- 0
- 2012-02-27
- Rick Smith
-
@RickSmith Vedi lamia ultimamodifica.Latua sintassi PHP è sbagliata.Devi usareecho o chiuderei tuoitagphp se vuoipubblicare HTMLgrezzoin unafunzione.`?> IL TUO HTML Php`@RickSmith See my latest edit. Your PHP syntax is wrong. You have to use echo or close out your php tags if you want to post raw HTML in a function. `?> YOUR HTML
- 0
- 2012-02-27
- Brian Fegter
@BrianFegter Grazie amico!Capito!Hafunzionato.Un'altra domanda veloce.Nelmio vecchiotema avevo unpo 'di scriptjava di compravendita che ho aggiunto alla sezionepiè dipagina deltema (mi è stato detto difarloin modo cheilmiotema venisse caricatoperprimo.) Sarebbegiusto aggiungere quel codice qui,o dovrei aggiungerlo ailpiè dipagina?)@BrianFegter Thanks man! I got it! That worked. One more quick question. In my old theme I had some buy sell java script that I added to the footer section of theme (I was told to do that so my theme loaded first.) Would it be ok to add that code here, or should I add it to the footer? )- 0
- 2012-02-27
- Rick Smith
Puoi semplicemente aggiungere un'altra azionee richiamareper wp_footerYou can just add another action and callback for wp_footer- 0
- 2012-02-27
- Brian Fegter
@BrianFegter ok,suggerisci dimetteretutta questa roba apiè dipagina?Come laformica di Google.strumentiperi webmaster,ecc ...?Non sono sicuro di quale sia lamigliorepratica ...@BrianFegter ok, Do you suggest putting all of this stuff in the footer? Like the google ant. webmaster tools, etc...? Not sure what the best practice is....- 0
- 2012-02-27
- Rick Smith
@RickSmith Èfuori dalmiogrado dipaga.lol@RickSmith That's out of my pay grade. lol- 0
- 2012-02-27
- Brian Fegter
@BrianFegter Ah,ah..grazie!:) Realizzi siti wordpress?@BrianFegter Ha,ha..thanks! :) Do you build wordpress sites?- 0
- 2012-02-27
- Rick Smith
@BrianFegter Ok,quindi ora sto cercando di aggiungere unpo 'dijava script a quelplug-in. Andando sultuoesempio sopra,comeposso aggiungerejava script adesso?@BrianFegter Ok, so now I'm trying to add some java script to that plug in. Going on your above example, how do I add java script to it?- 0
- 2012-02-27
- Rick Smith
Siprega di vedere lamia ultimamodifica.:)Please see my latest edit. :)- 0
- 2012-02-27
- Brian Fegter
@BrianFegter Grazieper aver dedicato deltempo ad aiutarmi con questo.Ecco un contenitoreper lapasta,penso che sto ancorafacendo qualcosa di sbagliato.:) http://pastebin.com/iT0bJjGE@BrianFegter Thanks for taking the time to help me with this. Here is a paste bin, I think I'm still doing something wrong. :) http://pastebin.com/iT0bJjGE- 1
- 2012-02-27
- Rick Smith
lasciaci [continuare questa discussionein chat] (http://chat.stackexchange.com/rooms/2630/discussion-between-brian-fegter-and-rick-smith)let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/2630/discussion-between-brian-fegter-and-rick-smith)- 1
- 2012-02-27
- Brian Fegter
- 2012-02-27
Permodificare l'intestazionein untemafiglio,copiailfile header.php daltemaprincipaleneltemafiglioe quindimodificalo.WordPress vedrà che hai un header.phpneltuotemafiglioe lo userà alposto deltemaprincipale header.php
Qualsiasifilemodello cheinseriscineltuotemafiglio avrà lapriorità sullo stessofileneltemaprincipale quando viene richiamato da WordPress.
Tutto ciò che vaneltag dovrebbeesserefatto usando qualcosa come lafunzionenella risposta di Brians.Se è specifico deltema,puoiinserirloin unfile chiamatofunctions.phpnella cartella deltema senzapassaggi aggiuntivi.
To modify the header in a child theme, copy the header.php from the parent theme into the child theme and then modify it. WordPress will see that you have a header.php in your child theme and use that instead of the parent theme header.php
Any template files you put in your child theme will take priority over the same file in the parent theme when called by WordPress.
Anything that goes in the tag should be done using something such as the function in Brians answer. If it's theme specific, you can put it in a file called functions.php in your theme folder without any extra steps.
-
Tom,grazie.Quindi,se hobisogno diinstallare 5 diversiframmenti di codicenelfile header.php,posso solo creare unplugine installarlo?Avrei ancorabisogno di copiareilfile header.phpnelmio childtheme?Tom, thanks. So if I need to install 5 different snips of code in the header.php, could I just make one plugin and install that? Would I still need to copy the header.php file into my child theme?
- 0
- 2012-02-27
- Rick Smith
-
Sì,non c'èmotivo di creare 5pluginYes there is no reason to make 5 plugins
- 0
- 2012-02-27
- Tom J Nowell
-
@RickSmith Se stai astraggendoin unplugin,non c'èmotivo di copiare su header.php.:)@RickSmith If you are abstracting to a plugin, there's no reason to copy over header.php. :)
- 0
- 2012-02-27
- Brian Fegter
-
Ilproblema con questa soluzione è che quandoiltema viene aggiornato,perderai le correzioniin header.phpfatte dall'autore.The problem with this solution is that when the theme is updated, you will miss out on fixes in header.php made by the author.
- 2
- 2016-02-24
- Knocks X
-
WordPress sovrascrive l'interofile o aggiunge solonuovo contenuto alfileprincipale?does WordPress override the whole file, or is it just appending new content to the parent file?
- 0
- 2020-05-31
- Ooker
- 2014-08-13
Grazie a Brian Fegter . Se questa risposta aiuta,valuta la risposta di Brian proprio qui sopra.
Questo è unesempio completamentefunzionale di come aggiungereelementi all '"intestazione"tramiteilproprioplugin. In questo caso,sto aggiungendo leproprietà di Facebook Open Graphperi pulsanti Condividie Mipiace.
Basta creare unfile PHP conilnome specificatoin "Plugin Script" all'inizio del codice diesempio,inserirloin una cartella con lo stessonome senzaestensione,ovviamente,e copiare questa cartellanella destinazione "/wp -content/plugins ".
Quindiin "Wordpress",aggiorna "Plugin"e vedraiiltuonuovoplugininstallato. Basta attivarloe letuepagineinizieranno a contenerei metadati di Open Graph Facebooke Twitter.
MOLTO IMPORTANTE: ilfile PHP deveessere codificatoin UTF-8 senza BOMe non deve contenere assolutamente alcun carattere allafine. Devegarantire questo.
<?php /* Plugin Name: My Facebook Open Graph Protocol Plugin Script: my-facebook-open-graph-protocol.php Plugin URI: Description: Add Facebook Open Graph Protocol to header Author: Diego Soto (Thanks to Brian Fegter) Donate Link: License: GPL Version: 0.1-alpha Author URI: https://wordpress.stackexchange.com/questions/43672/how-to-add-code-to-header-php-in-a-child-theme Text Domain: myfogp Domain Path: languages/ */ /* Copyright 2014 Diego Soto (http://disientoconusted.blogspot.com.ar/) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ add_action('wp_head', 'wpse_43672_wp_head'); function wpse_43672_wp_head(){ $title = get_the_title() ." ‹ ". get_bloginfo( "name", "display" ); $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), array( 90,55 ), false, "" ); $face_metad = get_post_meta(get_the_ID(), "metadescription", true); $twitter_metad = get_post_meta(get_the_ID(), "metadescription140", true); if (empty($twitter_metad)) $twitter_metad = $face_metad; //Close PHP tags ?> <meta property="og:title" content="<?php echo esc_attr($title); ?>" /> <meta property="og:image" content="<?php echo esc_attr($src[0]); ?>" /> <meta property="og:url" content="<?php the_permalink(); ?>" /> <meta property="og:description" content="<?php if (!empty($face_metad)) echo esc_attr($face_metad); else the_excerpt(); ?>" /> <meta name="twitter:title" content="<?php echo esc_attr($title); ?>" /> <meta name="twitter:image" content="<?php echo esc_attr($src[0]); ?>" /> <meta name="twitter:url" content="<?php the_permalink(); ?>" /> <meta name="twitter:description" content="<?php if (!empty($twitter_metad)) echo esc_attr($twitter_metad); else the_excerpt(); ?>" /> <?php //Open PHP tags } ?>
Chiunque siainteressato allefunzionalità delplugin.
-
Iltitolo sarà la concatenazione delnome dellapagina corrente eilnome del sito.
-
Seesiste un campopersonalizzato chiamato "metadescription",ilpluginprova perprendere la descrizione da questo campo. Altrimenti,prendiilfile descrizione dall'estratto.
-
Come l'immagine,ilplugin cerca di utilizzare laminiatura del contenuto immaginenellapagina.
Thanks to Brian Fegter. If this answer helps, please rate for Brian's answer right here above.
This is a fully functional example of how to add things to the "header" by its own plugin. In this case, I am adding the properties of Facebook Open Graph for the Share and Like buttons.
Just create a PHP file with the name specified in "Plugin Script" at the beginning of the sample code, place it in a folder with the same name without the extension, obviously, and copy this folder to the destination "/ wp-content / plugins".
Then within "Wordpress", refresh "Plugins" and you'll see your new plugin installed. Just Activate it, and your pages will begin to contain the metadata of Open Graph Facebook and Twitter.
VERY IMPORTANT: The PHP file must be encoded in UTF-8 without BOM, and should have absolutely no character at the end. Must ensure this.
<?php /* Plugin Name: My Facebook Open Graph Protocol Plugin Script: my-facebook-open-graph-protocol.php Plugin URI: Description: Add Facebook Open Graph Protocol to header Author: Diego Soto (Thanks to Brian Fegter) Donate Link: License: GPL Version: 0.1-alpha Author URI: https://wordpress.stackexchange.com/questions/43672/how-to-add-code-to-header-php-in-a-child-theme Text Domain: myfogp Domain Path: languages/ */ /* Copyright 2014 Diego Soto (http://disientoconusted.blogspot.com.ar/) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ add_action('wp_head', 'wpse_43672_wp_head'); function wpse_43672_wp_head(){ $title = get_the_title() ." ‹ ". get_bloginfo( "name", "display" ); $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), array( 90,55 ), false, "" ); $face_metad = get_post_meta(get_the_ID(), "metadescription", true); $twitter_metad = get_post_meta(get_the_ID(), "metadescription140", true); if (empty($twitter_metad)) $twitter_metad = $face_metad; //Close PHP tags ?> <meta property="og:title" content="<?php echo esc_attr($title); ?>" /> <meta property="og:image" content="<?php echo esc_attr($src[0]); ?>" /> <meta property="og:url" content="<?php the_permalink(); ?>" /> <meta property="og:description" content="<?php if (!empty($face_metad)) echo esc_attr($face_metad); else the_excerpt(); ?>" /> <meta name="twitter:title" content="<?php echo esc_attr($title); ?>" /> <meta name="twitter:image" content="<?php echo esc_attr($src[0]); ?>" /> <meta name="twitter:url" content="<?php the_permalink(); ?>" /> <meta name="twitter:description" content="<?php if (!empty($twitter_metad)) echo esc_attr($twitter_metad); else the_excerpt(); ?>" /> <?php //Open PHP tags } ?>
Anyone who is interested in the functionality of the plugin.
The title will be the concatenation of the name of the current page and the site name.
If a custom field called "metadescription" exists, the plugin tries to take the description from this field. Otherwise, take the description from the excerpt.
As the image, the plugin tries to use the thumbnail of the featured image on the page.
Sto creando un childthemeper laprima voltae avevo alcune domande sul codice aggiunto all'intestazione.
In untemanon child c'è un certo codice che aggiungo almiofile header.php comegoogle analytics,google webmastertools,buy sell ads,Facebook opengraph,ecc ....
Come siesegue questa operazionein un childtheme?Crei unfile header.phpneltuo childtheme?In caso affermativo,come sifa?È lo stesso @import che ho usato sul css?
Grazie.