Skip to content

Commit 234e75e

Browse files
committed
All: Add rel=canonical links on content pages
1 parent 78b4979 commit 234e75e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

plugins/jquery-actions.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,36 @@
99
remove_action( 'wp_head', 'rsd_link' );
1010
remove_action( 'wp_head', 'wlwmanifest_link' );
1111
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
12-
remove_action( 'wp_head', 'rel_canonical' );
1312

1413
// Remove shortlink <head> and header.
1514
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 );
1615
remove_action( 'template_redirect', 'wp_shortlink_header', 11 );
1716

17+
/**
18+
* Add rel=canonical on singular pages (API pages, and blog posts)
19+
*
20+
* Derived from WordPress 6.3.1 rel_canonical:
21+
*
22+
* - Avoid applying esc_url and its 'clean_url' filter so that
23+
* 'https://' is not stripped, and thus the URL is actually canonical.
24+
*/
25+
function jq_rel_canonical() {
26+
if ( !is_singular() ) {
27+
return;
28+
}
29+
$id = get_queried_object_id();
30+
if ( $id === 0 ) {
31+
return;
32+
}
33+
34+
$url = wp_get_canonical_url( $id );
35+
if ( $url) {
36+
echo '<link rel="canonical" href="' . esc_attr( $url ) . '" />' . "\n";
37+
}
38+
}
39+
remove_action( 'wp_head', 'rel_canonical' );
40+
add_action( 'wp_head', 'jq_rel_canonical' );
41+
1842
// Add rel=me link to HTML head for Mastodon domain verification
1943
//
2044
// Usage:

0 commit comments

Comments
 (0)