Pages

Friday, April 19, 2013

Wordpress create new comment form



Please copy/paste below code in functions.php

function comment_form_new( $args = array(), $post_id = null ) {
global $user_identity, $id;

if ( null === $post_id )
$post_id = $id;
else
$id = $post_id;

$commenter = wp_get_current_commenter();

$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
           '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
           '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
           '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
);

$required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required"><a>*</a></span>' );
$defaults = array(
'fields'               => apply_filters( 'comment_form_default_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"></textarea></p>',
'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>',
'logged_in_as'         => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',

'id_form'              => 'commentform',
'id_submit'            => 'submit',
'title_reply'          => __( 'Leave <a>a Comment</a>' ),
'title_reply_to'       => __( 'Leave a Reply to %s' ),
'cancel_reply_link'    => __( 'Cancel reply' ),
'label_submit'         => __( 'Send Comment' ),
);

$args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );

?>
<?php if ( comments_open() ) : ?>
<?php do_action( 'comment_form_before' ); ?>
<div id="respond">
<h3 id="reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
<?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
<?php echo $args['must_log_in']; ?>
<?php do_action( 'comment_form_must_log_in_after' ); ?>
<?php else : ?>
<form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>">
<?php do_action( 'comment_form_top' ); ?>
<?php if ( is_user_logged_in() ) : ?>
<?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
<?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
<?php else : ?>
<?php echo $args['comment_notes_before']; ?>
<?php
do_action( 'comment_form_before_fields' );
foreach ( (array) $args['fields'] as $name => $field ) {
echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
}
do_action( 'comment_form_after_fields' );
?>
<?php endif; ?>
<?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
<?php echo $args['comment_notes_after']; ?>
<p class="form-submit">
<input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
<?php comment_id_fields(); ?>
</p>
<?php do_action( 'comment_form', $post_id ); ?>
</form>
<?php endif; ?>
</div><!-- #respond -->
<?php do_action( 'comment_form_after' ); ?>
<?php else : ?>
<?php do_action( 'comment_form_comments_closed' ); ?>
<?php endif; ?>
<?php
}

After this open "comments.php"

and replace "<?php comment_form(); ?>"

with "<?php comment_form_new(); ?>"

Tuesday, April 16, 2013

Wordpress comment submit using ajax



Please write below code in functions.php

add_action('init', 'ajaxcomments_load_js', 10);
function ajaxcomments_load_js(){
wp_enqueue_script('ajaxcomments', get_stylesheet_directory_uri().'/ajaxcomments.js');
}

add_action('comment_post', 'ajaxify_comments',20, 2);
function ajaxify_comments($comment_ID, $comment_status){
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
switch($comment_status){
case '0':
wp_notify_moderator($comment_ID);
case '1':
echo "success";
$commentdata=&get_comment($comment_ID, ARRAY_A);
$post=&get_post($commentdata['comment_post_ID']);
wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
break;
default:
echo "error";
}
exit;
}
}


Please add below script in "Footer.php"
<script>
jQuery('document').ready(function($){
        var commentform=$('#commentform'); // find the comment form
        commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
        var statusdiv=$('#comment-status'); // define the infopanel
     
        commentform.submit(function(){
                        //serialize and store form data in a variable
                        var formdata=commentform.serialize();
                        //Add a status message
                        statusdiv.html('<p>Processing...</p>');
                        //Extract action URL from commentform
                        var formurl=commentform.attr('action');
                        //Post Form with data
                        $.ajax({
                                type: 'post',
                                url: formurl,
                                data: formdata,
                                error: function(XMLHttpRequest, textStatus, errorThrown){
                                        statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
                                },
                                success: function(data, textStatus){
                                        if(data=="success")
                                                statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
                                        else
                                                statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
                                        commentform.find('textarea[name=comment]').val('');
                                }
                        });
                        return false;
             
        });
});
</script>