Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Customize rel=canonical tag for single blog post

Upon inspection, this code never prints to the page. I imagine it might have to do with parse order.

It depends where you added it. If you want to add code to the <head> element of the page you either need to put it in that element in the head in the header.php template or in your theme’s functions.php file or a plugin with a hook. Pasting just that code in functions.php naked (without a hook or function definition) will cause WordPress to output it before the page has even started being output, which can result in errors.

Regardless, in this case WordPress offers a filter hook, get_canonical_url which can be used to change the URL used in the canonical header added by WordPress.

Use it like this:

function wpse_302620_canonical_url( $canonical_url, $post ) {
    if ( $post->ID === 1234 ) {
        $canonical_url="https://www.examplesite.com/my-guest-post";
    } 

    return $canonical_url;
}
add_filter( 'get_canonical_url', 'wpse_302620_canonical_url', 10, 2 );

Related Posts:

  1. if in category but only with post meta
  2. Add a checkbox to post screen that adds a class to the title
  3. Update all posts automatically when using post_meta
  4. Prevent duplicate posts in wp_insert_post using custom fields
  5. Better post meta efficiency?
  6. Change post format using custom field
  7. Sanitizing `wp_editor();` Values for Database, Edit, and Display
  8. Add custom fields to post programmatically
  9. What Is meta_id In wp_postmeta?
  10. Add custom field automatically (add_post_meta) with value based on number of words of article
  11. How can I sort posts by the date and a custom meta field?
  12. Getting value from get_post_custom
  13. Add a custom meta box in the post options that loads some html code in the header
  14. Calling Different Custom Post Timestamps in a table
  15. Cannot retrieve a custom RSS field from posts
  16. How to I retrieve the ID from the Posts page?
  17. Edit multiple custom post types while saving a new or edited post
  18. Saving Post Data in Another Database
  19. Add multiple meta keys to a post at once
  20. saving/reading custom field value does not work – no value gets POSTed
  21. Simple Custom Metabox Not Saving
  22. Post meta not working
  23. How to bulk Update URLs to new values?
  24. Adding an option to post editor to show a site disclaimer or message
  25. How do you update post date (year only) in a separate custom field?
  26. WP_Query sort by meta_value_num or date
  27. Creating Ordered Query using Meta_key
  28. How to only allow post to be deleted if custom field doesn’t exist
  29. First Custom Post Custom Fields Empty After New Custom Post
  30. How to Do Custom Fields to Output a Definition List
  31. How enter custom post meta or custom field on box post list archive page?
  32. Display Post from Custom Post Type with Shortcode
  33. Pagination Not Working With Custom Post Type Category
  34. Private post caught in endless redirect loop
  35. Redirect to custom post if custom archive page has just one result?
  36. Tag for custom post type
  37. How to create custom search form function including post “tags”
  38. Show posts from WP Custom Post Type selected from a field in a metabox
  39. How to retrieve all meta data directly from the $post object? Such as $post->related_topics?
  40. What is proper filter or hook action to add data to the $post object?
  41. Added a ACF field to custom page and want the results to show as a column in admin
  42. Modified core/image rendering not showing in posts – only on pages
  43. WordPress Pods Custom Post Type – separate Media Upload folder for each custom Post Types
  44. Not display an image of category of custom post
  45. Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
  46. How to invoke a HTML custom button based on a HTML dropdownlist menu
  47. Show post count in custom taxonomy page
  48. Blogposting Schema & Structured Data without plugin
  49. Hide or remove custom post status
  50. Template for displaying CPT / Taxonomy URL issues
  51. Multiple posts in one page
  52. Automatically select template based on query string for New Posts
  53. Put page on Archived statut after end of publication date
  54. Display Updated Date Instead of Published Date
  55. Is there a way to save different data when USER interacts with the same POST?
  56. post and page and custom post type
  57. Please I want to prefix my WP posts title according to each category
  58. How to Disable option of meta field if that option is selected for any other post in custom post type?
  59. Add postmeta to all blog posts
  60. Post relationships/nesting
  61. insert post fails after multiple postings
  62. delete post hook not being fired when deling in wordpress dashboard admin
  63. Manual Scheduler For Custom Post Type
  64. WordPress Delete Users from backend doesn’t work
  65. How to get value of a selected option from select tag and use it in WP_query to filter posts?
  66. When working with a post, almost all wp_postmeta are deleted
  67. Custom Post Type Rewrite URL
  68. Dev and prod on the same database- dev version active for IOS
  69. Is it possible to make a custom post that adds multiple pages?
  70. Custom Post Types and Broken Permalinks
  71. The first post appears twice
  72. How to delete/customize imported demo content from a theme or add my own?
  73. Bulk Post update_post_meta
  74. Loop through taxonomies and loop through their terms (Newbie question)
  75. Re-order Category Meta-data
  76. My posts page is missing the page title
  77. Hiding title of selected post without plugins
  78. Custom post styling per category?
  79. How to hide meta from search result only on pages?
  80. How do I display posts ordered by a date custom field?
  81. Customizing URL slugs of Custom Post Type and Taxonomy make posts/pages 404
  82. Show custom post category for single post
  83. How can change header text in post careate/edit form
  84. Grab meta data before post is saved
  85. How to get meta data from custom category name
  86. Add Categories To Custom Post
  87. How to fetch posts that are stored in the different table than the default table (wp_posts)?
  88. How do I set content in my pages so that it remains permanent while blog posts are added below?
  89. how to redirect page after delete post inside a post page?
  90. Permalink base only for posts
  91. Check if user has avatar
  92. Custom post type – Loop out in random order but same 15 to appear first… still random
  93. Custom post type template not called
  94. Difference between modified post and new post
  95. Displaying dynamic content (blog posts/portfolio) on a static homepage and keep the blog post styles
  96. How to use Categories of custom post type
  97. Append a random string to a post permalink
  98. alt of attached image in post / pages
  99. single- and archive- templates not being applied to custom post types, not even after flushing permalinks
  100. Auto populate custom fields by post date
Categories posts Tags custom-field, customization, post-meta, posts, rel-canonical
Adding something to the_content
HowTo: Custom Post Type (Meta) to Custom Table

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress