Skip to content

Custom Themes and Forms

Jörn Lund edited this page May 25, 2015 · 3 revisions

Custom Forms

Display the captcha

$attr = array(
    'data-theme' => 'dark',
);
do_action( 'recaptcha_print' , $attr );

Note: The optional argument $attr will not work for the old style reCaptcha.

Validating

There are two filters allowing you to validate the recaptcha. The first one recaptcha_valid will return a boolean indicating if the user input is valid or not.

Use it like this:

$recaptcha_valid = apply_filters( 'recaptcha_valid' , null )  !== false;

The second one recaptcha_error will return a WP_Error when the verification fails, the first filter argument otherwise.

Usage:

$something = apply_filters( 'recaptcha_error' , $something );
if ( is_wp_error( $something ) )
    wp_die( $something );
// go further with something

Comment form

When you write your own comment form you propably will like to preserve the Protect Comments option.

Display the captcha

On a custom comment form you can trigger the action print_comments_recaptcha.

That one should do it:

$attr = array(
    'data-theme' => 'dark',
);
do_action( 'print_comments_recaptcha' , $attr );

Note: The optional argument $attr will not work for the old style reCaptcha.

By default the plugin hooks into the comment_form_defaults filter, populating the comment_notes_after field. (Filter reference, WP Source ) Until WordPress 4.2 this was the only way to display a captcha right above the submit button.

$comment_form_defaults = apply_filters('comment_form_defaults' , array() );
echo $comment_form_defaults['comment_notes_after'];

WP 4.2. introduces the comment_form_submit_button filter. Enable the option saying 'My Comment Form is WordPress 4.2 compatible.' in the plugin settings and use it like this:

$my_comment_form_button = apply_filters('comment_form_submit_button' , '<input type="submit" name="submit" value="Post Comment, Dude" />' );
echo $my_comment_form_button;

Validating

For validation the plugin hooks into the WP action pre_comment_on_post. When validation fails it will call wp_die() with a corresponding error message. In the very unlikely case, that you implement your own comment processing, make sure you have a do_action( 'pre_comment_on_post' ) in your script.

When your form data is being sent via ajax, the return value of apply_filters('recaptcha_valid',null) will tell you if the captcha is valid.

function my_ajax_comment_form_sumbit() {
    $recaptcha_valid = apply_filters('recaptcha_valid',null);
    if ( $recaptcha_valid ) {
        // process comment submission
    } else {
        // send error message.
    }
}

Login

Display

Make sure you do_action('login_form'); in a custom login form.

Validation

The plugin uses the wp_authenticate_user filter and will return a WP_Error when the validation fails. In case the public and/or private keys are not working any more, and the you are going to log in with administrator privileges, the plugin will skip the captcha verification and let you in, so you can set up a new keypair in the plugin options.

Registration

Display

Make sure you do_action('register_form'); in a custom login form.

In a buddypress environment you will have to use the bp_account_details_fields action hook.

On a WP Multisite you should use signup_extra_fields.

Validation

On a single install the registration_errors will be filtered and an error is returned.

On Buddypress it hooks into bp_signup_pre_validate and wp_die()is called when the verification fails.

On a WP Multisite it uses the wpmu_validate_user_signup filter.

Lost Password

Display

Make sure you do_action('lostpassword_form'); in your password reset form.

Validation

On action lostpassword_post the plugin will wp_die() when verification fails.