1

I'd like to skip using SEO plugins for adding canonical URL to some of my posts and pages. I've thought about creating a custom field which I would then populate only when needed.

I've looked around and I've found a very old answer on Stack overflow so I'm not sure if this is still the best way to do it.

On page Canonical URLs In Wordpress I've found the following code:

function canonical_r(){
if( !is_admin() ) {
$canonical_raw = (get_post_meta(get_the_ID(), 'canonical', true));
if ($canonical_raw)  {
$canonical_url = preg_replace('/https*:\/\/|\s/', '', $canonical_raw);
?>
<link rel="canonical" href="<?php echo 'https://'.$canonical_url; ?>" />
<?php }} }
add_action('wp_head', 'canonical_r');

I guess that it could work, but it requires a child theme. I'd like to avoid that if it is possible.

Can this be done without a child theme? I used to use Toolest plugin for such things, but I don't have it available on the current project.

KWriter
  • 1,024
  • 4
  • 22

0 Answers0