2018年7月 Archive

Wordpressで「設定」にTOPメッセージ欄を追加

■function.phpに

//--------------------------------------------------------
//一般設定に項目を追加
//--------------------------------------------------------
function add_contact_info_field( $whitelist_options ) {
    $whitelist_options['general'][] = 'blog_copyright';
    $whitelist_options['general'][] = 'toppage_message';
    return $whitelist_options;
}
add_filter( 'whitelist_options', 'add_contact_info_field' );

function regist_contact_info_field() {
    add_settings_field( 'blog_copyright', 'コピーライト', 'display_blog_copyright', 'general' );
    add_settings_field( 'toppage_message', 'トップページのメッセージ', 'display_toppage_message', 'general' );
}
add_action( 'admin_init', 'regist_contact_info_field' );

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

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




TOPページにメッセージを入れる場合は入力している文字とボックスが表示。
空だとボックスごと非表示。
■TOPのテーマに

<?php if(get_option('toppage_message')): ?>
<div>
<p><?php echo get_option( 'toppage_message' );?></p>
</div>
<?php endif; ?>

1