Mark Comments by Current User

To highlight comments of the currenly logged-in user, add this plugin:

<?php
/*
Plugin Name: Comment Class for Logged-In User
Plugin URI: n/a
Description: add the comment_class .comment-author-logged-in for the comments of logged-in users.
Version: 1.0
Author: alchymyth
Author URI: http://transformationpowertools.com
License: GPL2
*/

add_filter( 'comment_class', 'comment_class_logged_in_user' );

function comment_class_logged_in_user( $classes ) {
	global $comment;
	if ( $comment->user_id > 0 && is_user_logged_in() ) {  
		global $current_user; get_currentuserinfo(); 
		$logged_in_user = $current_user->ID;  
		if( $comment->user_id == $logged_in_user ) $classes[] = 'comment-author-logged-in';
	}
return $classes;
}

//  requires formatting of the css class: 
//  .comment-author-logged-in { }
?>

Add a file with the above code into the /wp-content/plugins folder; activate the plugin under ‘appearance – plugins – installed plugins’; and add a corresponding style to the stylesheet of the currently used theme.

The code compares the comment author’s ID with the ID of the logged-in user, and adds the class .comment-author-logged-in to the ‘comment_class’ output.

Comments on the ‘Posts Page’ in WordPress

If you are using a static home page and have set one of your other pages to be the ‘posts page‘ [Codex], then there is no direct way to allow comments on that page.

You will need to do some custom programming to allow viewers to comment on the ‘posts page‘, i.e. not just on the individual posts, but on the whole page in general.

WordPress does not use any page templates for the ‘posts page‘, but uses index.php or home.php of the currently active theme – see template hierarchy.

This example assumes that the theme does not have a home.php, so we are doing the coding in index.php.

First, we need to check that we are on the ‘posts page‘, and that is set under dashboard – settings – reading:

if( is_home() && get_option('page_for_posts') ) :

Then we get the ‘original’ page using WP_Query():

$posts_page = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => 1, 'post__in' => array(get_option('page_for_posts')) ) );

We could then for instance output the content of that page as it is written in the page editor; this content would otherwise not get shown for a ‘posts page’; or use ‘get_template_part(‘content’,’page’)’ as in the example for Twenty Twelve.

Last we output the comments and comment form with:

global $withcomments; $withcomments = 1;
comments_template();
$withcomments = 0; 

The $withcomments = 1;  is needed to show the comments part in a ‘posts page‘.

The full example code is:

<?php if( is_home() && get_option('page_for_posts') ) :
$posts_page = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => 1, 'post__in' => array(get_option('page_for_posts')) ) );
if( $posts_page->have_posts() ) while( $posts_page->have_posts() ) : $posts_page->the_post();
get_template_part('content','page');
//this is for Twenty Twelve; or whatever you want to output from the 'posts page'
global $withcomments; $withcomments = 1;
comments_template();
$withcomments = 0;
endwhile; wp_reset_postdata();
endif; ?>

Title Attribute for Post Thumbnail

two men shake hands - bronze statues in Calagary/BCThe ‘featured image’ in WordPress automatically outputs the image alt attribute, but no title attribute.

Those two attributes are used for different reasons:
– the alt attribute is used to output alternative information if the image is inaccessible, for instance if the user has opted not to show images in the site, or for screen readers for accessibility reasons;
– the title attribute would present additional information about the image, and is shown by some browsers when you hover over the image.

For a WordPress ‘featured image’, the code below is taking the image title as you see it in the media library,  to be used as the title attribute.

The influence on search engine optimisation is not clear – it seems that the alt attribute is given more importance than the title attribute.

Back to the coding – To add the image title of the featured image as the title attribute to the img tag, a little filter function is needed, added  to the  ‘post_thumbnail_html’ filter.

Instead of just adding the code to functions.php of your theme, where it would get lost when you switch themes, it is more universal to create a basic plugin:

<?php
/*
Plugin Name: Featured Image Title Attribute Output
Plugin URI: n/a
Description: adds the image title as the title attribute to the 'featured image' output.
Version: 1.0
Author: alchymyth
Author URI: http://www.transformationpowertools.com/wordpress
License: GPL2
*/

add_filter('post_thumbnail_html','add_post_thumbnail_title_attribute',10,3);

function add_post_thumbnail_title_attribute($html, $post_id, $post_thumbnail_id) {

if( $html == '' ) {
return $html;
} else {
$out = $html;
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));

if ($thumbnail_image && isset($thumbnail_image[0])) {

$title = $thumbnail_image[0]->post_title;
if($title) {
$output = ' title="' . esc_attr($title) . '" ';
$out = str_replace( 'class=', $output . 'class=', $html );
}
}
}
return $out;
}
?>

Upload this as a appropriately named file into the plugins folder of your WordPress installation, and activate it like any other plugin from dashboard – plugins.