• As many people might know, when you password protect a page using the standard WordPress functionality, it does protect it, but there is absolutely no feedback to the user if they input the wrong password.

    I grabbed some code I found and inserted it into my functions.php file. It works – see my site.

    This brings me to my current problem – I can’t get any translatable strings to work in this piece of code. I have a multi-site setup in English and French, and all translations work correctly otherwise. I only have a vague grasp of php – not enough to diagnose the problem. Can anybody help?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Since you added the code to functions.php, it becomes part of your theme. Your theme probably does not have ‘Sorry, your password is wrong.’ as a translatable string in the theme’s language files. You could add it, then recompile the machine readable files.

    You could also make the code into a simple plugin with its own text domain. You would still need to compile the language files.

    If your site only uses 2 languages, if you can determine how the current language is determined, you can use that in a conditional that chooses one string or the other (French or English) to be used in the form.

    Thread Starter Alex Poole

    (@alex-poole)

    Thanks for your suggestion. What I’ve done already is taken a string that I know for sure is translated fine from the theme files, double checked that it is in the .po file, and inserted it into the function as $test, but it still doesn’t get translated:

    /**
     * ALEX Password error message - Adapted from https://www.ceus-now.com/add-error-message-on-password-protected-page/
     */
    add_filter( 'the_password_form', 'wpse_71284_custom_post_password_msg' );
    
    /**
     * Add a message to the password form.
     *
     * @wp-hook the_password_form
     * @param   string $form
     * @return  string
     */
    function wpse_71284_custom_post_password_msg( $form )
    {
        // No cookie, the user has not sent anything until now.
        if ( ! isset ( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) )
            return $form;
    
        // Translate and escape.
        $msgError = __( 'Sorry, wrong password - please try again.', 'graphy' );
    
    $msgHelp = __( 'Need access? Drop me a line: [email protected]', 'graphy' );
    
    $test = __( 'Powered by WordPress', 'graphy' );
    
        // We have a cookie, but it doesn’t match the password.
        $msg = "<p class='password-feedback'><strong>$msgError</strong></br>$msgHelp<span id='transTest'>$test;</span></p>";
    
        return $msg . $form;
    }

    Have a made a mistake in the syntax? Or can it not hook into translations in this way?

    Moderator bcworkz

    (@bcworkz)

    No, that looks fine. I cannot explain why ‘Powered by WordPress’ does not translate if the string is exactly that in .po files and not ‘Powered by %s’ or some variation. You might try using the same code as used by the theme on the footer template to output the same thing.

    When you say you have a multi-site setup, do you mean an actual WordPress Mutli-site network where one site is set for English, the other for French? Or some other poly-language plugin that appears as multiple sites? If you are using a WP network, then I’m stumped. There are some quirks about network installs that keep things from always working as you expect. This might be a question for the multisite forum.

    If you are using a poly-language plugin, try switching languages in the general settings. If that works, there is some quirk about the plugin. Asking in that plugin’s dedicated support forum might shed some light on the issue.

    To eliminate possible outside influences, revert your site to as close to the default state as possible. Deactivate all plugins and switch to a default twenty* theme. Be sure the theme has French language files. (Available from the French WP download.) Call your callback directly from a template and echo the return. (comment out the cookie check for testing) Just pass ‘form goes here’ or similar as the function’s argument. Switch languages in general settings. Your function’s text should translate this way. Start restoring your original state, one plugin at a time, testing after each activation. When the translation fails, the last thing you activated is causing problems.

    Thread Starter Alex Poole

    (@alex-poole)

    Its a standard multi-site setup, one site in English, one in French – using the Multisite Language Switcher plugin to link both together.

    Thanks so much for taking the time to help. Unfortunately the rest of the diagnostics you recommend are beyond my current expertise and time resources. Thanks again ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Feedback on password protected pages translation issue’ is closed to new replies.