ご注意!

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

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

閉じる

説明

サイト全体で確認メッセージの出力をフィルタリングします。

パラメーター

$メッセージ
(string) (必須) スマートタグを処理した確認メッセージ。
$form_data
(array) (必須) 後で使用するために準備された、処理済みのフォーム設定/データ。
$fields
(array) (必須) サニタイズされたフィールドデータ。
$entry_id
(int) (必須) エントリID。

ソース

wpforms/src/Frontend/Frontend.php

詳細情報

このフィルターは、確認メッセージを表示するように設定されたすべてのフォームに適用されます。フォームビルダー内で設定されたメッセージの内容を上書きして、表示される確認メッセージを変更または置き換えるために使用できます。

以下の例では、フォームに入力したユーザー名を取得し、確認メッセージに対してよりパーソナライズされたメッセージを表示します。

コードを実行したい特定のフォームIDに合わせて、フォームIDを25から変更することを忘れないでください。このチェックを削除すると、すべてのフォームで実行されます。

/**
 * Filters confirmation message output site-wide.
 *
 * @link   https://wpforms.com/developers/wpforms_frontend_confirmation_message/
 * 
 * @param  string   $message     Confirmation message including Smart Tags.
 * @param  array    $form_data   Form data and settings.
 * @param  array    $fields      Sanitized field data.
 * @param  int      $entry_id    Entry ID.
 *
 * @return string
 */

function wpf_dev_frontend_confirmation_message( $message, $form_data, $fields, $entry_id ) {
    
    // Only run on my form with ID = 25
    if ( absint( $form_data[ 'id' ] ) !== 25 ) {
            return $message;
        } 

    // also it is possible to access the first, middle, and the last name as follows inplace of [ 'value' ]
    //  $contact_name = $fields[ '0' ][ 'first' ]; - this will pass in the first name
    //  $contact_name = $fields[ '0' ][ 'last' ]; - this will pass in the last name
    //  $contact_name = $fields[ '0' ][ 'middle' ]; - this will pass in the middle name in the format First Middle Last
         
    // Get the name field ID '0' to set the name for the message
    $contact_name = $fields[ '0' ][ 'value' ];
    
    // Add the name to the message
    $message = __('Thanks ' . $contact_name .  ' we will be in touch!', 'plugin-domain');
    return $message;
     
}
add_filter( 'wpforms_frontend_confirmation_message', 'wpf_dev_frontend_confirmation_message', 10, 4 );

参考記事