This document explains how the Multisite Language Switcher generates and formats language switcher links for frontend display. It covers the MslsOutput class, the rendering pipeline, blog context switching, permalink generation, and the MslsLink factory for different display modes.
For information about how these links are displayed through widgets, blocks, and shortcodes, see Widgets and Gutenberg Blocks. For SEO-related alternate link generation, see SEO and Alternate Links.
The MslsOutput class is responsible for generating HTML links to translated versions of the current page across different blogs in the multisite network. It coordinates between the blog collection, options system, and link formatters to produce the final output.
The class extends MslsMain and follows a factory pattern for instantiation:
The output system determines which blogs have translations, switches context to each blog to retrieve permalinks, and formats the results according to user-configured display settings.
Sources: includes/MslsOutput.php1-216 MultisiteLanguageSwitcher.php186-192
The following diagram illustrates how MslsOutput generates language switcher links:
Sources: includes/MslsOutput.php47-102 MultisiteLanguageSwitcher.php57-62
The get() method iterates through available blogs and performs context switching to retrieve translated permalinks:
| Step | Method/Function | Purpose |
|---|---|---|
| 1 | $this->collection->get_filtered() | Retrieve blogs based on filter settings |
| 2 | MslsOptions::create() | Get options object for current content type |
| 3 | MslsLink::create($display) | Create link formatter based on display type |
| 4 | switch_to_blog($blog_id) | Switch WordPress context to target blog |
| 5 | $mydata->get_permalink($language) | Get translated content URL |
| 6 | restore_current_blog() | Restore original WordPress context |
| 7 | Format and collect HTML | Build array of formatted links |
The context switching is critical because permalinks must be generated within each blog's context where the translated content exists.
Sources: includes/MslsOutput.php47-102 includes/MslsBlogCollection.php280-299
The rendering pipeline includes logic to determine whether a blog should be included in the output:
The is_requirements_not_fulfilled() method checks if a translation exists when only_with_translation is enabled.
Sources: includes/MslsOutput.php201-215 includes/MslsOutput.php68-76
The MslsLink class provides a factory pattern for creating different link display types. Four concrete implementations exist:
Sources: includes/MslsLink.php1-106
The following table shows the available display types and their format strings:
| Class | Index | Description | Format |
|---|---|---|---|
MslsLink | 0 | Flag and description | <img src="{src}" alt="{alt}"/> {txt} |
MslsLinkTextOnly | 1 | Text only | {txt} |
MslsLinkImageOnly | 2 | Flag only | <img src="{src}" alt="{alt}"/> |
MslsLinkTextImage | 3 | Description and flag | {txt} <img src="{src}" alt="{alt}"/> |
The factory method MslsLink::create($display) accepts an integer index and returns the appropriate link formatter. If an invalid index is provided, it defaults to index 0.
Sources: includes/MslsLink.php24-81 includes/MslsAdmin.php355-359
During blog iteration, the link object is populated with data for each language:
The __toString() method then replaces placeholders in the format string with actual values using array mapping.
Sources: includes/MslsOutput.php58-64 includes/MslsLink.php97-105
The output is wrapped with configurable HTML tags defined in the options:
| Tag | Purpose | Example |
|---|---|---|
before_output | HTML before entire list | <ul> |
after_output | HTML after entire list | </ul> |
before_item | HTML before each link | <li> |
after_item | HTML after each link | </li> |
These tags are retrieved via get_tags() and can be customized through the msls_output_get_tags filter.
Sources: includes/MslsOutput.php160-185 includes/MslsOptions.php29
The __toString() method assembles the final output string:
Sources: includes/MslsOutput.php142-158
Tags can be set programmatically using set_tags():
The set_tags() method merges provided tags with existing tags using wp_parse_args(), allowing partial overrides.
Sources: includes/MslsOutput.php187-198
Each link is formatted as an HTML anchor element with specific attributes:
The current language link receives special CSS class and ARIA attributes for accessibility.
Sources: includes/MslsOutput.php79-98
Each generated link contains:
$mydata->get_permalink($language) or $mydata->get_current_link()$blog->get_description())MslsLink object (flag, text, or both)current_language for current blogaria-current="page" for current blogSources: includes/MslsOutput.php62-96
The get_alternate_links() method generates <link rel="alternate"> tags for SEO:
The method uses HrefLang mapper to convert language codes to proper hreflang format (e.g., "de_DE" → "de-DE"). The first URL is also tagged with hreflang="x-default" unless there's only one translation.
Sources: includes/MslsOutput.php106-140 includes/Map/HrefLang.php
The output system is accessed through several public API functions:
| Function | Returns | Purpose |
|---|---|---|
msls_get_switcher($attr) | string | Get formatted HTML output |
msls_the_switcher($arr) | void | Echo HTML output |
msls_output() | MslsOutput | Get output instance |
Sources: MultisiteLanguageSwitcher.php57-82 MultisiteLanguageSwitcher.php186-192
The output is consumed by multiple frontend components:
Each integration point calls msls_output()->__toString() or uses msls_get_switcher() to retrieve the formatted output.
Sources: includes/MslsWidget.php39-65 includes/MslsShortCode.php includes/MslsPlugin.php43-50
The output system provides several filter hooks for customization:
| Hook | Location | Purpose |
|---|---|---|
msls_get_output | MultisiteLanguageSwitcher.php59 | Override entire output object |
msls_output_get | includes/MslsOutput.php79-89 | Customize individual link HTML |
msls_output_get_tags | includes/MslsOutput.php174-181 | Modify wrapper tags |
msls_output_no_translation_found | includes/MslsOutput.php150 | Handle empty output |
msls_output_get_alternate_links | includes/MslsOutput.php118 | Modify alternate link URLs |
msls_output_get_alternate_links_arr | includes/MslsOutput.php137 | Modify alternate links array |
msls_output_get_alternate_links_default | includes/MslsOutput.php134 | Modify x-default link |
Sources: includes/MslsOutput.php14-25 MultisiteLanguageSwitcher.php50-61
Refresh this wiki
This wiki was recently refreshed. Please wait 2 days to refresh again.