サポート » 使い方全般 » ユーザー>プロフィール項目の追加項目でチェックボックスを利用したい

  • 解決済 dekirukun

    (@dekirukun)


    ユーザーの編集のプロフィール項目の追加を調べていて、
    function.php への追加

    function my_new_contactmethods( $contactmethods ) {
    /* 団体名 */
    $contactmethods['pref_hos_dantai'] = '団体名';
    return $contactmethods;
    }
    add_filter('user_contactmethods','my_new_contactmethods',10,1);

    の様な状態でのテキスト一行の項目追加は実装できるようになったのですが、

    チェックボックスをユーザーの編集のプロフィール項目へ追加したいのですがどのようにしたらよいかわかりません。

    初歩的なことなのかもしれませんが、WordPress codexは私には難しく読み解くことができませんでしたので、よろしくお願いいたします。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • user_contactmethodsではなくshow_user_profile, edit_user_profileを使えばできるようになるのではと思います。

    Codexは難しいとのことですが…例が載っているのでURLを貼っておきます。
    http://codex.wordpress.org/Plugin_API/Action_Reference/show_user_profile
    http://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile

    追加される場所はプロフィール編集ページのパスワードより下の部分となりますが
    input type="checkbox"が使えます。

    トピック投稿者 dekirukun

    (@dekirukun)

    Junko Nukagaさま ありがとうございます。

    例をコピペして、プロフィール情報へ追加項目でチェックボックスを表示することができました。

    /**
     * Show custom user profile fields
     * @param  obj $user The user object.
     * @return void
     */
    function numediaweb_custom_user_profile_fields($user) {
    ?>
    <table class="form-table">
    <tr>
    	<th width="21">
    		<label for="tc_location">お手本<?php _e('Location'); ?></label>
    	</th>
    	<td width="268">
    		<input type="text" name="tc_location" id="tc_location" value="<?php echo esc_attr( get_the_author_meta( 'tc_location', $user->ID ) ); ?>" class="regular-text" />
    		<br><span class="description"><?php _e('お手本', 'travelcat'); ?></span>
    	</td>
    </tr>
    
    <tr>
    <th>
    <label for="label_dpc"><?php _e('左ラベル', 'DPC'); ?></label>
    </th>
    <td>
    <span class="description">これを作成<?php _e('説明文', 'dpc'); ?></span><BR />
      <input type="checkbox" name="dpc" value="<?php echo esc_attr( get_the_author_meta( 'dpc1', $user->ID ) ); ?>"/>
      <label for="checkbox">DPC1</label><BR />
      <input type="checkbox" name="dpc" value="<?php echo esc_attr( get_the_author_meta( 'dpc2', $user->ID ) ); ?>"/>
      <label for="checkbox">DPC2</label><BR />
      <input type="checkbox" name="dpc" value="<?php echo esc_attr( get_the_author_meta( 'dpc3', $user->ID ) ); ?>"/>
      <label for="checkbox">DPC3</label><BR />
    </td></tr>
    </table>
    <?php
    }
    add_action('show_user_profile', 'numediaweb_custom_user_profile_fields');
    add_action('edit_user_profile', 'numediaweb_custom_user_profile_fields');

    上記の様にして、「プロフィールを更新」を押しても値が保存されませんでした。

    下記の様な記入が必要なのかな?と上記の下に追記をして試してみましたが、値を更新して保存/表示を行うにはどのようなことが必要でしょうか?

    add_action('edit_user_profile_update', 'update_extra_profile_fields');
    
    function update_extra_profile_fields($user_id) {
             update_user_meta($user_id, 'dpc', $_POST['dpc']);
     }

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「ユーザー>プロフィール項目の追加項目でチェックボックスを利用したい」には新たに返信することはできません。