Salta al contenuto
  • CATEGORIES
  • La Community Connect.gt
  • Presentati alla Community
  • News Ufficiali da Connect.gt
  • Internet News
  • A Scuola Di
  • Digital Marketing
  • Intelligenza Artificiale
  • SEO & GEO
  • Web Marketing e Content
  • Google Analytics e Web Analytics
  • Google Ads & Advertising
  • YouTube
  • TikTok, Instagram e altri Social
  • Google Adsense, Affiliazioni e Guadagno
  • E-Commerce
  • Coding e Sistemistica
  • Help Center: consigli per il tuo progetto
  • Hosting e Cloud
  • Coding
  • CMS & Piattaforme Self-Hosted
  • Annunci e Collaborazioni
  • Annunci Vendita e Acquisto: Pubblicità, Domini, Link e altro
  • Collaborazioni WEB e Lavoro: offro e cerco
  • Impresa, Fisco e Leggi
  • Consulenza Legale e Professioni Web
  • Consulenza Fiscale, Imprese, Startup e P.Iva
  • Gli Off Topic
  • PC, Smartphone e altri Hardware
  • Tutti i Software
  • GT Fetish Cafè
  • Discussioni
  • 0 Non letti 0
  • Recenti
  • Hashtag
  • Popolare
  • Utenti
  • Privacy
  • Termini e condizioni
  • © 2026 Search On Media Group S.r.l.
Collassa
Connect.gt
Il network Search On
Connect.gt WMF AI Festival Accademia Advanced SEO Tool AgentCy Social Media Strategies Search Tech
Registrati Accedi
Connect.gt
Stream Interest Categorie Hashtag
  1. La community italiana di SEO, marketing digitale e AI
  2. Categorie
  3. Coding e Sistemistica
  4. CMS & Piattaforme Self-Hosted
  5. child theme: errore fatale con funzioni

child theme: errore fatale con funzioni

Pianificato Fissato Bloccato Spostato CMS & Piattaforme Self-Hosted
11 Post 2 Autori 2.0k Visualizzazioni 1 Seguito
  • Da Vecchi a Nuovi
  • Da Nuovi a Vecchi
  • Più Voti
Rispondi
  • Risposta alla discussione
