WordPress ユーザー編集ページに任意の項目を追加

概要説明

ユーザー情報にカスタムフィールド機能を使って任意の項目を追加させるカスタマイズ。スタッフページなどを既存のユーザー機能をカスタマイズさせる際に使用します。

コード – プロフィール項目を追加

functions.php
/*----------------------------------------------------
 ユーザー編集ページに任意の項目を追加
----------------------------------------------------*/
if ( !function_exists( 'add_custom_user_profile' ) ){

    function add_custom_user_profile( $user ) {
        echo '<h2>追加情報</h2>';

        echo '<table class="form-table">';
        echo '<tr>';
        echo '<th><label for="branch">所属店舗</label></th>';
        echo '<td><input type="text" name="branch" id="branch" value="'.esc_attr( get_the_author_meta( 'branch', $user->ID )).'" class="regular-text" /></td>';
        echo '</tr>';
        echo '</table>';
    }

    function save_custom_user_profile( $user_id ) {

        if ( !current_user_can( 'edit_user', $user_id ) ) {
            return FALSE;
        }

        update_user_meta( $user_id, 'branch', $_POST['branch'] );
    }

    add_action( 'show_user_profile', 'add_custom_user_profile' );
    add_action( 'edit_user_profile', 'add_custom_user_profile' );

    add_action( 'personal_options_update', 'save_custom_user_profile' );
    add_action( 'edit_user_profile_update', 'save_custom_user_profile' );
}
WordPress ユーザー編集ページに任意の項目を追加

追加した情報の表示

<?php esc_attr( get_the_author_meta( 'branch', $user->ID )); ?>

とすることで追加情報を表示させることも可能です

関連機能

  1. カテゴリ選択条件付き絞り込み検索を追加する

  2. WordPress お問合せフォームの郵便番号に入力したら住所を自動で入力する機能を追加 ( Contact Form 7 )

  3. 編集画面にnoindex設定を追加 ( カスタム投稿に対応 )

サイドバー

よく使うカスタマイズ

最近の記事

アニメの名言集

服だけ溶かす薬。男ってのはね
こういうの渡しておけば喜ぶんだよ

葬送のフリーレン
by フリーレン

Profile

PAGE TOP
Amazon プライム対象