Skip to content

Instantly share code, notes, and snippets.

@mycarrysun
Last active November 8, 2016 20:26
Show Gist options
  • Save mycarrysun/23fbab9f8292f340e52c708b2d701b2c to your computer and use it in GitHub Desktop.
Save mycarrysun/23fbab9f8292f340e52c708b2d701b2c to your computer and use it in GitHub Desktop.
Modifications to ninja forms version 2.9.x recaptcha field so multiple recaptchas will work on a single page
function nf_grecaptcha_explicit_render(){
jQuery('.g-recaptcha').each(function(){
var sitekey = jQuery(this).data('sitekey');
grecaptcha.render(this, {
'sitekey':sitekey
})
});
}
<?php
//This is a modified version of the function in the file: ninja-forms/deprecated/includes/fields/recaptcha.php
function ninja_forms_field_recaptcha_display( $field_id, $data, $form_id = '' ) {
$settings = get_option( "ninja_forms_settings" );
$lang = $settings['recaptcha_lang'];
$siteKey = $settings['recaptcha_site_key'];
$field_class = ninja_forms_get_field_class( $field_id, $form_id );
$rand = wp_rand(0, 99999);
wp_enqueue_script(
'g-recaptcha',
'https://www.google.com/recaptcha/api.js?onload=nf_grecaptcha_explicit_render&render=explicit&hl='.$lang, ['nf-grecaptcha-explicit-render'] );
if ( !empty( $siteKey ) ) { ?>
<input id="ninja_forms_field_<?php echo $rand;?>" name="ninja_forms_field_<?php echo $field_id;?>" type="hidden" class="<?php echo $field_class;?>" value="" rel="<?php echo $field_id;?>" />
<div class="g-recaptcha" data-callback="nf_recaptcha_set_field_value" data-sitekey="<?php echo $siteKey; ?>"></div>
<script type="text/javascript">
function nf_recaptcha_set_field_value(inpval){
jQuery("#ninja_forms_field_<?php echo $rand;?>").val(inpval)
}
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment