-
-
Notifications
You must be signed in to change notification settings - Fork 0
Hreflang Attributes
These snippets helps you dynamically set hreflang attributes for single language WordPress websites. It improves SEO by ensuring search engines can index the correct pages for each language or region.
- Automatically generates hreflang tags.
- Links translated pages correctly.
- Compatible with most multilingual plugins (e.g., WPML, Polylang).
-
Add to Your Theme Copy the code from the file and paste it into your theme's
header.php
file or a custom plugin. -
Customisation
- Ensure the code aligns with your multilingual plugin setup.
- Modify the generated tags if necessary to match your site's requirements.
-
Testing
- View your page source to confirm hreflang tags are appearing correctly.
- Use SEO tools to verify proper functionality.
<link rel="alternate" hreflang="en" href="https://example.com/en/" />
<link rel="alternate" hreflang="fr" href="https://example.com/fr/" />
Add more snippets to this page as the repository grows. Each snippet should include:
- Name: Descriptive title of the snippet
- Overview: Brief description of its purpose
- Features: Key functionalities
- Usage: Step-by-step integration instructions
- Example Output: Sample of the expected result
This script dynamically generates hreflang attributes for multilingual WordPress websites. It ensures that each page references all its translated versions and complies with Google’s guidelines for hreflang implementation. It is compatible with URL structures where each language is hosted under specific prefixes (e.g., /en
, /fr
, /de
).
- Dynamic Language Support: Automatically generates hreflang tags for each language version of a page.
-
Root Domain for Default Language: Supports the default language (
en
) on the root domain (e.g.,https://example.com
). -
Language-Specific URL Prefixes: Handles other languages using distinct URL structures, such as
/fr
for French or/de
for German. - Reciprocal Links: Ensures hreflang annotations include links to all alternate page versions, resolving common "missing return link" issues.
-
Fallback Support: Includes
x-default
hreflang for global fallback when no specific language preference is detected. - Multilingual Plugin Compatibility: Works seamlessly with plugins like WPML or Polylang.
-
Add to Your Theme Copy the code from the file and paste it into your theme's
header.php
file or include it as part of a custom plugin. -
Define Supported Languages Update the
$hreflang_languages
array in the script to include your supported languages and their URL prefixes. For example:
$hreflang_languages = [
'en' => '', // English (root domain)
'fr' => '/fr', // French
'de' => '/de', // German
'x-default' => '' // Default homepage
];