hreflang-translations.php
Hreflang Translation Links
Handles bidirectional translation linking between posts and automatic hreflang meta tag output for SEO.
Tags
Table of Contents
Functions
- jpkcom_simplelang_get_site_default_locale() : string
- Get site default locale
- jpkcom_simplelang_get_language_name() : string
- Get language name from locale
- jpkcom_simplelang_group_posts_by_language() : array<string, array<int, WP_Post>>
- Group posts by language
- jpkcom_simplelang_validate_translations() : array<string|int, int>
- Validate translation links
- jpkcom_simplelang_get_translation_posts() : array<string|int, WP_Post>
- Get translation posts efficiently
- jpkcom_simplelang_sync_translations() : void
- Sync translation links bidirectionally
- jpkcom_simplelang_render_translations_meta_box() : void
- Render translation links meta box
- jpkcom_simplelang_output_hreflang_tags() : void
- Generate and output hreflang tags
Functions
jpkcom_simplelang_get_site_default_locale()
Get site default locale
jpkcom_simplelang_get_site_default_locale() : string
Returns the site's default locale, ignoring any temporary locale switches. This is the locale set in Settings > General.
Tags
Return values
string —The site default locale (e.g., 'de_DE', 'en_US').
jpkcom_simplelang_get_language_name()
Get language name from locale
jpkcom_simplelang_get_language_name(string $locale) : string
Converts a locale code to a human-readable language name.
Parameters
- $locale : string
-
The locale code (e.g., 'de_DE', 'fr_FR').
Tags
Return values
string —The language name in native language.
jpkcom_simplelang_group_posts_by_language()
Group posts by language
jpkcom_simplelang_group_posts_by_language(array<string, mixed> $query_args) : array<string, array<int, WP_Post>>
Queries posts and groups them by their assigned language for display in the translation links meta box.
Parameters
- $query_args : array<string, mixed>
-
WP_Query arguments.
Tags
Return values
array<string, array<int, WP_Post>> —Posts grouped by locale.
jpkcom_simplelang_validate_translations()
Validate translation links
jpkcom_simplelang_validate_translations(int $post_id, array<string|int, int> $translation_ids) : array<string|int, int>
Ensures that translation links are valid and prevents duplicate languages.
Parameters
- $post_id : int
-
The current post ID.
- $translation_ids : array<string|int, int>
-
Array of translation post IDs.
Tags
Return values
array<string|int, int> —Validated array of translation post IDs.
jpkcom_simplelang_get_translation_posts()
Get translation posts efficiently
jpkcom_simplelang_get_translation_posts(array<string|int, int> $post_ids) : array<string|int, WP_Post>
Fetches multiple posts in a single query to avoid N+1 query problems.
Parameters
- $post_ids : array<string|int, int>
-
Array of post IDs to fetch.
Tags
Return values
array<string|int, WP_Post> —Array of post objects.
jpkcom_simplelang_sync_translations()
Sync translation links bidirectionally
jpkcom_simplelang_sync_translations(int $post_id, array<string|int, int> $new_translations) : void
Updates translation links for the current post and ensures all posts in the translation set are linked to each other. Creates a complete translation set where every post links to all others in the set.
Parameters
- $post_id : int
-
The post ID to sync.
- $new_translations : array<string|int, int>
-
Array of translation post IDs.
Tags
jpkcom_simplelang_render_translations_meta_box()
Render translation links meta box
jpkcom_simplelang_render_translations_meta_box(WP_Post $post) : void
Displays a multi-select dropdown with posts grouped by language.
Parameters
- $post : WP_Post
-
Current post object.
Tags
jpkcom_simplelang_output_hreflang_tags()
Generate and output hreflang tags
jpkcom_simplelang_output_hreflang_tags(int $post_id, array<string|int, int> $translation_ids) : void
Creates hreflang link tags for all translations including the current post.
Parameters
- $post_id : int
-
The current post ID.
- $translation_ids : array<string|int, int>
-
Array of translation post IDs.