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

概要説明

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

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

functions.php
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/*----------------------------------------------------
ユーザー編集ページに任意の項目を追加
----------------------------------------------------*/
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' );
}
/*---------------------------------------------------- ユーザー編集ページに任意の項目を追加 ----------------------------------------------------*/ 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' ); }
/*----------------------------------------------------
 ユーザー編集ページに任意の項目を追加
----------------------------------------------------*/
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 ユーザー編集ページに任意の項目を追加

追加した情報の表示

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php esc_attr( get_the_author_meta( 'branch', $user->ID )); ?>
<?php esc_attr( get_the_author_meta( 'branch', $user->ID )); ?>
<?php esc_attr( get_the_author_meta( 'branch', $user->ID )); ?>

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

関連機能

  1. WordPressのpost_typeを指定してビジュアルエディタを無効にする

  2. 新規記事作成時の初期値設定

  3. ローカル環境構築ツール「Local」でWordPress作業環境を構築 ( Windows,Mac対応 )

サイドバー

よく使うカスタマイズ

最近の記事

アニメの名言集

ぜーんぶひっくるめてのお前なんだ
いいんだそれで

バガボンド
by 沢庵宗彭

Profile

PAGE TOP
Amazon プライム対象