LoginSignup
6
4

More than 5 years have passed since last update.

WordPressのユーザーに独自項目を追加

Last updated at Posted at 2018-05-10

概要

古いバージョンのWordPressでwp_usersを利用して会員情報を運用しているケースがあり、
どうしてもユーザー情報に管理項目を追加したい要件があったので、調べた内容をメモしておきます。

追加方法

functions.phpに下記内容を追加することで対応できそうです。

functions.php
<?php

  ...


// ユーザーの管理項目を追加
add_action('show_user_profile', 'Add_user_fields');
add_action('edit_user_profile', 'Add_user_fields');
function Add_user_fields($user) {
?>
    <h3>管理項目</h3>
    <table class="form-table">
        <tr>
            <th><label for="mail_magazine_stop">メルマガ配信停止</label></th>
            <td>
                <?php $selected = get_the_author_meta('mail_magazine_stop', $user->ID); ?>
                <select name="mail_magazine_stop" id="mail_magazine_stop">
                    <option value="yes" <?php echo ($selected == "yes")?  'selected="selected"' : '' ?>>停止する</option>
                    <option value="" <?php echo ($selected != "yes")?  'selected="selected"' : '' ?>>停止しない</option>
                </select>
            </td>
        </tr>
    </table>
<?php
}
add_action('personal_options_update', 'save_user_fields');
add_action('edit_user_profile_update', 'save_user_fields');
function save_user_fields($user_id) {
    if (! current_user_can('edit_user', $user_id))
        return false;

    update_usermeta($user_id, 'mail_magazine_stop', $_POST['mail_magazine_stop']);
}

追加した感じはこのようになります。
キャプチャ.png

参考サイト

6
4
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
6
4