Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
If you think it is expensive (not sure about that) you can wrap it in a function which sets and unsets the filter. You can even check if the post is password protected before setting the filter.... –  Mark Kaplun Dec 23 '12 at 11:04
    
@MarkKaplun How do you test the protection? comments_popup_link() might be used outside of the main query, so you cannot rely on a global $post object. Not sure I understand what you mean with setting/unsetting. Where you call the add_filter() is up to you, the theme author. :) –  toscho Dec 23 '12 at 11:12
    
My understanding of the question was that the OP can edit theme files and therefor can create a wrapper function around comments_popup_link() and use it instead. AFAICT comments_popup_link() requires $post to be set to function. –  Mark Kaplun Dec 23 '12 at 11:38
1  
Ah, I see. Made an update. :) –  toscho Dec 23 '12 at 11:47
1  
wow. you guys are very proficient in wordpress. –  Aliyah Dec 23 '12 at 19:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.