Highlight comments using #comment-hash and jquery

Adding this snippet to the functions.php of your wordpress theme will add class=”highlight” to a comment when visiting a comment URL eg: wpsnipp.com/category/post/#comment-12 Don’t forget to create some css for the highlight class!

 

 

add_action( 'init', 'jquery_register' );
function jquery_register() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}
add_action('wp_head','wps_highlight_hash');
function wps_highlight_hash(){
echo '<script type="text/javascript">
$(document).ready(function() {
/* HIGHLIGHT WITH COMMENT HASH */
if(window.location.hash) {
$(window.location.hash).addClass("highlight");
}
});
</script>';
}

.highlight{
background:#F5EAC6;
}

Leave a comment