Home / comment / Highlight comments using #comment-hash and jquery
  • How to Highlight comments using #comment-hash and jquery

    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!

    ( example screenshot ) ( click code to copy )

    wordpress snippet : PHP

    <>
    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>';
    }
    
    ( click code to copy )

    wordpress snippet : SAMPLE CSS

    <>
    .highlight{
           background:#F5EAC6;
           }
    

    ( WordPress codex functions, hooks, in this snippet. )

    register, query, init, wp_head, add_action, _e, is_admin, wp_deregister_script, wp_enqueue_script, wp_register_script, wp,
  • Baby_pop20

    dsa