見出し画像

【WordPress】カスタム設定項目:チェックボックスの設置

概要:

functions/option.php を作成し、functions.phpでそちらを読み込む。
get_template_part( 'functions/option' );

見本:

補足:
「オプション設定」というブロック自体も設置。
その中に、ボタンの表示・非表示を制御する「showbtn」という名前の項目を作成。

<?php

function add_custom_options_fields_option( $whitelist_options ) {
 $whitelist_options['general'][] = 'showbtn';
 return $whitelist_options;
}
add_filter( 'whitelist_options', 'add_custom_options_fields_option' );

function add_general_custom_sections_option() {

 add_settings_section( 'option', 'オプション設定', 'option_section_message', 'general' );
 add_settings_field( 'showbtn', 'ボタンの表示', 'custom_showbtn_field', 'general', 'option', array( 'label_for' => 'showbtn' ) );
}
add_action( 'admin_init', 'add_general_custom_sections_option' );

function option_section_message() {
?>

<?php
}

function custom_showbtn_field( $args ) {
 $showbtn = get_option( 'showbtn' );
?>
 <label><input name="showbtn" id="showbtn" type="checkbox" value="1" <?php checked(1, get_option('showbtn')); ?> />表示する</label>
<?php
}

?>

テンプレート側での分岐:

※調査中

参考記事:


この記事が気に入ったらサポートをしてみませんか?