X

Highlight comments using #comment-hash and jquery

Slider Revolution Review - all the buttons

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!


( example screenshot )

Code Snippet:


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>';
}


Code Snippet:


.highlight{
background:#F5EAC6;
}

Comments  Leave a Reply

  1. dsa

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!