WordPressの管理画面の設定に電話番号、住所等基本情報入力項目の追加方法をご紹介します。
オプション項目を追加することで1カ所でそれぞれを管理することができ変更の際などに対処しやすくなります。
まずは、function.phpに設定を追記します。
<?php
function add_contact_info_field( $whitelist_options ) {
$whitelist_options['general'][] = 'address_num';
$whitelist_options['general'][] = 'address';
$whitelist_options['general'][] = 'tel';
$whitelist_options['general'][] = 'fax';
return $whitelist_options;
}
add_filter( 'whitelist_options', 'add_contact_info_field' );
function regist_contact_info_field() {
add_settings_field( 'address_num', '郵便番号', 'display_address_num', 'general' );
add_settings_field( 'address', '住所', 'display_address', 'general' );
add_settings_field( 'tel', '電話番号', 'display_tel', 'general' );
add_settings_field( 'fax', 'FAX', 'display_fax', 'general' );
}
add_action( 'admin_init', 'regist_contact_info_field' );
function display_address_num() {
$address_num = get_option( 'address_num' );
?>
<input name="address_num" type="text" id="address_num" value="<?php echo esc_html( $address_num ); ?>" class="regular-text">
<?php
}
function display_address() {
$address = get_option( 'address' );
?>
<input name="address" type="text" id="address" value="<?php echo esc_html( $address ); ?>" class="regular-text">
<?php
}
function display_tel() {
$tel = get_option( 'tel' );
?>
<input name="tel" type="text" id="tel" value="<?php echo esc_html( $tel ); ?>" class="regular-text">
<?php
}
function display_fax() {
$fax = get_option( 'fax' );
?>
<input name="fax" type="text" id="fax" value="<?php echo esc_html( $fax ); ?>" class="regular-text">
<?php
}
これで設定が完了です。
管理画面の設定>一般の下部に以下のように追加せれていれば大丈夫です。

利用する場合は、以下のようにします。
郵便番号、住所を表示したい場合は以下の通りです。
<?php echo "〒".get_option( 'address_num' ); echo get_option( 'address' ); ?>
また、ショートコード化することでさらに利用の幅が広がります。
ショートコード化する方法は以下の通りです。
<?php
function option_address_num() {
return get_option('address_num');
}
add_shortcode('option_address_num', 'option_address_num');
function option_address() {
return get_option('address');
}
add_shortcode('option_address', 'option_address');
function option_tel() {
return get_option('tel');
}
add_shortcode('option_tel', 'option_tel');
function option_fax() {
return get_option('fax');
}
add_shortcode('option_fax', 'option_fax');
?>
ショートコード は投稿などのエディターやカスタムフォールドで利用できます。
利用方法は以下の通りです。
電話番号:[option_tel]
ただし、カスタムフィールドで利用する際には注意が必要です。
項目には、上記のように設定し表示する際には以下のようにします。
<?php
echo apply_filters('the_content', get_post_meta($post->ID, 'custom_field', true));
?>
以上、Wordpressの管理画面の設定に電話番号、住所等基本情報入力項目の追加方法のご紹介でした。
電話番号や住所などの他には必要に応じて追加することで保守がしやすくなります。