Generic way to insert <link> into <head>?

Hi all,
We’ve recently run across an issue where an author created a bunch of annotations on some content using hypothesis and then realized, too late, that they had anchored their annotations on a “part” rather than a “chapter,” as initially intended. I want to preserve the annotations they made and just move them to the now published chapter. It appears from hypothesis’ documentation (see that we can do this simply by inserting a <link rel="canonical" href="[url]" /> into the page’s header, but I’m not aware of any Pressbooks native method to add material to a page’s <head> element. Short of gaining server access and/or installing a add header/footer plugin to the site (not our first choice), does anyone have any advice/ideas?

How about?

add_action(  'wp_head', function () {
	/** @var WP_POST $post */
	$post = get_post();
	// Some code you write yourself to determine whether or not you want to print into the header
	if ($post->ID == 20 ) {
		echo '<link rel="canonical" href="[url]" />' . "\n";
} );

That could work, but we’d have to either add it to an existing functionality plugin or make a new one, and our host only reviews those changes weekly. This should do what we need, though. Thanks, Dac!

Dac’s suggestion will definitely work. However, I would strongly recommend against doing it. rel="canonical" has a very specific meaning. Linking the part with the annotations to the chapter where the annotations belong using rel="canonical" tells Google and everything else that the part and the chapter are literally the same thing. See here for more details.

It may seem the best way to get the annotations moved over is to use this feature of Hypothesis, but I would encourage seeking out another solution (on the Hypothesis end). Making this kind of modification to a webbook has lots of potential downsides.

Agreed. See this previous conversation for my understanding of where things stood: Hypothesis annotations and cloned books?

1 Like