Effettua l'accesso per rispondere
Questa discussione è stata eliminata. Solo gli utenti con diritti di gestione possono vederla.
  • E
    E
    eugene
    scritto su ultima modifica di
    User Attivo 5
    #1
    child theme: errore fatale con funzioni

    Buonasera

    Sto personalizzando un child theme basato sul tema twentyten. Da quel che ho letto è possibile sovrascrivere anche le funzioni semplicemente creando nel child theme un documento functions.php ed inserendovi le nuove funzioni oppure quelle già presenti nel functions.php principale (in questo caso quello del tema twentyten) con le opportune modifiche. In questo modo le nuove funzioni da me inserite andranno a sovrascrivere o ad aggiungersi a quelle già esistenti.

    Nel fare questo finora non ho avuto alcun problema, poco fa però ho avuto la necessità di modificare la funzione twentyten_continue_reading_link e quindi, secondo quanto detto precedentemente, la ho copiata all'interno di functions.php del nuovo tema.

    Ecco cosa ho ottenuto:

    Fatal error: Cannot redeclare twentyten_continue_reading_link() (previously declared in /my-site/wp-content/themes/my-child-theme/functions.php:33) in /my-site/wp-content/themes/twentyten/functions.php on line 256

    quale mistero si nasconde dietro a tutto questo?

    Grazie.

    1 Risposta Ultima Risposta
    Miglior risposta 0
    0
    • E
      E
      elmanisero
      scritto su ultima modifica di
      User Attivo 5
      #2

      È possibile modificare alcune cose nel functions.php del tema child, utilizzando filtri, azioni, ecc ma pare che non si possano copiare e modificare se non a certe condizioni alcune funzioni del functions.php di TwentyTen, così almeno mi pare di aver capito nei link qui sotto.
      http://core.trac.wordpress.org/ticket/16500http://ottopress.com/2010/wordpress-protip-child-themes/

      1 Risposta Ultima Risposta
      Miglior risposta 0
      0
      • E
        E
        eugene
        scritto su ultima modifica di
        User Attivo 5
        #3

        Grazie mille ancora una volta.
        E' tutto spiegato più o meno qui:

        Overriding Functions

        One exception to the overriding mechanism of child themes has to do with the functions.php file. In a child theme situation, both functions.php files from both themes are loaded. This is necessary because elements of your parent theme might require pieces of the functions.php file to be loaded. This can make overriding functions in the parent theme tricky unless it?s written to allow you to do just that.
        The key to this is that the functions.php file of the child theme is loaded first. So if the parent theme is written in a manner WordPress calls ?pluggable?, then you can override those functions.
        In the twentyten theme?s functions.php file, several of the functions are defined like this:

        1 if ( ! function_exists( 'twentyten_admin_header_style' ) ) :
        2 function twentyten_admin_header_style() {
        3 ...
        4 }
        5 endif;

        That is a pluggable function. Basically, before it defines the function, it checks to see if the function is already defined. If the parent theme uses this mechanism, then a child theme can override this function by simply defining a function of the same name first. So all you have to do to change it is to copy the function into your child?s functions.php file and make your changes. When the parent theme loads, it will see that you already defined the function and continue on.
        Another way to override things is through the normal WordPress action and filter hook mechanisms. If a theme?s functions.php file uses those, then you can simply add your own hooked functions with different names. However, because the child?s functions.php file loads first, it can?t actually unhook things defined by the parent theme.
        The way to get around this is to use the after_setup_theme action hook. This action is called immediately after both functions.php files are loaded.
        For example, if I look at the twentyten theme, I?ll find this:

        1 function twentyten_excerpt_length( $length ) {
        2 return 40;
        3 }
        4 add_filter( 'excerpt_length', 'twentyten_excerpt_length' );

        I don?t want that, I want my excerpts to be 55 words instead. So I add this to my functions.php file:

        1 function my_excerpt_length( $length ) {
        2 return 55;
        3 }
        4 add_filter( 'excerpt_length', 'my_excerpt_length' );

        Whoops! It doesn?t work. Why not? Because I didn?t remove twentyten?s hook, and its filter is overriding mine. So I have to add this too:

        1 function my_undo_hooks( $length ) {
        2 remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );
        3 }
        4 add_action( 'after_setup_theme', 'my_undo_hooks' );

        And then it works. I?ve added my filter, and removed the one in twentyten. Voila.

        In breve, il documento functions.php del child theme viene caricato prima di quello del main theme. Il codice prevede due tipi di funzioni, quelle "aggiornabili" (pluggable) e quelle no.
        Se una funzione è "pluggable" è reso evidente dalla presenza della verifica relativa all'esistenza stessa della funzione in questione (if ( ! function_exists( 'nome_funzione' ) ) 🙂. In questo caso è possibile scrivere nel nostro functions.php la stessa funzione con le modifiche opportune e queste diverranno effettive.

        Diversamente la funzione non è modificabile attraverso il child theme. Per quel che ho dedotto si deve intervenire direttamente nel main theme modificando la funzione in questione oppure convertendola in "pluggable" per poi modificarla nel child theme.
        Insomma, in questo caso non si può fare a meno di intervenire direttamente nel tema principale.

        1 Risposta Ultima Risposta
        Miglior risposta 0
        0
        • E
          E
          elmanisero
          scritto su ultima modifica di
          User Attivo 5
          #4

          Eugene

          Insomma, in questo caso non si può fare a meno di intervenire direttamente nel tema principale.

          Veramente io ho capito un' altra cosa, come in questo caso in cui la funzione non è pluggable nel file function di twentyten

          function twentyten_excerpt_length( $length ) {    return 40;}add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
          

          Se voglio che la lunghezza dell' inizio post sia lunga 55 lettere invece di 40 non basta aggiungere questa mia funzione nel file function del child che non funzionerebbe

          function my_excerpt_length( $length ) {    return 55;}add_filter( 'excerpt_length', 'my_excerpt_length' );
          

          Ma devo aggiungere anche questa funzione al file function del child che serve a rimuovere quello di twenty ten in questo modo

          function my_undo_hooks( $length ) {    remove_filter( 'excerpt_length', 'twentyten_excerpt_length' );}add_action( 'after_setup_theme', 'my_undo_hooks' );
          
          1 Risposta Ultima Risposta
          Miglior risposta 0
          0
          • E
            E
            eugene
            scritto su ultima modifica di
            User Attivo 5
            #5

            In effetti questa faccenda degli hook l'avevo letta ma avevo capito fosse applicabile solo ad alcune funzioni. La mia proverbiale pigrizia e la voglia di fare tutto e subito hanno fatto si che non approfondissi l'argomento.
            Vediamo se ci ho capito qualcosa:
            non potendo modificare un funzione non "pluggabile" devo prima risciverla con un altro nome nel child theme e poi eliminare l'originale dal main theme, sempre tramite uno script nel child theme in modo da non apportare mai alcuna modifica al tema principale. Fin qui abbastanza chiaro, ma nell'esempio viene riportata una funzione che applica un filtro, mentre la funzione che devo modificare io è completamente differente:

            function twentyten_continue_reading_link() {
                return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) . '</a>';
            }
            

            Come dovrei procedere?
            Inoltre questa funzione viene anche richiamata da altre funzioni e non so se eseguire la suddetta operazione (ovvero eliminarla e sostituirla) può compromettere il corretto funzionamento dell'intero sistema.

            1 Risposta Ultima Risposta
            Miglior risposta 0
            0
            • E
              E
              elmanisero
              scritto su ultima modifica di
              User Attivo 5
              #6

              Neanche io so se può compromettere qualcos'altro. Cercando ho trovato questo, ma non so se funziona.

              function yoda_twentyten_continue_reading_link() {
                  return ' <a href="'. get_permalink() . '">' . __( 'Continua a leggere <span class="meta-nav">&rarr;</span>', 'twentyten-child' ) . '</a>';
              }
              
              function yoda_twentyten_auto_excerpt_more( $more ) {
                  return ' &hellip;' . yoda_twentyten_continue_reading_link();
              }
              
              function my_child_theme_setup() {
                  remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
                  add_filter( 'excerpt_more', 'yoda_twentyten_auto_excerpt_more' );
              }
              
              add_action( 'after_setup_theme', 'my_child_theme_setup' );
              
              1 Risposta Ultima Risposta
              Miglior risposta 0
              0
              • E
                E
                eugene
                scritto su ultima modifica di
                User Attivo 5
                #7

                Purtroppo in questo momento non sono al mio PC e quindi non posso verificare l'efficacia del codice da te indicato, tuttavia vediamo se ho ben capito la logica di tutto questo:

                definisco la nuova funzione twentyten_continue_reading_link assegnandole un nuovo nome:

                function yoda_twentyten_continue_reading_link() {
                    return ' <a href="'. get_permalink() . '">' . __( 'Continua a leggere <span class="meta-nav">&rarr;</span>', 'twentyten-child' ) . '</a>';
                }
                

                per sicurezza (o correttezza) definisco anche le funzioni che richiamano quella da me precedentemente definita:

                function yoda_twentyten_auto_excerpt_more( $more ) {
                    return ' &hellip;' . yoda_twentyten_continue_reading_link();
                }
                

                "informo" il sistema del fatto che invece di richiamare la funzione twentyten_auto_excerpt_more deve adesso richiamare yoda_twentyten_auto_excerpt_more:

                function my_child_theme_setup() {
                    remove_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
                    add_filter( 'excerpt_more', 'yoda_twentyten_auto_excerpt_more' );
                }
                

                non ho capito la sintassi ma credo che serva per rendere effettivo quanto dichiarato dalla precedente funzione:

                add_action( 'after_setup_theme', 'my_child_theme_setup' );
                

                In definitiva, se io modifico una funzione (alterandone il nome) devo far attenzione ad intervenire anche in tutte le altre funzioni che la richiamano.
                Attraverso remove_filter e add_filter "comunico" successivamente al sistema il nome della nuova funzione al posto di quella vecchia.

                Qualcosa l'ho azzeccata?

                1 Risposta Ultima Risposta
                Miglior risposta 0
                0
                • E
                  E
                  elmanisero
                  scritto su ultima modifica di
                  User Attivo 5
                  #8

                  In teoria si ma non garantisco nulla come sempre.

                  1 Risposta Ultima Risposta
                  Miglior risposta 0
                  0
                  • E
                    E
                    eugene
                    scritto su ultima modifica di
                    User Attivo 5
                    #9

                    Perfetto, non appena l'hosting torna attivo faccio dei test.

                    Grazie ancora per l'aiuto e per i preziosi chiarimenti.

                    1 Risposta Ultima Risposta
                    Miglior risposta 0
                    0
                    • E
                      E
                      eugene
                      scritto su ultima modifica di
                      User Attivo 5
                      #10

                      Una piccola curiosità: che cappero sta ad indicare (o a cosa serve) il termine che si trova tra virgolette ('twentyten-child' o 'twentyten' o 'twentyeleven' o qualsiasi altra cosa) e che si trova ovunque nei temi?

                      function yoda_twentyten_continue_reading_link() {
                          return ' <a href="'. get_permalink() . '">' . __( 'Continua a leggere <span class="meta-nav">&rarr;</span>', 'twentyten-child' ) . '</a>';
                      }
                      
                      1 Risposta Ultima Risposta
                      Miglior risposta 0
                      0
                      • E
                        E
                        elmanisero
                        scritto su ultima modifica di
                        User Attivo 5
                        #11

                        Sinceramente non lo so ma fa parte del codice di wordpress, se ti interessa ti rimando a questa lettura http://codex.wordpress.org/WordPress_Coding_Standards

                        1 Risposta Ultima Risposta
                        Miglior risposta 0
                        0

                        Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.

                        Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.

                        Con il tuo contributo, questo post potrebbe essere ancora migliore 💗

                        Registrati Accedi
                        Rispondi
                        • Risposta alla discussione
                        Effettua l'accesso per rispondere
                        • Da Vecchi a Nuovi
                        • Da Nuovi a Vecchi
                        • Più Voti


                        • Accedi

                        • Non hai un account? Registrati

                        • Accedi o registrati per effettuare la ricerca.
                        • Primo post
                          Ultimo post
                        Home Accedi Registrati
                        Menu
                        • CATEGORIES
                        • La Community Connect.gt
                        • Presentati alla Community
                        • News Ufficiali da Connect.gt
                        • Internet News
                        • A Scuola Di
                        • Digital Marketing
                        • Intelligenza Artificiale
                        • SEO & GEO
                        • Web Marketing e Content
                        • Google Analytics e Web Analytics
                        • Google Ads & Advertising
                        • YouTube
                        • TikTok, Instagram e altri Social
                        • Google Adsense, Affiliazioni e Guadagno
                        • E-Commerce
                        • Coding e Sistemistica
                        • Help Center: consigli per il tuo progetto
                        • Hosting e Cloud
                        • Coding
                        • CMS & Piattaforme Self-Hosted
                        • Annunci e Collaborazioni
                        • Annunci Vendita e Acquisto: Pubblicità, Domini, Link e altro
                        • Collaborazioni WEB e Lavoro: offro e cerco
                        • Impresa, Fisco e Leggi
                        • Consulenza Legale e Professioni Web
                        • Consulenza Fiscale, Imprese, Startup e P.Iva
                        • Gli Off Topic
                        • PC, Smartphone e altri Hardware
                        • Tutti i Software
                        • GT Fetish Cafè
                        • Discussioni
                        • Non letti 0
                        • Recenti
                        • Hashtag
                        • Popolare
                        • Utenti
                        • Privacy
                        • Termini e condizioni
                        • © 2026 Search On Media Group S.r.l.