Come generare/aggiornare una sitemap XML senza plugin?

20 apr 2011, 17:40:19
Visualizzazioni: 24K
Voti: 4

Mi piace programmare tutto manualmente nei miei siti WordPress, senza utilizzare plugin. Esiste un modo per generare o aggiornare la sitemap ogni volta che pubblico/aggiorno un articolo su uno dei miei blog Multisite, senza utilizzare plugin?

4
Commenti

Ti rendi conto che non c'è una vera differenza tra "hardcode" e un plugin a parte il fatto che i plugin sono portatili e quindi più facili da lavorare?

Wyck Wyck
20 gen 2012 20:53:51

Voto negativo per la restrizione "senza un Plugin".

Chip Bennett Chip Bennett
20 feb 2012 19:47:49

Non voler aggiungere UN ALTRO plugin su un sito di un CLIENTE è perfettamente accettabile per un compito così semplice. È vero che il codice è lo stesso ma forse l'OP non lo sapeva

JasonDavis JasonDavis
23 mag 2013 06:45:45

come ha detto chip è una domanda inutile, inoltre, creare un codice per la sitemap che non causi problemi al sito quando viene generata, non è banale e una buona risposta non si adatterebbe al formato del sito

Mark Kaplun Mark Kaplun
25 mag 2017 10:10:48
Tutte le risposte alla domanda 5
0

Il seguente codice funziona subito. La tua sitemap sarà visibile all'indirizzo: https://your-website-name.com/sitemap.xml

Ogni volta che crei o aggiorni una pagina, articolo o custom post type apparirà nella sitemap. Assicurati di aggiungere il nome del tuo custom post type:

add_action( 'publish_post', 'ow_create_sitemap' );
add_action( 'publish_page', 'ow_create_sitemap' );
add_action( 'save_post',    'ow_create_sitemap' );

function ow_create_sitemap() {
    $postsForSitemap = get_posts(array(
        'numberposts' => -1,
        'orderby'     => 'modified',
        // 'custom_post' dovrebbe essere sostituito con il tuo Custom Post Type (uno o più)
        'post_type'   => array( 'post', 'page', 'custom_post' ),
        'order'       => 'DESC'
    ));

    $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
    $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';

    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post );

        $postdate = explode( " ", $post->post_modified );

        $sitemap .= '<url>'.
                    '<loc>' . get_permalink( $post->ID ) . '</loc>' .
                    '<lastmod>' . $postdate[0] . '</lastmod>' .
                    '<changefreq>monthly</changefreq>' .
                    '</url>';
      }

    $sitemap .= '</urlset>';

    $fp = fopen( ABSPATH . 'sitemap.xml', 'w' );

    fwrite( $fp, $sitemap );
    fclose( $fp );
}
3 gen 2019 06:18:53
4

Non so se funzioni su multisite, ma per me sta funzionando perfettamente in un'installazione singola di WordPress.

Quando crei/aggiorni qualsiasi articolo o pagina, genererà un file sitemap.xml e aggiornerà i link (URL) con i più recenti per primi (ultima modifica).

Copia e incolla il codice qui sotto nel file functions.php del tuo tema attivo:

/* funzione per creare il file sitemap.xml nella directory principale del sito */        
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");  
add_action( "save_post", "eg_create_sitemap" );   
function eg_create_sitemap() {
    $postsForSitemap = get_posts( array(
        'numberposts' => -1,
        'orderby'     => 'modified',
        'post_type'   => array( 'post', 'page' ),
        'order'       => 'DESC'
    ) );
    $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
    $sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";    
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post );   
        $postdate = explode( " ", $post->post_modified );   
        $sitemap .= "\t" . '<url>' . "\n" .
            "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
            "\n\t\t" . '<lastmod>' . $postdate[0] . '</lastmod>' .
            "\n\t\t" . '<changefreq>monthly</changefreq>' .
            "\n\t" . '</url>' . "\n";
    }     
    $sitemap .= '</urlset>';     
    $fp = fopen( ABSPATH . "sitemap.xml", 'w' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}
19 dic 2011 12:39:02
Commenti

Non credo che funzionerà su multisite - sta scrivendo sitemap.xml nella stessa posizione del file sul server, quindi ci sarebbe solo 1 file sitemap.xml effettivo che verrebbe sovrascritto ogni volta che un blog apporta una modifica...

Sators Sators
19 mag 2017 21:50:11

Interrogare tutti i post del sito ad ogni salvataggio è un modo sicuro per far cadere un sito che ha una quantità non banale di contenuti. Inoltre renderà il salvataggio di nuovi post sempre più lento con ogni pubblicazione

Mark Kaplun Mark Kaplun
25 mag 2017 10:14:13

@MarkKaplun - Cosa consiglieresti come alternativa all'interrogazione di tutti i post?

Motivated Motivated
8 set 2022 11:37:24

@Motivated non lo so, dipende da cosa hai bisogno di fare. (le sitemap fanno parte del core dalla versione 5.2, quindi almeno questa risposta non è rilevante per questo caso specifico)

Mark Kaplun Mark Kaplun
8 set 2022 16:14:04
2

Prima di utilizzare il codice fornito nella risposta di w3uiguru, ho dovuto apportare alcuni miglioramenti che seguono lo standard accettato per i file XML. Il codice è riportato di seguito:

