0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MW WP Formのパラメータ受取と動的リスト表示

Last updated at Posted at 2023-02-19

やりたいこと

・詳細ページから、お問い合わせのボタンを押すと、遷移先のフォームに遷移前のタイトルが入力されている。
・選択フォームの内容が動的にリスト表示される。

画面遷移のイメージ

スクリーンショット 2023-02-19 12.41.02.png

投稿記事作成

ホームページ内で求人情報を公開して応募できるシステムを想定。タクソノミーは勤務地などを設定。

カスタム投稿タイプ:search-jobs
タクソノミー:search-jobs_place
ターム:以下のようのに設定
スクリーンショット 2023-02-19 13.34.04.png

フォームを作成

プラグインのMW WP Formを使用。
MW WP Formの投稿を作成する際、設定の「URL引数を有効にする」にチェックを入れることで遷移前の情報を渡すことができる。
スクリーンショット 2023-02-19 12.58.08.png

今回使用するショートコードはこちら。
また作成する際、以下のようにvalue="{post_title}"でパラメータを受け取れる。

MW WP Form
<div class="group">
    <label for="chosedjob">選択したお仕事</label>
    [mwform_text name="chosedjob" id="chosedjob" value="{post_title}"]
</div>
<div class="group">
    <label for="chosedarea">勤務地</label>
    [mwform_radio name="chosedarea" id="chosedarea" children=""]
</div>
<div class="group">
    <label for="chosedarea">お名前</label>
    [mwform_text name="chosedarea" id="fullname"]
</div>
<div class="group">
    <label for="chosedarea">メールアドレス</label>
    [mwform_email name="email" id="email"]
</div>
<div class="group">
    [mwform_submitButton name="send" confirm_value="確認画面へ" submit_value="送信する"]
    [mwform_backButton value="戻る"]
</div>

続いてフィルターフックでパラメータを受け取ったフォームの非活性化と、動的にリスト表示するコードを書く。

functions.php
// 非活性に変更(MW WP Form)
function my_mwform_textarea_shortcode_tag( $output, $tag, $attr ) {
	if ( $tag == 'mwform_text' && $attr['name'] == 'chosedjob' ) {
        $output = str_replace( '<input ', '<input disabled ', $output );
    }
    return $output;
}
add_filter( 'do_shortcode_tag', 'my_mwform_textarea_shortcode_tag', 10, 3 );

// 動的リスト表示(MW WP Form)
$form_id = 16;
function add_custom_taxonomy_to_form( $children, $atts ) {
    $taxonomy = 'search-jobs_place';
    $terms = get_terms( $taxonomy, array( 'hide_empty' => false ) );
    if ( ! empty( $terms ) && $atts['name'] == 'chosedarea' ) {
        foreach ( $terms as $term ) {
            $children[$term->term_id] = $term->name;
        }
    }
    return $children;
}
add_filter( 'mwform_choices_mw-wp-form-' . $form_id, 'add_custom_taxonomy_to_form', 10, 2 );

ここまでやって、見た目はこんな感じ。
スクリーンショット 2023-02-19 13.27.13.png

動的なリスト表示はOK。

確認

パラメータが受け取れるか確認していくが、今回は画像のようなカスタム投稿で作成した。

スクリーンショット 2023-02-19 13.03.17.png

また遷移前のページのリンクはこのような書き方になる

page.php
<?php
    $post_id = get_the_ID();
    $link = add_query_arg( 'post_id', $post_id, site_url( '/contact_r/' ) );
    echo $link;
?>
<a class="btnBase btnSecondary" href="<?php echo esc_url( $link ); ?>">応募する</a>

この投稿のページから問い合わせページに遷移すると、遷移先のURLに遷移前のポストIDが、value="{post_title}"を入れたフォームには遷移前のタイトルが入力されている。

スクリーンショット 2023-02-19 13.00.23.png

スクリーンショット 2023-02-19 13.30.58.png

今後実装したいこと

・遷移前にチェックした項目を遷移後にも反映
→おそらく公式のマニュアルにあると思うが、正直探すのが大変・・・

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?