WP Residence Help WP Residence Help

  • WpEstate
  • WPRESIDENCE
  • Video Tutorials
  • Client Support
  • API
Home / 30. WPResidence Translate Plugin / Admin Bar Switcher & Editor Header Badge — Developer Reference

Admin Bar Switcher & Editor Header Badge — Developer Reference

2 views 0

This article covers the two admin-side language controls that the wpresidence-translate plugin wires into wp-admin: the admin-bar Content Language dropdown and the block editor header language badge. Both are thin layers on top of the language manager; the interesting parts are where they store state, how they gate access, and where they hook in. The sibling user article explains the UX. Product context lives on the multi-language real estate website page.

Source Files

File Role
includes/admin/admin-bar-language-switcher.php Admin bar dropdown — node registration, switch handler, nonce-protected URL.
includes/admin/editor-header-language.php Block editor language badge — localizes the payload into the admin JS bundle.
includes/admin/post-editor.php Post editor auto-translate button; reads the same language resolution helpers.

Admin Bar Switcher — Bootstrap

wpr_translate_bootstrap_admin_bar_language_switcher() is called from the plugin bootstrap. It registers two hooks:

add_action( 'admin_init',   'wpr_translate_handle_admin_language_switch' );
add_action( 'admin_bar_menu', 'wpr_translate_register_admin_bar_language_switcher', 90 );

Admin Bar Node Registration

wpr_translate_register_admin_bar_language_switcher( $wp_admin_bar ) runs at priority 90 on admin_bar_menu. It bails early unless:

  • is_admin() is true.
  • $wp_admin_bar instanceof WP_Admin_Bar.
  • current_user_can( 'edit_posts' ).
  • is_admin_bar_showing().
  • count( wpr_translation_get_active_languages() ) >= 2.

The parent node is registered with ID wpr-translate-admin-language and title Content language: {Current}. Each child node is wpr-translate-admin-language-{code}, with the current entry receiving class wpr-translate-admin-language-current.

Screenshot: Admin bar showing the Content language parent node with child language items

State — User Meta

The selection is stored per user under the meta key:

Meta key Type Written by
wpr_translate_admin_language string (ISO code) wpr_translate_handle_admin_language_switch() via update_user_meta().

Reads go through wpr_translate_get_admin_selected_language_code(), which sanitizes via sanitize_key() and validates against wp_list_pluck( wpr_translation_get_active_languages(), 'code' ). Invalid values return an empty string, letting callers fall back to the default language.

Switch Handler Flow

wpr_translate_handle_admin_language_switch() runs on admin_init. Only triggers when $_GET['wpr_translate_admin_language'] is present. Steps:

  1. Check current_user_can( 'edit_posts' ).
  2. Verify $_GET['_wpnonce'] against the action wpr_translate_switch_admin_language.
  3. sanitize_key() the requested code and validate against active languages.
  4. If valid: update_user_meta( $user_id, 'wpr_translate_admin_language', $code ).
  5. If invalid or empty: delete_user_meta( $user_id, 'wpr_translate_admin_language' ).
  6. Build the redirect URL by stripping wpr_translate_admin_language and _wpnonce from REQUEST_URI, then wp_safe_redirect() and exit.

URLs are built by wpr_translate_get_admin_language_switch_url( $code ), which composes add_query_arg() onto the current REQUEST_URI and wraps with wp_nonce_url().

Fallback Language

When the user has no stored meta, the current label resolves through wpr_translation_get_default_language_code( $languages ). This keeps the dropdown title informative on first visit.

Editor Header Badge — Bootstrap

wpr_translate_enqueue_post_editor_language_badge_assets( $hook ) is registered at priority 20:

add_action( 'admin_enqueue_scripts',
            'wpr_translate_enqueue_post_editor_language_badge_assets', 20 );

It only acts when $hook is post.php or post-new.php. Priority 20 ensures the admin script bundle wpr-translate-admin is already registered and can receive the wp_localize_script() payload.

Language Resolution Order

  1. wpr_translate_return_current_language_admin() — mirrors the admin bar user meta when available.
  2. wpr_translation_get_default_language_code( wpestate_translation_get_active_languages() ).
  3. get_option( 'wpestate_default_language', '' ) — legacy compatibility for sites upgraded from older WPResidence translation helpers.
  4. Hardcoded 'en'.

JS Payload Shape

The badge data is exposed to the admin bundle under wprTranslatePostLanguage:

window.wprTranslatePostLanguage = {
    label:   'Français ( France )',
    code:    'fr',
    flagUrl: 'https://example.com/wp-content/plugins/wpresidence-translate/assets/img/flags/4x3/fr.svg',
    flagAlt: 'Français ( France ) flag',
};

Flag URLs resolve via WPR_TRANSLATE_URL . 'assets/img/flags/4x3/{flag}.svg', falling back to the language code when no flag is set on the language record. The alt text template is __( '%s flag', 'wpr-translate' ).

