首页 > 解决方案 > 如何使用插件钩子使用 var_dump() 或 printr() 从 Contact Form 7 表单中显示提交的值

问题描述

我想看看“$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);
}

标签: phpwordpresscontact-form-7

解决方案


var_dump()CF7使用AJAX提交表单,普通方式是看不到的。所以通过 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
}

作为选项,您可以通过CF7 DOM 事件 示例在前端使用 JS 执行此
操作 - 当您的表单提交时:

<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>

推荐阅读