How to translate hardcoded strings in theme’s functions.php with wpml string translation?

I added a login/register and a logout menu item to my navigation. Therefore I used following code in my theme’s functions.php script:

add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
    if (is_user_logged_in() && $args->theme_location == 'footer') {
        $items .= '<li><a href="'. wp_logout_url() .'">Logout</a></li>';
    }
    elseif (!is_user_logged_in() && $args->theme_location == 'footer') {
        $items .= '<li><a href="'. site_url('my-account') .'">Login/Register</a></li>';
    }
    return $items;
}

So if the user is logged in the logout menu item will show up, otherwise the login/register menu item. But I have a bilingual website using wpml and would like to know how to make the “Login/Register”-string translatable.

Read More

I already tried this:

$items .= '<li><a href="'. site_url('my-account') .'">'.__('Login/Register').'</a></li>'; 

and also this:

$items .= '<li><a href="'. site_url('my-account') .'">'.__('Login/Register', 'theme_name').'</a></li>'; 

but it didn’t work. According to this it should be enough to display the string in the dashboard menu -> wpml -> string translation

So what did I wrong?

Related posts

Leave a Reply

1 comment