LoginSignup
12
12

More than 5 years have passed since last update.

【Wordpress】管理画面の設定に項目を追加する

Last updated at Posted at 2015-10-24

忘れないようにメモ。詳細はそのうち追記予定。

まずは、function.phpに設定を追記する。
例として、設定 > 一般にサイトタイトル2とキャッチフレーズ2を追加してみる。

function.php
/*
 * 一般設定に項目を追加
 */
function add_contact_info_field( $whitelist_options ) {
    $whitelist_options['general'][] = 'blog_title2';
    $whitelist_options['general'][] = 'catch_phrase2';
    return $whitelist_options;
}
add_filter( 'whitelist_options', 'add_contact_info_field' );

function regist_contact_info_field() {
    add_settings_field( 'blog_title2', 'サイトのタイトル2', 'display_blog_title', 'general' );
    add_settings_field( 'catch_phrase2', 'キャッチフレーズ2', 'display_catch_phrase', 'general' );
}
add_action( 'admin_init', 'regist_contact_info_field' );

function display_blog_title() {
    $blog_title = get_option( 'blog_title2' );
?>
    <input name="blog_title2" type="text" id="blog_title2" value="<?php echo esc_html( $blog_title ); ?>" class="regular-text">
<?php
}

function display_catch_phrase() {
    $catch_phrase = get_option( 'catch_phrase2' );
?>
    <input name="catch_phrase2" type="text" id="catch_phrase2" value="<?php echo esc_html( $catch_phrase ); ?>" class="regular-text">
<?php
}

設定はこれで完了。
表示する時はget_option使用する。

header.php
<?php

echo get_option( 'blog_title2' );
echo get_option( 'catch_phrase2' );

?>
12
12
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
12
12