0

[I am using the shared code here.

I added a Login and Register link to the menu.]

(Login and Register in woocommerce site)

But I can't check language urls.


www.example.com/tr/
www.example.com/en/
www.example.com/ar/

The code that ı use :

function so_loginout_menu_links( $items, $args ) {
   if ( $args->theme_location == 'primary' && function_exists('is_woocommerce') ) {
      if (is_user_logged_in()) {
         $items .= '<li><a href="'. site_url('/my-account/customer-logout') .'">'. __("Log Out") .'</a></li>';
         $items .= '<li><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ).'">'. __("My Account") .'</a></li>';
      } else {
         $items .= '<li><a href="'. site_url('/my-account') .'">'. __("Log In") .'</a></li>';
         $items .= '<li><a href="'. site_url('/my-account').'">'. __("Register") .'</a></li>';
      }
   }
   return $items;
}
add_filter( 'wp_nav_menu_items', 'so_loginout_menu_links', 10, 2 );

site_url = www.example.com

I want someone who is in English to come to www.example.com/en/my-account when my account link is clicked.
This may be in a different language.
There are currently 3 languages in my site. How do I control?


Sorry for late reply. I updated it this way.

function so_loginout_menu_links( $items, $args ) {
   if ( $args->theme_location == 'primary' && function_exists('is_woocommerce') ) {
      if (is_user_logged_in()) {
         $items .= '<li><a href="'. pll_home_url().('/my-account/customer-logout') .'">'. __("Log Out") .'</a></li>';
         $items .= '<li><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ).'">'. __("My Account") .'</a></li>';
      } else {
         $items .= '<li><a href="'. pll_home_url().('/my-account/') .'">'. __("Log In") .'</a></li>';
         $items .= '<li><a href="'. pll_home_url().('/my-account/').'">'. __("Register") .'</a></li>';
      }
   }
   return $items;
}
add_filter( 'wp_nav_menu_items', 'so_loginout_menu_links', 10, 2 );

My urls are different in all languages. For example:
My Account URL
TR: www.example.com/tr/hesabim
EN: www.example.com/my-account
AR: www.example.com/ar/my-account-ar

When pll_home_url is added.
That's the links.
www.example.com/tr/my-account
www.example.com/ar/my-account
This leads to the English page again.

  • Please provide more information. The code adds register/login links to the primary menu, depending on wheter a user is logged in or not. What exactly do you want to check? – pnine Jan 01 '20 at 15:28
  • Yeah, if he's logged in. log out and my account menu comes up. I want to continue with whatever language comes from urls. @pnine – Bülent Çelik Jan 01 '20 at 15:38
  • How do you provide i18n? Do you use a plugin like WPML/Polylang/qTranslate or do you just want to detect user's local information and then insert the language identifier in your menu structure? The latter can be achieved with [Accept-Language](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4), like [here](https://stackoverflow.com/questions/3770513/detect-browser-language-in-php) – pnine Jan 02 '20 at 08:59
  • I'm use to polylang. If it is in English language, I want the register page to be in English. but Turkish is opening.Arabic is the same. redirecting to turkish link. Sorry for my English. – Bülent Çelik Jan 02 '20 at 18:51
  • Then why don't you use one of their [functions](https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/)? For example use `pll_current_language` to get the current language slug. Put the result in `pll_home_url` to retrieve the home url and use that instead of your `site_url`. – pnine Jan 03 '20 at 19:24
  • @pnine I am sorry for my late answer. I started learning new coding. Thank you. – Bülent Çelik Jan 05 '20 at 14:25

1 Answers1

0

Let's focus on a single url, for example www.example.com/ar/my-account-ar with it's corresponding translation www.example.com/tr/hesabim. As you can see the URL path is not only affected by the 2-letter language slug, but also by the translation of my-account-ar to hesabim. So when you append the hardcoded string '/my-account/' to your urls it can't work.

I dont have a polylang installation to test with, but maybe you can get the specific url by using the post id of that page: get_the_permalink(pll_get_post(get_page_by_path( 'page-slug' )->ID, $language_slug));

For more info on pll_get_post have a look at the polylang reference

pnine
  • 83
  • 1
  • 8