4
5

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 5 years have passed since last update.

wordpressのユーザーページに入力項目を追加する

Last updated at Posted at 2015-08-03

ユーザー→ユーザーについて→プロフィール情報の下に入力項目を追加する

テキストエリアを追加する場合

function.php
<?php 
//ユーザーのプロフィール情報下にテキストエリアを追加
function add_user_essay_form( $bool ) {
    global $profileuser;
    if ( preg_match( '/^(profile\.php|user-edit\.php)/', basename( $_SERVER['REQUEST_URI'] ) ) ) {
?>
    <tr>
        <th scope="row"><label for="user_essay">肩書き</label></th>
            <td>
				<textarea name="user_essay" rows="5" cols="30" onfocus="this.select()"><?php echo esc_attr( $profileuser->user_essay ); ?></textarea>
                <p>職務や経歴などを記入してください。</p>
            </td>
        </tr>
<?php
    }
    return $bool;
}
add_action( 'show_password_fields', 'add_user_essay_form' );

function update_user_essay( $user_id, $old_user_data ) {
    if ( isset( $_POST['user_essay'] ) && $old_user_data->user_essay != $_POST['user_essay'] ) {
        $user_essay = wp_filter_kses( $_POST['user_essay'] );
        update_user_meta( $user_id, 'user_essay', $user_essay );
    }
}
add_action( 'profile_update', 'update_user_essay', 10, 2 );
?>

テキスト項目を追加する場合

function.php
<?php

function add_user_data_form($bool)
{
	global $profileuser;
	if ( preg_match('/^(profile\.php|user-edit\.php)/', basename($_SERVER['REQUEST_URI'])) )
	{ ?>

<tr>
	<th scope="row">項目1</th>
	<td>
		<input type="text" name="user_data1" id="user_data1" value="<?php echo esc_html($profileuser->user_data1); ?>" />
	</td>
</tr>

<?php }
	return $bool;
}
add_action('show_password_fields', 'add_user_data_form');

function update_user_data($user_id, $old_user_data)
{
	if ( isset($_POST['user_data1']) && $old_user_data->user_data1 != $_POST['user_data1'] )
	{
		$user_data1 = sanitize_text_field($_POST['user_data1']);
		$user_data1 = wp_filter_kses($user_data1);
		$user_data1 = _wp_specialchars($user_data1);

		update_user_meta($user_id, 'user_data1', $user_data1);
	}
}
add_action('profile_update', 'update_user_data', 10, 2);

?>

出力する

テキストエリア

echo nl2br($userdata->user_essay);

テキスト

<?php $userdata = get_userdata('ユーザーID'); ?>
<?php echo esc_html($userdata->user_data1); ?>

https://ja.forums.wordpress.org/topic/8406
http://wordpress-design.net/archives/1927
http://aroun-d.com/2012/06/04/3917/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?