Comments number message in password protected post

I’ve got a password protected post and comments_popup_link('No Comments', '1 Comment', '% Comments'); function displays “ENTER YOUR PASSWORD TO VIEW COMMENTS.” message. The question is, is it possible to change this message without touching core files, maybe apply filter or something like that. Thanks.

Related posts

Leave a Reply

1 comment

  1. The only hook to change that is gettext:

    add_action( 'loop_start', 'wpse_77028_switch_filter' );
    add_action( 'loop_end',   'wpse_77028_switch_filter' );
    
    /**
     * Turn comment text filter on or off depending on global $post object.
     *
     * @wp-hook loop_start
     * @wp-hook loop_end
     * @return  void
     */
    function wpse_77028_switch_filter()
    {
        $func = 'loop_start' === current_filter() ? 'add_filter' : 'remove_filter';
        $func( 'gettext',    'wpse_77028_comment_num_text', 10, 3 );
    }
    
    /**
     * Change default text for comments_popup_link()
     *
     * @wp-hook gettext
     * @param   string $translated
     * @param   string $original
     * @param   string $domain
     * @return  string
     */
    function wpse_77028_comment_num_text( $translated, $original, $domain )
    {
        if ( 'Enter your password to view comments.' === $original
            and 'default' === $domain
            )
            return ' ';
    
        return $translated;
    }
    

    Unfortunately, this is rather expensive: gettext is called many, many times, and the filter will run each time. You cannot deactivate it after the first match, because it might be needed more than once per page. I have added a wrapper function to make sure it is running only when there is a global $post object.