WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Function to add reCaptcha to comment form (2 posts)

  1. Tatiane Pires
    Member
    Posted 3 years ago #

    I'm trying to create a function on functions.php theme file to add reCaptcha form HTML (available at Custom Theming), but I didn't find an appropriate hook at Filter Reference or at Action Reference to use with add_action().

    I know that there are a lot of plugins to do it. I don't want to use a plugin because I want it to be customizable. And I don't want to edit comments.php on my theme either.

    All this is because I want to validate reCaptcha input with jQuery before the form is actually sent, like in my Contact page.

    So far, what I need to know is if there is a tag (either a hook or filter) to use in add_action() that runs when the comment form html is sent to the browser.

    Thanks.

  2. Tatiane Pires
    Member
    Posted 3 years ago #

    In fact, I didn't need a plugin to add recaptcha to respond form.

    Action Reference actually has what I need:
    comment_form is the template action I was looking for.

    functions.php

    function add_comment_recaptcha() {
    	require_once('lib/recaptchalib.php');
    	$publickey = "your_public_key"; // you got this from the signup page
    	echo recaptcha_get_html($publickey).'<div id="captchaStatus" style="display:none;"></div>';
    }
    add_action('comment_form', 'add_comment_recaptcha');

    validate-forms.js
    To validate the form, I use the function validateCaptcha(), adding submitHandler to .validate():

    submitHandler: function(form){
    	if(validateCaptcha()){ offerForm.ajaxSubmit(); }
    }

    script.js
    Then some jQuery to position the submit button after the captcha:

    var button = $('#respond .form-submit');
    if (button.length > 0) {
    	button.remove(); //removes from its current place
    	$('#respond').append(button); //places the button after recaptcha
    }

Topic Closed

This topic has been closed to new replies.

About this Topic