ご注意!

この記事にはPHPとJavaScriptのコードが含まれており、開発者を対象としています。このコードは便宜上提供していますが、コードのカスタマイズやサードパーティの開発についてはサポートを提供していません。

追加のガイダンスについては、WPBeginner の カスタムコードの追加方法に関するチュートリアル を参照してください。

閉じる

説明

wpforms_conversational_forms_footer アクションは、会話型フォームアドオンで使用されるテンプレートのページ読み込みの最後にトリガーされます。

パラメーター

このアクションはパラメーターを受け付けません。

詳細情報

このアクションは、標準のWordPressのwp_footerアクションと非常によく似ています。ページ読み込みの最後に実行されますが、wpforms_conversational_forms_footerアクションに限定すると、会話型フォームのページテンプレートでのみ実行されます。

これは、会話型フォームアドオンで使用されるテンプレート用のカスタムJavaScriptがある場合に特に役立ちます。

ソース

wpforms-conversational-forms/src/Frontend.php

以下の例では、会話型フォームの下部にメッセージとカウントダウンタイマーを追加します。

/**
 * Action to be called once the conversational form has completely loaded.
 *
 * @link https://wpforms.com/developers/wpforms_conversational_forms_footer/
 */
function wpf_cf_countdown_timer() {
    ?>
        // Start our countdown wrapper and block
    <div class="timer_wrapper"><?php _e('The countdown has begun!')?><div id="demo"></div></div>
 
        // Run the script for the countdown
    <script type="text/javascript">
 
        // Set the date we're counting down to
        var countDownDate = new Date( "2030-12-24T12:00:00" ).getTime();
 
        // Update the count down every 1 second
        var x = setInterval( function() {
 
            // Get today's date and time
            var now = new Date().getTime();
 
            // Find the distance between now and the count down date
            var distance = countDownDate - now;
 
            // Time calculations for days, hours, minutes and seconds
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);
 
            // Output the result in an element with id="demo"
            document.getElementById("demo").innerHTML = days + "d " + hours + "h "
                + minutes + "m " + seconds + "s ";
 
            // If the count down is over, write some text
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("demo").innerHTML = "EXPIRED";
            }
        }, 1000);
    </script>
    <?php
}
add_action( 'wpforms_conversational_forms_footer', 'wpf_cf_countdown_timer', 1 );