Screenshot: Browser DevTools Console inspecting window.wprTranslatePostLanguage payload

Post Editor Auto-Translate Button

The related file includes/admin/post-editor.php hooks post_submitbox_misc_actions via wpr_translate_render_post_auto_translation_button(). It reuses the same language resolution chain — wpr_translate_get_post_language(), then wpr_translation_resolve_post_language_code(), then the default — so the publish-panel button, the editor header badge, and the admin bar switcher stay coherent.

When the target post’s language equals the default language, the button is replaced with the Original post – no translation action. notice. When no source post can be resolved via wpr_translate_resolve_metabox_source_post_id(), the button is rendered disabled.

Capability Map

Surface Capability
Admin bar node render edit_posts
Admin bar switch handler edit_posts + nonce wpr_translate_switch_admin_language
Editor badge payload Any user who can load post.php/post-new.php
Auto-translate button current_user_can( 'edit_post', $post->ID )
Languages admin page manage_options

Extension Notes

  • Do not write to wpr_translate_admin_language directly from custom code — always go through update_user_meta() after validating against wpr_translation_get_active_languages().
  • If you are adding your own language-aware list filter, read the user’s selection via wpr_translate_get_admin_selected_language_code() and fall back to wpr_translation_get_default_language_code().
  • The editor badge reads on every post editor page load — consumers should prefer wpr_translate_get_post_language( $post_id ) to follow the same source of truth.
  • The switcher intentionally does not alter the admin UI locale. Changing WordPress admin language remains a user-profile concern; this switcher only filters content.

Related Articles

  • Managing Languages — Developer Reference — the language manager API both surfaces read from.
  • Translating Posts & Pages — the editor-side user workflow.
  • Post List Table Enhancements — how the admin language filter interacts with list screens.

For background, see the multi-language real estate website page.

30. WPResidence Translate Plugin

Related Articles

  • String Scanner — Developer Guide
  • The String Scanner
  • Gettext Pipeline & MO Files — Developer Guide
  • Gettext & MO Files — Making Translations Appear on the Front End

WP Residence Documentation

  • 01. Getting Started
    • How to Get Support
    • Get your buyer license code.
    • Use SSL / https
    • Server / Theme Requirements
  • 02. Installation & Setup
  • 03. Installation FAQ
  • 06. Search & Filtering
    • Advanced Search Display Settings
    • Advanced Search Form
    • Geolocation Search for Half Map
    • Save Search Theme Options
    • Advanced Search Colors
  • 09. Agent, Agency & Developers
  • 08. Property Pages & Layouts
  • 07. Property Lists, Categories & Archive
  • 13. WPResidence Elementor Studio
  • 10. Blog Posts & Blog List
  • 11. Shortcodes
    • Contact Form
    • Featured Agency/Developer
    • Membership Packages
    • Testimonials
    • Google Map with Property Marker
    • Listings per Agent, Agency or Developer
    • Display Categories
    • Agent List
    • Recent Items Slider
    • Recent items
    • List Properties or Articles by ID
    • Featured Agent
    • Featured Article
    • Featured Property
    • Login & Register Form
    • Icon Content Box Shortcode
  • 12. Widgets
  • 04. Theme Options & Global Settings
    • General Settings
    • User Types Settings
    • Appearance
    • Logos & Favicon
    • Header
    • Footer Style and Colors
    • Price & Currency
    • Property Custom Fields
    • Features & Amenities
    • Listing Labels
    • Theme Slider
    • Permalinks
    • Splash Page
    • Social & Contact
    • Map Settings
    • Pin Management
    • How read from file works
    • General Design Settings
    • Custom Colors Settings
    • Header Design & Colors
    • Mobile Menu Colors
    • User Dashboard Colors
    • Print PDF Design
    • Property, Agent, Blog Lists Design Settings
    • Sidebar Widget Design
    • Font management
    • How to add custom CSS
    • Custom Property Card Unit – Beta version
    • Email Management
    • Import & Export theme options
    • reCaptcha settings
    • YELP API Integration
    • iHomefinder Optima Express IDX
    • MEMBERSHIP & PAYMENT Settings
    • Property Submission Page
    • PayPal Setup
    • Stripe Setup
    • Wire Transfer Payment Method
  • 20. Translations & Languages
  • 26. FAQ
  • 10. Pages
  • 11. Header
  • 12. Footer
  • 05. Maps & Location Settings
  • 18. Payments & Monetization
  • Plugins
    • 19. Included Plugins
    • 22. Third Party Plugins – IDX Compatibility
    • 21. Third-Party Plugins – Multi-Language
    • 23. Third party Plugins – Other
  • Technical
    • 24. Technical how to | Custom Code Required
    • 25. Technical: Child Theme

Join Us On

Powered by WP Estate - All Rights Reserved
  • WpEstate
  • WPRESIDENCE
  • Video Tutorials
  • Client Support
  • API