WordPress e l’attributo rel del tag HTML link

Mentre stavo leggendo quali sono i meta tag HTML considerati da Google nell’indicizzazione delle pagine, mi sono imbattuto nell’articolo correlato Indicare a Google i contenuti suddivisi in più pagine. Siccome ho qualche articolo suddiviso in più pagine, come questo, ho voluto verificare se e come sono inseriti questi tag nel codice.

E ho notato qualcosa di strano.

Un ripasso veloce

L’attributo rel="next" e rel="prev" per il tag HTML <link> inserito nel tag <head> va usato per indicare che la pagina che si sta leggendo fa parte di un articolo unico suddiviso in più pagine. Il W3C definisce next così:

The next keyword indicates that the document is part of a sequence, and that the link is leading to the document that is the next logical document in the sequence.

W3c, Link type “next”.

mentre per prev dice:

The prev keyword indicates that the document is part of a sequence, and that the link is leading to the document that is the previous logical document in the sequence.

W3c, Link type “prev”.

Per cui, se un articolo è suddiviso in 5 parti e stiamo leggendo la pagina 1 della serie, il tag <link> sarà così:

<link rel="next" href="http://www.example.com/parte-2" />

mentre nella pagina 2 avremo:

<link rel="prev" href="http://www.example.com/parte-1" /> <link rel="next" href="http://www.example.com/parte-3" />

e nell’ultima:

<link rel="prev" href="http://www.example.com/parte-4" />

Cosa ho notato

E veniamo ora al punto. Ho scoperto che WordPress non usa in modo corretto questo attributo, in quanto lo usa per indicare il post precedente e il post successivo a quello che si sta leggendo. Ad ogni modo è possibile intervenire per ripristinare l’uso corretto del tag <link>.

Non ho verificato se esiste un ticket su questa cosa, ma comunque ho cercato se qualcuno si fosse già accorto del problema. Tra i risultati vi segnalo questo post, da cui ho tratto ispirazione per il mio articolo e in parte per la funzione che vi propongo sotto. Vi voglio evidenziare questo passaggio di quanto scrive l’autore:

Currently (version 4.2.2), wordpress already adds a rel=“prev” and rel=“next” tag to single posts, but these tags are added to all posts irrespective of whether they have pagination or not. And oddly enough, the pages that have pagination do not have these tags. Having these tags on non-paginated posts makes little to no sense from the SEO perspective and it only serves as additional clutter you can get rid of.

Mukesh M, Adding Rel=“Next” And Rel=“Prev” Tags To Paginated Single Posts (Using NextPage Tag).

Rimozione del tag <link> di WordPress

Se avete un plugin personale con le funzioni che vi trascinate dietro anche quanto cambiate tema, inserite la seguente riga:

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );

Se non avete questo tipo di plugin, inseritela nel file functions.php del tema in uso.

Aggiunta del tag <link> corretto

Aggiungiamo ora la funzione che utilizza in modo corretto il tag <link> e il suo attributo rel="next" e rel="prev" solo quando il post o la pagina sono suddivisi in più pezzi.

Aggiungete le righe seguenti al plugin personale o al file functions.php del tema. Il codice è abbastanza commentato.

 1/**
 2 * Prints the HTML tags link rel prev and next in the header.
 3 *
 4 * The attributes prev and next for the HTML tags link in the header
 5 * must be used when an article in divided into multiple pieces, so that the
 6 * first page has a link tag to the second part, the second page has a link
 7 * to the previous part and to the following part, the last page has a link
 8 * to the previous part.
 9 *
10 * WordPress uses incorrectly these tags to link the current post to the previous
11 * and the following posts, as if all the published posts belong to a single big article.
12 *
13 * @link https://support.google.com/webmasters/answer/1663744?hl=it&ref_topic=4617741
14 * @global object $wp_query The WP_Query object.
15 */
16function add_link_tag_header() {
17    // Get the global variable $wp_query, which will be used later.
18    global $wp_query;
19
20    // Figure out if we have a paginated post/page and count them.
21    $total_pages = count( explode( '<!--nextpage-->', $wp_query->post->post_content ) );
22
23    // If we are in a post/page and the total number of pages is greater than 1.
24    if ( is_singular() && 1 < $total_pages ) {
25        // Get the current number of page.
26        $current_page = intval( get_query_var( 'page' ) );
27
28        // If we are in the first page, change the number to 1 (in an array, the first element has number = 0).
29        if ( 0 === $current_page ) {
30            $current_page = 1;
31        }
32
33        echo "\n\n" . '<!-- Function: add_link_tag_header() -->' . "\n";
34
35        // Add prev HTML link tag.
36        if ( $current_page > 1 && $current_page <= $total_pages ) {
37            echo '<link rel="prev" href="' . esc_url( trailingslashit( get_permalink() . ( $current_page - 1 ) ) ) . '" />' . "\n";
38        }
39
40        // Add next HTML link tag.
41        if ( $current_page >= 1 && $current_page < $total_pages ) {
42            echo '<link rel="next" href="' . esc_url( trailingslashit( get_permalink() . ( $current_page + 1 ) ) ) . '" />' . "\n";
43        }
44
45        echo '<!-- Function: add_link_tag_header() -->' . "\n\n";
46    }
47}
48add_action( 'wp_head', 'add_link_tag_header' );</code>

Una volta messa in opera la modifica, alla pagina 6 di un articolo di 10 pagine avremo all’interno dal tag <head> qualcosa di questo tipo:

<!-- Function: add_link_tag_header() -->
<link rel="prev" href="https://www.example.com/titolo-del-post/5/" />
<link rel="next" href="https://www.example.com/titolo-del-post/7/" />
<!-- Function: add_link_tag_header() -->

Tutto questo in attesa che gli sviluppatori di WordPress sistemino la cosa.