Add HTML Tags and Attributes back into Comments in WordPress

Since WordPress 3, the notice of being able to use HTML Tags and Attributes has been removed and there is just an empty string, which means that the use of the allowed tags in comments is still feasible but readers may not know. You can add this notice back in using the filter comment_form_defaults

wordpress-filter-comments-html-tags

Add in your themes functions.php file

<?php //<~ don't add me in
//Add comment HTML tags
//Ref - http://codex.wordpress.org/Function_Reference/comment_form
add_filter( 'comment_form_defaults', 'wpb_change_comments' );
function wpb_change_comments( $defaults ) {
$defaults['comment_notes_after'] = '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML <abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>';
return $defaults;
}
view raw tags.php hosted with ❤ by GitHub

You can use the filter to change many elements either text strings or markup, below is a list…

/**
* Filter the default comment form fields.
*
* @since 3.0.0
*
* @param array $fields The default comment fields.
*/
$fields = apply_filters( 'comment_form_default_fields', $fields );
$defaults = array(
'fields' => $fields,
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required="required"></textarea></p>',
/** This filter is documented in wp-includes/link-template.php */
'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
/** This filter is documented in wp-includes/link-template.php */
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( '<a href="%1$s" aria-label="Logged in as %2$s. Edit your profile.">Logged in as %2$s</a>. <a href="%3$s">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
'comment_notes_after' => '',
'id_form' => 'commentform',
'id_submit' => 'submit',
'class_form' => 'comment-form',
'class_submit' => 'submit',
'name_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'title_reply_before' => '<h3 id="reply-title" class="comment-reply-title">',
'title_reply_after' => '</h3>',
'cancel_reply_before' => ' <small>',
'cancel_reply_after' => '</small>',
'cancel_reply_link' => __( 'Cancel reply' ),
'label_submit' => __( 'Post Comment' ),
'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
'submit_field' => '<p class="form-submit">%1$s %2$s</p>',
'format' => 'xhtml',
);
view raw comments-filter.php hosted with ❤ by GitHub

So if you wanted to add the tags and change the label_submit text to ‘Post A Reply‘ you can adjust the earlier function wpb_comments_change() like so…

<?php //<~ don't add me in
//Add comment HTML tags
//Ref - http://codex.wordpress.org/Function_Reference/comment_form
add_filter( 'comment_form_defaults', 'wpb_change_comments' );
function wpb_change_comments( $defaults ) {
$defaults['comment_notes_after'] = '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML <abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>';
$defaults['label_submit'] = __('Post A Reply');
return $defaults;
}
view raw comments-change.php hosted with ❤ by GitHub

Modifying the Allowed Tags for Comments

So these allowed HTML tags are generated from the allowed_tags() function which stores the tags in /wp-icludes/kses.php as a global array variable $allowedtags –  you could set what tags you want by redefining the variable $allowedtags

<?php //<~ don't add me in
add_action('init', 'wpb_allowedtags_comments', 10);
function wpb_allowedtags_comments() {
define('CUSTOM_TAGS', true);
global $allowedtags;
$allowedtags = array(
'a' => array(
'href' => array (),
'title' => array ()),
'code' => array(),
'em' => array(),
'strong' => array(),
'pre' => array(),
);
}
view raw allowedtags.php hosted with ❤ by GitHub

Here I have taken a bunch of tags out and added pre in, this works, however it is advised against in kses.php  and you are advised to use wp_kses_allowed_html() (but this edit works ok for my purpose).

So the user would see…

wordpress-filter-comments-html-tags-edited

 

3 Comments

  1. david on May 4, 2016 at 4:44 am

    Thank you

  2. Aliza on January 20, 2016 at 1:15 pm

    THankyou working…..

    • Mark Sandel on July 25, 2016 at 1:34 pm

      True, a good article!

Leave a Comment





%d bloggers like this: