Better CF7 widget hooking
All checks were successful
Generate Build Info / build-info (push) Successful in 2s

This commit is contained in:
carpentryplus25
2026-03-12 23:19:12 -04:00
parent 9e54bcb3ef
commit a24af298f0

View File

@@ -1207,6 +1207,48 @@ if ( class_exists( 'WPCF7' ) ) {
<?php
}
add_filter( 'do_shortcode_tag', 'dapper_cf7_universal_insert_checkbox', 10, 4 );
function dapper_cf7_universal_insert_checkbox( $output, $tag, $attr, $m ) {
if ( $tag !== 'contact-form-7' ) {
return $output;
}
if ( get_option( 'dapper_enable_cf7_human_checkbox', 'on' ) !== 'on' ) {
return $output;
}
dapper_debug_log( 'Universal CF7 shortcode filter FIRED - processing form ID: ' . ( $attr['id'] ?? 'unknown' ) );
$unique = uniqid();
$checkbox_html = '
<div class="dapper-cf7-human-check" style="margin: 1.5em 0; padding: 1em; background: #f8f9fa; border: 1px solid #ccd0d4; border-radius: 4px; text-align: center;">
<label style="font-size: 1.1em; cursor: pointer; user-select: none;">
<input type="checkbox" name="dapper_cf7_human_confirm" id="dapper_cf7_human_confirm_' . $unique . '" value="1" required style="transform: scale(1.4); margin-right: 0.8em; vertical-align: middle;">
I am human / not a robot
</label>
<input type="hidden" name="dapper_cf7_human_token" id="dapper_cf7_human_token_' . $unique . '" value="">
<input type="hidden" name="dapper_cf7_human_time" value="' . time() . '">
<p style="margin: 0.6em 0 0; font-size: 0.9em; color: #555;">Quick check to help stop spam. Thanks!</p>
</div>';
// Insert after response div or before </form> — aggressive match
$output = preg_replace( '/(<div class="wpcf7-response-output"[^>]*>.*?<\/div>)/is', '$1' . $checkbox_html, $output, 1 );
if ( strpos( $output, $checkbox_html ) === false ) {
$output = str_replace( '</form>', $checkbox_html . '</form>', $output );
}
if ( strpos( $output, $checkbox_html ) === false ) {
$output .= $checkbox_html; // Last resort append
dapper_debug_log( 'Universal CF7 fallback append used for shortcode/widget' );
}
dapper_debug_log( 'Universal CF7 insertion COMPLETE for shortcode' );
return $output;
}
// 2. NEW: Visible checkbox — use reliable append method
add_filter( 'wpcf7_form_elements', 'dapper_cf7_append_human_checkbox', 100 ); // Higher priority = later
function dapper_cf7_append_human_checkbox( $form ) {