テーマがアクティブ化されたときにフィールドのデフォルト値を取得できるように、WordPress のテーマ オプションにいくつかのデフォルト値を設定したいと考えています。以下は私のコードです。テーマオプションページにデフォルト値が表示されますが、オプションを保存する前に変数のデフォルト値を取得できません。保存する前にテーマオプションからデフォルト値を取得する方法はありますか? ありがとう。
//set default options
$sa_options = array(
'footer_copyright' => '© ' . date('Y') . ' ' . get_bloginfo('name'),
'intro_text' => 'some text',
'featured_cat' => ''
);
//register settings
function sa_register_settings() {
register_setting( 'sa_theme_options', 'sa_options', 'sa_validate_options' );
}
add_action( 'admin_init', 'sa_register_settings' );
//add theme options page
function sa_theme_options() {
add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'theme_options', 'sa_theme_options_page' );
}
add_action( 'admin_menu', 'sa_theme_options' );
// Function to generate options page
function sa_theme_options_page() {
global $sa_options;
<?php if ( false !== $_REQUEST['updated'] ) : ?>
<div class="updated fade"><p><?php _e( 'Options saved' ); ?></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php $settings = get_option( 'sa_options', $sa_options ); ?>
<?php settings_fields( 'sa_theme_options' ); ?>
<input id="footer_copyright" name="sa_options[footer_copyright]" type="text" value="<?php esc_attr_e($settings['footer_copyright']); ?>" />