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

时间:2019-06-05 21:35:00

标签: php wordpress 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);
}

1 个答案:

答案 0 :(得分:0)

CF7使用AJAX提交表单,您无法以普通方式看到var_dump()。因此,通过PHP,您可以使用WordPress debug.log文件。在“ 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
}

作为选项,您可以使用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>