| Server IP : 85.112.90.236 / Your IP : 192.168.1.26 Web Server : Apache System : Linux 85-112-90-236.cprapid.com 6.12.0-211.7.3.el10_2.x86_64 #1 SMP PREEMPT_DYNAMIC Tue May 19 12:46:58 EDT 2026 x86_64 User : ftechme ( 1002) PHP Version : 8.2.32 Disable Function : exec,passthru,shell_exec,system MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/ftechme/www/wp-content/mu-plugins/ |
Upload File : |
<?php
/**
* Plugin Name: Faisal Technology Polylang Switcher Fix
* Description: Keeps custom language switchers linked to the matching translated page.
* Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function ftech_switcher_target() : array {
$current_lang = function_exists( 'pll_current_language' ) ? pll_current_language( 'slug' ) : '';
$target_lang = 'ar' === $current_lang ? 'en' : 'ar';
$label = 'ar' === $target_lang ? 'العربية' : 'English';
$url = function_exists( 'pll_home_url' ) ? pll_home_url( $target_lang ) : home_url( '/' );
if ( function_exists( 'pll_get_post' ) && is_singular() ) {
$post_id = get_queried_object_id();
$translation_id = $post_id ? pll_get_post( $post_id, $target_lang ) : 0;
if ( $translation_id ) {
$url = get_permalink( $translation_id );
}
}
return array(
'url' => $url,
'label' => $label,
);
}
add_filter(
'wp_nav_menu_args',
function ( $args ) {
$location = isset( $args['theme_location'] ) ? (string) $args['theme_location'] : '';
if ( in_array( $location, array( 'primary___ar', 'primary_ar' ), true ) || ( 'primary' === $location && function_exists( 'pll_current_language' ) && 'ar' === pll_current_language( 'slug' ) ) ) {
$menu = wp_get_nav_menu_object( 'Primary Arabic' );
if ( $menu ) {
$args['menu'] = $menu->term_id;
}
}
return $args;
},
1
);
add_filter(
'wp_nav_menu_items',
function ( $items, $args ) {
$location = empty( $args->theme_location ) ? '' : (string) $args->theme_location;
if ( ! in_array( $location, array( 'primary', 'primary___ar', 'primary_ar' ), true ) ) {
return $items;
}
$target = ftech_switcher_target();
$replacement = '<li class="menu-item ftech-lang-switch"><a href="' . esc_url( $target['url'] ) . '">' . esc_html( $target['label'] ) . '</a></li>';
if ( false !== strpos( $items, 'ftech-lang-switch' ) ) {
return preg_replace( '#<li class="menu-item ftech-lang-switch">.*?</li>#', $replacement, $items );
}
return $items . $replacement;
},
99,
2
);
add_filter(
'wp_page_menu',
function ( $menu, $args ) {
$lang = function_exists( 'pll_current_language' ) ? pll_current_language( 'slug' ) : 'en';
if ( 'ar' === $lang ) {
$pages = array(
46 => 'الرئيسية',
50 => 'الخدمات التقنية',
54 => 'المشاريع',
52 => 'التصوير',
48 => 'عن فيصل',
56 => 'تواصل',
);
} else {
$pages = array(
5 => 'Home',
7 => 'IT Services',
9 => 'Projects',
8 => 'Photography',
6 => 'About',
10 => 'Contact',
);
}
$current_id = get_queried_object_id();
$items = '';
foreach ( $pages as $page_id => $title ) {
$current = (int) $current_id === (int) $page_id ? ' current_menu-item' : '';
$aria = $current ? ' aria-current="page"' : '';
$items .= '<li class="menu-item page-item-' . esc_attr( (string) $page_id ) . esc_attr( $current ) . '"><a href="' . esc_url( get_permalink( $page_id ) ) . '"' . $aria . '>' . esc_html( $title ) . '</a></li>';
}
$target = ftech_switcher_target();
$items .= '<li class="menu-item ftech-lang-switch"><a href="' . esc_url( $target['url'] ) . '">' . esc_html( $target['label'] ) . '</a></li>';
return '<div class="menu"><ul id="primary-menu" class="menu">' . $items . '</ul></div>';
},
99,
2
);
add_action(
'wp_enqueue_scripts',
function () {
$target = ftech_switcher_target();
wp_register_script( 'ftech-polylang-switcher-fix', false, array(), '1.0.0', true );
wp_enqueue_script( 'ftech-polylang-switcher-fix' );
wp_add_inline_script(
'ftech-polylang-switcher-fix',
'window.ftechLanguageTarget = ' . wp_json_encode( $target ) . ';' .
"document.addEventListener('DOMContentLoaded',function(){var t=window.ftechLanguageTarget;if(!t){return;}document.querySelectorAll('.ftech-floating-lang,.ftech-lang-switch a').forEach(function(a){a.setAttribute('href',t.url);a.textContent=t.label;});});"
);
},
40
);