/* funzione per creare il file sitemap.xml nella directory root del sito */
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
    if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) { 
        $tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) ); 
    } else { 
        $tempo = get_option( 'gmt_offset' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
    $postsForSitemap = get_posts( array(
        'numberposts' => -1,
        'orderby'     => 'modified',
        'post_type'   => array( 'post', 'page' ),
        'order'       => 'DESC'
    ) );
    $sitemap .= '<?xml version="1.0" encoding="UTF-8"?>' . '<?xml-stylesheet type="text/xsl" href="' . 
        esc_url( home_url( '/' ) ) . 'sitemap.xsl"?>';
    $sitemap .= "\n" . '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
    $sitemap .= "\t" . '<url>' . "\n" .
        "\t\t" . '<loc>' . esc_url( home_url( '/' ) ) . '</loc>' .
        "\n\t\t" . '<lastmod>' . date( "Y-m-d\TH:i:s", current_time( 'timestamp', 0 ) ) . $tempo . '</lastmod>' .
        "\n\t\t" . '<changefreq>daily</changefreq>' .
        "\n\t\t" . '<priority>1.0</priority>' .
        "\n\t" . '</url>' . "\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\t" . '<url>' . "\n" .
            "\t\t" . '<loc>' . get_permalink( $post->ID ) . '</loc>' .
            "\n\t\t" . '<lastmod>' . $postdate[0] . 'T' . $postdate[1] . $tempo . '</lastmod>' .
            "\n\t\t" . '<changefreq>Weekly</changefreq>' .
            "\n\t\t" . '<priority>0.5</priority>' .
            "\n\t" . '</url>' . "\n";
    }
    $sitemap .= '</urlset>';
    $fp = fopen( ABSPATH . "sitemap.xml", 'w' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}
23 giu 2014 01:01:39
Commenti

Potresti per favore spiegare il tuo codice e inviare una [modifica] per formattarlo in modo più leggibile? Grazie

kaiser kaiser
23 giu 2014 03:10:50

@locutor-antonio-cezar @gabriel Devi cambiare Weekly in weekly a causa di Errore 1840: Elemento '{http://www.sitemaps.org/schemas/sitemap/0.9}changefreq': [facet 'enumeration'] Il valore 'Weekly' non è un elemento dell'insieme {'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'}.

Iurie Iurie
8 ago 2015 00:28:27
0

WordPress include una funzionalità integrata per le mappe del sito XML a partire dalla versione 5.5 che puoi personalizzare.

Vedi questo articolo del blog per maggiori dettagli e alcuni esempi rapidi. Dai un'occhiata qui per vedere una lista di hook che puoi utilizzare per personalizzare la mappa del sito predefinita.

10 mag 2024 02:30:25
1

Ho modificato un po' il codice di @locutor-antonio-cezar poiché stavo cercando un caso d'uso molto specifico. Avevo bisogno di una sitemap scritta appositamente per Google News. Cosa c'è di diverso? L'intero markup segue le regole. Nel mio caso specifico, ho limitato il numero di articoli a 20. Inoltre, gli articoli più vecchi di 2 giorni scompaiono. Forse qualcuno ne avrà bisogno:

/* funzione per creare il file sitemap.xml nella directory root del sito */
// add_action("publish_post", "eg_create_sitemap");
// add_action("publish_page", "eg_create_sitemap");
add_action( "save_post", "eg_create_sitemap" );
function eg_create_sitemap() {
    if ( str_replace( '-', '', get_option( 'gmt_offset' ) ) < 10 ) { 
        $tempo = '-0' . str_replace( '-', '', get_option( 'gmt_offset' ) ); 
    } else { 
        $tempo = get_option( 'gmt_offset' ); 
    }
    if( strlen( $tempo ) == 3 ) { $tempo = $tempo . ':00'; }
    $postsForSitemap = get_posts( array(
        'numberposts' => 20,
        'orderby'     => 'modified',
        'post_type'   => array( 'post', 'page' ),
        'order'       => 'DESC',
        'date_query' => array(
            'after' => date('Y-m-d', strtotime('-2 days')) 
        )
    ) );
    $sitemap .= '<?xml version="1.0" encoding="UTF-8"?>';
    $sitemap .= "\n" . '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">' . "\n";
    foreach( $postsForSitemap as $post ) {
        setup_postdata( $post);
        $postdate = explode( " ", $post->post_modified );
        $sitemap .= "\t" . "<url>" . "\n";
        $sitemap .= "\t\t" . "<loc>" . get_permalink( $post->ID ) . '</loc>';
        $sitemap .= "\t\t" . '<news:news>' . "\n";
        $sitemap .= "\t\t\t" . '<news:publication>' . "\n";
        $sitemap .= "\t\t\t\t" . '<news:name><![CDATA[ IL TUO SITO ]]></news:name>' . "\n";
        $sitemap .= "\t\t\t\t" . '<news:language>LINGUA</news:language>' . "\n";
        $sitemap .= "\t\t\t" . '</news:publication>' . "\n";
        $sitemap .= "\t\t\t<news:publication_date>" . $postdate[0] . 'T' . $postdate[1] . $tempo . "</news:publication_date>\n";
        $sitemap .= "\t\t\t" . '<news:title><![CDATA[' . get_the_title( $post) . ']]></news:title>' . "\n";
        $sitemap .= "\t\t" . '</news:news>' . "\n";
        $sitemap .= "\t" . '</url>' . "\n";
    }
    $sitemap .= '</urlset>';
    $fp = fopen( ABSPATH . "sitemap_news.xml", 'w' );
    fwrite( $fp, $sitemap );
    fclose( $fp );
}
7 ago 2020 17:07:57
Commenti

Cosa succede se devi generare un output per tutti i post? Limitarlo a 20 non è scalabile. Quali alternative ci sono?

Motivated Motivated
8 set 2022 11:39:48