0

I did asked a similar question some time ago but i couldn't get a useful answer. I would like to ask again.

I actually wanted to add a field called "security" to prevent those autobot to send me spam through the wordpress comment form. And i have a specific answer for this security field. I want my comment form to be submitted oif only the answer is correct or else the comment not submit.

You can see the example at my existing website. In my website, i have set the answer to be 17. The form can only be submitted successfully if the answer is correct.

Here is the code i have to process the enquiry form in my website(but I have cut the code short for easier reference):

Php code Part 1:

$security = $_POST['security'];

if ($security=="10") {
   mail($to, $subject, $message, "Jornes");
   header("Location:/?security=submitted");
}

else {
   header("Location:/?security=error");
}

Php code Part 2:

<?php
$s=$_GET['security'];
if ($s=="error") {
   echo ('<p class="errorMessage">Ops! Something went wrong at the security field. Please fix the error and retry!</p><p>&nbsp;</p>');
   }
?>

I would like to ask, how shall i get this code implemented to wp-comment-post.php or any other solution you have? Thanks a lot!

But, i am not going to use plugin. Any expert here?

1

Check out the default comment-template.php file: wp-includes/comment-template.php

You will need to replace the arguments:

$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>',
    '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>',
    'id_form'              => 'commentform',
    'id_submit'            => 'submit',
    'title_reply'          => __( 'Leave a Reply' ),
    'title_reply_to'       => __( 'Leave a Reply to %s' ),
    'cancel_reply_link'    => __( 'Cancel reply' ),
    'label_submit'         => __( 'Post Comment' ),
);

Override the part of the form where you want to insert your required item. For example, I use the after_notes section as follows normally:

$defaults['comment_notes_after'] .= '<div>Your Code Here</div>';

You can then call comment form like:

comment_form( $defaults );

That should draw your extra section into the comment form.

The simplest way to do this would be to add your content to the comment_notes_after argument on the comment form.

For example:

$comment_fields = array(
   '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><p>Enter Code:<input name="security" value="" /></p>';
);
apply_filters( 'comment_form_default_fields', $comment_fields );

That should add <p>Enter Code: <input name="security" value="" /></p> after the allowed fields section of the comment form.

It needs the first part as that is what is in the comment_notes_after field by default.

2
  • Hi Privateer, i think you have misunderstood the question i am asking about. Or you can do some sample how to get the code implemented to the form? Thank you! – Jornes Jan 15 '15 at 17:51
  • I added a quick snippet to show how to do what I explained above using a filter. – Privateer Jan 15 '15 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.