douchan4674 2019-06-05 21:35
浏览 98
已采纳

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

I want to see what is inside "$name" variable, how can i do that?

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条回答 默认 最新

  • douliao8760 2019-06-05 22:29
    关注

    CF7 uses AJAX to submit forms and you can't see var_dump() in ordinary way. So through PHP you can use WordPress debug.log file. Iside "wp-config.php" instead of define('WP_DEBUG', false); write:

    // 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 );
    

    Then:

    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
    }
    

    As option you can do that in front-end with JS thru the CF7 DOM events
    Example - when your form is submit:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度