如何使用插件挂钩使用 var_dump() 或 printr() 显示来自 Contact Form 7 表单的提交值

How to display submitted values from Contact Form 7 form with var_dump() or printr() using plugin hooks

我想查看“$name”变量中的内容,我该怎么做?

add_action("wpcf7_before_send_mail", "wpcf7_do_something_else"); 

function wpcf7_do_something_else( &$WPCF7_ContactForm ) {
    $name = $WPCF7_ContactForm->posted_data['your-name'];
    var_dump($name);
}

CF7使用AJAX提交表单,普通方式看不到var_dump()。所以通过 PHP 你可以使用 WordPress debug.log 文件。 Iside "wp-config.php" 而不是 define('WP_DEBUG', false); 写:

// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

然后:

add_action("wpcf7_before_send_mail", "wpcf7_do_something_else"); 

function wpcf7_do_something_else( &$WPCF7_ContactForm ) {

    $name = $WPCF7_ContactForm->posted_data['your-name'];

    ob_start();                     // start buffer capture

    var_dump($name);

    $contents = ob_get_contents();  // put the buffer into a variable
    ob_end_clean();                 // end capture
    error_log($contents);           // write the log file
}

作为选项,您可以在 front-end 中使用 JS 通过 CF7 DOM events
示例 - 提交表单时:

<script>
document.addEventListener( 'wpcf7submit', function( event ) {
    var inputs = event.detail.inputs;

    for ( var i = 0; i < inputs.length; i++ ) {
        if ( 'your-name' == inputs[i].name ) {
            alert( inputs[i].value );
            /*
            or in console:
            console.log( inputs[i].value );
            */
            break;
        }
    }
}, false );
</script>