Add get_comments_popup_link to WordPress
Dec
16
2012

WordPress is a great platform for building your own blog, but, as with any open-source code base, it has a few annoyances that makes writing your own custom code harder than should be necessary. One of my biggest complaints has to be the inconsistency with functions that return strings versus output the string via echo.

Most major functions in WordPress have echo and string variants. For instance, we can output the title of a WordPress post with:

<?php the_title(); ?>

Alternatively, we can store the value of the post title as a string variable with:

<?php $str = get_the_title(); ?>

As I said, most major functions in WordPress follow this pattern— but an annoyingly small set only have their echo variant. This is the case for the function comments_popup_link, which echoes out a formatted link to the comments section of a post. Unfortunately, WordPress does not have a get_comment_popup_link counterpart, which was exactly what I needed when building my Read More and Comments links for TheScubaGeek.com.

I copy/pasted the core functionality for comments_popup_link into my theme’s functions.php file, then modified the echo statements to write to a string instead. I also had to modify comments_number to be a string variant function get_comments_number_str (the function get_comments_number does exists in WordPress, but it does not apply the string formatting options).

Pretty simple copy/paste/replace job, but I figured I’d share this with the community to save everyone future headaches:

/**
 * Modifies WordPress's built-in comments_popup_link() function to return a string instead of echo comment results
 */
function get_comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
	global $wpcommentspopupfile, $wpcommentsjavascript;

	$id = get_the_ID();

	if ( false === $zero ) $zero = __( 'No Comments' );
	if ( false === $one ) $one = __( '1 Comment' );
	if ( false === $more ) $more = __( '% Comments' );
	if ( false === $none ) $none = __( 'Comments Off' );

	$number = get_comments_number( $id );

	$str = '';

	if ( 0 == $number && !comments_open() && !pings_open() ) {
		$str = '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
		return $str;
	}

	if ( post_password_required() ) {
		$str = __('Enter your password to view comments.');
		return $str;
	}

	$str = '<a href="';
	if ( $wpcommentsjavascript ) {
		if ( empty( $wpcommentspopupfile ) )
			$home = home_url();
		else
			$home = get_option('siteurl');
		$str .= $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
		$str .= '" onclick="wpopen(this.href); return false"';
	} else { // if comments_popup_script() is not in the template, display simple comment link
		if ( 0 == $number )
			$str .= get_permalink() . '#respond';
		else
			$str .= get_comments_link();
		$str .= '"';
	}

	if ( !empty( $css_class ) ) {
		$str .= ' class="'.$css_class.'" ';
	}
	$title = the_title_attribute( array('echo' => 0 ) );

	$str .= apply_filters( 'comments_popup_link_attributes', '' );

	$str .= ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
	$str .= get_comments_number_str( $zero, $one, $more );
	$str .= '</a>';
	
	return $str;
}

/**
 * Modifies WordPress's built-in comments_number() function to return string instead of echo
 */
function get_comments_number_str( $zero = false, $one = false, $more = false, $deprecated = '' ) {
	if ( !empty( $deprecated ) )
		_deprecated_argument( __FUNCTION__, '1.3' );

	$number = get_comments_number();

	if ( $number > 1 )
		$output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
	elseif ( $number == 0 )
		$output = ( false === $zero ) ? __('No Comments') : $zero;
	else // must be one
		$output = ( false === $one ) ? __('1 Comment') : $one;

	return apply_filters('comments_number', $output, $number);
}

Armed with the new string-returning functions above, I was able to modify WordPress’s excerpt_more filter in my theme’s functions.php file to append the following links at the end of each post excerpt.

function new_excerpt_more($more) {
	$str = ' [...]<br /><a href="'.get_permalink().'" class="read-more">Read More</a>';
	$str .= get_comments_popup_link('Add Comment', '1 Comment ', '% Comments ', 'read-comments', 'Comments off');
	return $str;
}
add_filter('excerpt_more', 'new_excerpt_more');

One Response to “Add get_comments_popup_link to WordPress”

  • #1

    Thanks TheScubaGeek.com for the get variant of the “comments_popup_link” function. This is exactly what I was looking for.

Leave a Reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>