Add Canonical Tags in Child Theme of WordPress for SEO
The provided PHP code is intended for a WordPress child theme to add a custom canonical link to the head section of the website. The canonical link helps search engines understand the preferred URL for a page, which is crucial for SEO (Search Engine Optimization).
Breakdown of the Code:
- Enqueue Parent Styles:
function enqueue_parent_styles() {
wp_enqueue_style(‘parent-style’, get_template_directory_uri() . ‘/style.css’);
}
add_action(‘wp_enqueue_scripts’, ‘enqueue_parent_styles’);
This function (enqueue_parent_styles
) is used to enqueue the parent theme’s styles. It ensures that the child theme inherits the styles from the parent theme.
- Custom Canonical Link Function:
function add_custom_canonical_link() {
$protocol = (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] === ‘on’) ? ‘https’ : ‘http’;
$host = $_SERVER[‘HTTP_HOST’];
$requestUri = $_SERVER[‘REQUEST_URI’];
$canonicalUrl = esc_url($protocol . ‘://’ . $host . $requestUri);if ($canonicalUrl) {
// Check if it’s the front page or the blog page
if (is_front_page() || (is_home() && !is_paged())) {
echo ‘<link rel=”canonical” href=”‘ . $canonicalUrl . ‘” />’ . PHP_EOL;
}
}
}
add_action(‘wp_head’, ‘add_custom_canonical_link’, 999);
- This function (
add_custom_canonical_link
) generates the canonical URL based on the current page’s URL. - It then checks if it’s the front page or the main blog page without pagination (
!is_paged()
). - If the conditions are met, it echoes the canonical link in the head section of the website.
WordPress Child Theme:
A WordPress child theme is a theme that inherits the functionality and styling of another theme called the parent theme. Child themes are used to make modifications or customizations to a theme without directly editing the original theme’s files.
Advantages of Using a Child Theme:
- Preserves Changes During Updates:
- When the parent theme is updated, changes made in a child theme are preserved, preventing them from being overwritten.
- Easy Customization:
- Child themes allow developers to customize templates, styles, and functionality without modifying the core parent theme.
- Organized Development:
- It provides an organized structure for theme development, separating customizations from the core theme.
Creating a WordPress Child Theme:
To create a child theme, follow these general steps:
- Create a New Folder:
- In the
wp-content/themes/
directory, create a new folder for your child theme.
- In the
Create Stylesheet (style.css):
- Create a
style.css
file in the child theme folder. Include a comment block specifying the theme name, author, and template.
/*
Theme Name: My Child Theme
Author: Your Name
Template: parent-theme-folder-name
*/
Create Functions File (functions.php):
- Create a
functions.php
file in the child theme folder. This file can be used to enqueue styles/scripts, add custom functions, or modify existing ones.
Activate the Child Theme:
- Go to the WordPress admin dashboard, navigate to “Appearance” > “Themes,” and activate your child theme.
Add Customizations:
- Customize your child theme by adding or modifying files in the child theme folder.
By following these steps, you will have a functional child theme ready for customization.
- Here is the complete code for your
functions.php
file
<?php
// Enqueue parent styles
function enqueue_parent_styles() {
wp_enqueue_style(‘parent-style’, get_template_directory_uri() . ‘/style.css’);
}
add_action(‘wp_enqueue_scripts’, ‘enqueue_parent_styles’);// Add custom canonical link in the head section
function add_custom_canonical_link() {
$protocol = (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] === ‘on’) ? ‘https’ : ‘http’;
$host = $_SERVER[‘HTTP_HOST’];
$requestUri = $_SERVER[‘REQUEST_URI’];
$canonicalUrl = esc_url($protocol . ‘://’ . $host . $requestUri);if ($canonicalUrl) {
// Check if it’s the front page or the blog page
if (is_front_page() || (is_home() && !is_paged())) {
echo ‘<link rel=”canonical” href=”‘ . $canonicalUrl . ‘” />’ . PHP_EOL;
}
}
}
add_action(‘wp_head’, ‘add_custom_canonical_link’, 999); // Set a high priority to ensure it runs after other actions
?>