How to Add jQuery Tooltips in WordPress Comment Form

Comments allow users to engage with the content on your website. That’s why we believe that it is important to style your comments layout and comment form, so it is user-friendly as well as good looking. Recently, a user asked us how they can add jQuery tooltips to WordPress comment form. We thought others might find this useful as well. In this tutorial, we will show you how to add jQuery tooltips in WordPress comment form.

jQuery tooltip in action on WordPress comment form

Why Add jQuery Tooltips?

Tooltips appear when a user takes their mouse to an item, usually providing them description about that particular item. In this tutorial, we will be adding jQuery tooltips to show tips like, Please use your real name in comment form fields.

By adding jQuery tooltips, you can enhance user experience, and it will look nicer.

How to Add jQuery Tooltips

First thing, you need to do is create a folder on your desktop and name it wpb-comment-tooltips. Inside this folder, you need to create these three files:

  • wpb-comment-tooltips.php
  • wpb-tooltip.css
  • wpb-tooltip.js

Use a plain text editor like Notepad to create these files. Once you have created the files, you need to open wpb-comment-tooltip.php in text editor. Copy and paste this code in the file:

01 <?php
02 /**
03 Plugin Name: WPBeginner's Comment Form Tool Tips
04 Description: A jQuery powered comment form tool tips plugin based on a tutorial by WPBeginner
05 Version: 1.0
06 Author: WPBeginner
07 Author URI: http://www.wpbeginner.com
08 License: GPL2
09 */
10
11 // Only load our scripts and style when a comment form is displayed
12
13 add_action( 'comment_form_before', 'wpb_comment_tooltips' );
14
15 function wpb_comment_tooltips() {
16 wp_enqueue_script('wpb-tooltip-jquery', plugins_url('/wpb-tooltip.js', __FILE__ ), array('jquery-ui-tooltip'), '', true);
17 wp_enqueue_style('wpb-tooltip-css', plugins_url('/wpb-tooltip.css', __FILE__), false, null);
18 }
19
20 // Modify comment form fields and add title attribute to form input fields
21  
22 function alter_comment_form_fields($fields){
23     $fields['email'] =  '<p class="comment-form-email"><label for="email">' . __( 'Email', 'twentythirteen' ) . '</label> ' .
24       ( $req ? '<span class="required">*</span>' : '' ) .
25       '<input id="email" title="Your email is safe with us, see our privacy policy." name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
26       '" size="30"' . $aria_req . ' /></p>'
27     $fields['url'] = '<p class="comment-form-url"><label for="url">' .
28       __( 'Website', 'twentythirteen' ) . '</label>' .
29       '<input id="url" name="url" title="Your website or any social media profile URL" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
30       '" size="30" /></p>'
31       $fields['author'] = '<p class="comment-form-author">' .
32       '<label for="author">' . __( 'Name', 'twentythirteen' ) . '</label> ' .
33       ( $req ? '<span class="required">*</span>' : '' ) .
34       '<input id="author" title="Please use your real name, no keywords." name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
35       '" size="30"' . $aria_req . ' /></p>';
36     return $fields;
37 }
38 add_filter('comment_form_default_fields','alter_comment_form_fields');
39 ?>

In the above code, we first created a plugin header, given this plugin a name and description. After that we load our JavaScript and CSS file (see our guide on how to add JavaScript and Styles in WordPress).

We also make sure that these files are only loaded when a comment form is displayed. After that we modified the default comment form and added the title attribute in input fields. This title attribute contains the message we want to be displayed in the tooltip. For example, in the author field we have used:

title="Please use your real name, no keywords."

Now that you have created the plugin file, it is time to add a little jQuery. Open wpb-tooltip.js file and add this code inside it:

01 (function($) {
02 $( "#commentform" ).tooltip({ position: {
03     my: "center bottom-10",
04         at: "center top",
05         using: function( position, feedback ) {
06           $( this ).css( position );
07           $( "<div>" )
08             .addClass( "arrow" )
09             .addClass( feedback.vertical )
10             .addClass( feedback.horizontal )
11             .appendTo( this );
12         } } });
13 })(jQuery);

In this code, #commentform is the selector where jQuery will display tooltips and .tooltip is the content part where we have defined the position for tooltips.

Now the final step is to add a little CSS in wpb-tooltip.css file. Simply copy and paste this code:

01 .ui-tooltip, .arrow:after {
02     background: #356aa0;
03     border: 2px solid white;
04   }
05   .ui-tooltip {
06     padding: 10px 20px;
07     color: white;
08     border-radius: 20px;
09     font: bold 14px "Helvetica Neue", Sans-Serif;
10     text-transform: uppercase;
11     box-shadow: 0 0 7px #356aa0;
12     max-width:350px;
13   }
14   .arrow {
15     width: 70px;
16     height: 16px;
17     overflow: hidden;
18     position: absolute;
19     left: 50%;
20     margin-left: -35px;
21     bottom: -16px;
22   }
23   .arrow.top {
24     top: -16px;
25     bottom: auto;
26   }
27   .arrow.left {
28     left: 20%;
29   }
30   .arrow:after {
31     content: "";
32     position: absolute;
33     left: 20px;
34     top: -20px;
35     width: 25px;
36     height: 25px;
37     box-shadow: 6px 5px 9px -9px #356aa0;
38     -webkit-transform: rotate(45deg);
39     -moz-transform: rotate(45deg);
40     -ms-transform: rotate(45deg);
41     -o-transform: rotate(45deg);
42     tranform: rotate(45deg);
43   }
44   .arrow.top:after {
45     bottom: -20px;
46     top: auto;
47   }

Feel free to modify this CSS file to meet your needs.

That’s all. Now you have successfully created a plugin that adds jQuery tooltips in your WordPress comment form. All you need to do is upload wpb-comment-tooltips folder from your desktop to /wp-content/plugins/ directory on your web server using an FTP client like Filezilla. Once you have uploaded the plugin, go to Plugins page in WordPress admin area and activate the plugin.

Leave a Reply