- Joined
- Jan 12, 2020
- Messages
- 1
- Reaction score
- 0
I am trying to use the Bootstrap validator to validate the comments in WordPress, as it's a lot nicer than the default WordPress error system. The Jquery validation works nicely, but the form won't submit and post the fields to the wp-comments.php file. Any suggestions?
Functions.php
Comments.php
Functions.php
Code:
<?php
function add_theme_scripts() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/nucleare.js', array ( 'jquery' ), 1.1, true);
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
remove_filter('widget_text_content', 'wpautop');
if ( ! function_exists ( 'nucleare_entry_footer' ) ) {
function nucleare_entry_footer() {
// Hide category and tag text for pages.
if ( 'post' == get_post_type() ) {
/* translators: used between list items, there is a space after the comma */
the_tags( '<span class="tags-links"><i class="fa fa-tags space-right"></i>', esc_html__( ', ', 'nucleare' ), '</span>' );
}
edit_post_link( __( 'Edit', 'nucleare' ), '<i class="material-icons">
create
</i>', '</span>' );
}
}
add_filter( 'comment_form_defaults', 'wpsites_comment_form_defaults' );
function wpsites_comment_form_defaults( $defaults ) {
$defaults['title_reply'] = __( 'Comments' );
$defaults['label_submit'] = __( 'Submit comment', 'custom' );
return $defaults;
}
/**
* @author Brad Dalton
* @example http://wpsites.net/web-design/add-remove-change-order-of-comment-form-default-fields/
* @copyright 2014 WP Sites
*/
add_filter( 'genesis_title_comments', 'wpsites_title_comments' );
function wpsites_title_comments() {
$title = '<h3>Comments</h3>';
return $title;
}
add_filter( 'comment_form_default_fields', 'wpsites_comment_form_fields' );
function wpsites_modify_comment_form_text_area($arg) {
$arg['comment_field'] = '<div class="uk-margin">
<textarea class="uk-textarea" rows="5" id="comment" name="comment" placeholder="Comments*"></textarea>
</div>' .
'' . $aria_req . '' .
'' .
( $req ? : '' )
;
return $arg;
}
add_filter('comment_form_defaults', 'wpsites_modify_comment_form_text_area');
// Method to handle comment submission
function ajaxComment($comment_ID, $comment_status) {
// If it's an AJAX-submitted comment
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
// Get the comment data
$comment = get_comment($comment_ID);
// Allow the email to the author to be sent
wp_notify_postauthor($comment_ID, $comment->comment_type);
// Get the comment HTML from my custom comment HTML function
$commentContent = getCommentHTML($comment);
// Kill the script, returning the comment HTML
die($commentContent);
}
}
// Send all comment submissions through my "ajaxComment" method
add_action('comment_post', 'ajaxComment', 20, 2);
// Modify comments header text in comments
add_filter( 'genesis_title_comments', 'child_title_comments');
function child_title_comments() {
return __(comments_number( '<h3>No Responses</h3>', '<h3>1 Response</h3>', '<h3>% Responses...</h3>' ), 'genesis');
}
// Unset URL from comment form
function crunchify_move_comment_form_below( $fields ) {
$comment_field = $fields['comment'];
unset( $fields['comment'] );
$fields['comment'] = $comment_field;
return $fields;
}
add_filter( 'comment_form_fields', 'crunchify_move_comment_form_below' );
// Add placeholder for Name and Email
function modify_comment_form_fields($fields){
$fields['author'] = ' <div class="uk-margin">
<div class="uk-inline">
<span class="uk-form-icon" uk-icon="icon: user"></span>
<input class="uk-input" name="author" placeholder="Name*" type="text">
</div>
</div>
</div>'.
'' .
( $req ? '<span class="required">*</span>' : '' ) .
'</p>';
$fields['email'] = ' <div class="uk-inline">
<span class="uk-form-icon" uk-icon="icon: mail"></span>
<input class="uk-input" name="email" placeholder="Email*" type="email">
</div>
' .
' ' .
( $req ? '
' : '' )
.
'</p>';
$fields['url'] = ' <div class="uk-inline">
<span class="uk-form-icon" uk-icon="icon: link"></span>
<input class="uk-input" name="url" placeholder="Website" type="text">
</div>
';
return $fields;
}
add_filter('comment_form_default_fields','modify_comment_form_fields');
if ( ! function_exists ( 'nucleare_posted_on' ) ) {
function nucleare_posted_on() {
$format = get_post_format();
$formats = get_theme_support( 'post-formats' );
if ( $format && in_array( $format, $formats[0] ) ) {
$format_string = get_post_format_string( $format );
echo '<span><a class="entry-format entry-format-' . esc_attr( $format ) . '" href="' . esc_url( get_post_format_link( $format ) ) . '" title="' . esc_attr( sprintf( __( 'All %s posts', 'nucleare' ), $format_string ) ) . '">' . $format_string . '</a></span>';
}
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" style="display:none;" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
$posted_on = sprintf(
_x( '<span uk-icon="calendar"></span> %s', 'post date', 'nucleare' ),
'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
);
$byline = sprintf(
_x( '<span uk-icon="user"></span>
%s', 'post author', 'nucleare' ),
'<span class="author vcard"><a class="url fn n" href="https://marianchristiepoetry.net/?page_id=4581">' . esc_html( get_the_author() ) . '</a></span> '
);
echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';
if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) {
echo '<span class="comments-link"><span uk-icon="comments"></span> ';
comments_popup_link( __( 'Leave a comment', 'nucleare' ), __( '1 Comment', 'nucleare' ), __( '% Comments', 'nucleare' ) );
echo '</span>';
}
}}
function prefix_category_title( $title ) {
if ( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'prefix_category_title' );
/**
* Estimates the reading time for a given piece of $content.
*
* @param string $content Content to calculate read time for.
* @param int $wpm Estimated words per minute of reader.
*
* @returns int $time Esimated reading time.
*/
function reading_time() {
$post="";
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
$readingtime = ceil($word_count / 200);
if ($readingtime == 1) {
$timer = " minute";
} else {
$timer = " minutes";
}
$totalreadingtime = $readingtime . $timer;
return $totalreadingtime;
}
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class ($classes, $item) {
if (in_array('current-page-ancestor', $classes) || in_array('current-menu-item', $classes) ){
$classes[] = 'uk-active ';
}
return $classes;
}
function comment_validation_init() {
if(is_single() && comments_open() ) { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#commentform').bootstrapValidator({
message: 'This value is not valid',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
author: {
validators: {
notEmpty: {
message: 'Please Enter Your Name'
}
}
},
email: {
validators: {
notEmpty: {
message: 'Please Enter Your Email'
},
emailAddress: {
message: 'This Is Not A Valid Email Address'
}
}
},
comment: {
validators: {
notEmpty: {
message: "Don't Forget To Leave A Comment"
}
}
}
}
});
});
</script>
<?php
}
}
add_action('wp_footer', 'comment_validation_init');
Comments.php
Code:
<?php foreach (get_comments() as $comment): ?>
<ul class="uk-comment-list">
<li>
<article class="uk-comment uk-visible-toggle" tabindex="-1">
<header class="uk-comment-header uk-position-relative">
<div class="uk-grid-medium uk-flex-middle" uk-grid>
<div class="uk-width-auto">
<img class="uk-comment-avatar" src="images/avatar.jpg" width="80" height="80" alt="">
</div>
<div class="uk-width-expand">
<h4 class="uk-comment-title uk-margin-remove"><a class="uk-link-reset" href="#"> <?php echo $comment->comment_author; ?>
</a></h4>
<p class="uk-comment-meta uk-margin-remove-top"><a class="uk-link-reset" href="#">12 days ago</a></p>
</div>
</div>
<div class="uk-position-top-right uk-position-small uk-hidden-hover"><a class="uk-link-muted" href="#">Reply</a></div>
</header>
<div class="uk-comment-body">
<?php echo $comment->comment_content; ?>
</div>
</article>
<?php endforeach;
if ('open' == $post->comment_status) : ?>
<div id="respond">
<h3><?php comment_form_title( 'Add a comment', 'Leave a Reply to %s' ); ?></h3>
<div class="cancel-comment-reply">
<small><?php cancel_comment_reply_link(); ?></small>
</div>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>
You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.
</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if ( $user_ID ) : ?>
<p>
Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>.
<a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out »</a>
</p>
<?php else : ?>
<div class="row">
<div class="form-group">
<div class="col-lg-12"><label class="control-label" for="author">Name</label></div>
<div class="col-lg-5"><input class="form-control" name="author" id="author" value="" size="22" tabindex="1" type="text"></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-lg-12"><label class="control-label" for="email">Email</label></div>
<div class="col-lg-5"><input class="form-control" name="email" id="email" value="" size="22" tabindex="2" type="text"></div>
<span class="help-block col-lg-12">You're email address will not be published</span>
</div>
</div>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<div class="row">
<div class="form-group">
<div class="col-lg-12"><label class="control-label" for="comment">Comment</label></div>
<div class="col-lg-12"><textarea class="form-control" name="comment" id="comment" cols="100" rows="10" tabindex="3"></textarea></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-lg-12">
<p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit" />
</div>
<?php comment_id_fields(); ?> </div>
</div>
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
</div>
<?php endif; // if you delete this the sky will fall on your head ?>