我正在创建一个自定义的WordPress主题,在我的主题选项中,我一直在尝试创建一个用户可以编写自定义CSS的区域。
我正在尝试在我的主题中使用Ace Editor;编辑器按预期工作,但字段根本没有保存。
这是我的代码:
<h2>Custom Code</h2>
<?php
do_settings_fields('register_custom_code','custom_code_section');
settings_fields('cus_code_setting');
?>
</div>
<div class="clear"></div>
</div>
<?php
}
add_action('admin_init','register_custom_code');
function register_custom_code() {
register_setting( 'cus_code_setting', 'custom_css', 'custom_css_validation');
add_settings_section( 'custom_code_section', '', 'custom_code_settings_callback', 'register_custom_code' );
add_settings_field( 'custom_css', '', 'custom_css_callback', 'register_custom_code', 'custom_code_section' );
function custom_css_callback() {
// The default message that will appear
$custom_css_default = __( '/*
Welcome to the Custom CSS editor!
Please add all your custom CSS here and avoid modifying the core theme files, since that\'ll make upgrading the theme problematic. Your custom CSS will be loaded after the theme\'s stylesheets, which means that your rules will take precedence. Just add your CSS here for what you want to change, you don\'t need to copy all the theme\'s style.css content.
*/' );
$custom_css = get_option( 'custom_css', $custom_css_default );
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( 'Custom CSS' ); ?></h2>
<?php if ( ! empty( $_GET['settings-updated'] ) ) echo '<div id="message" class="updated"><p><strong>' . __( 'Custom CSS updated.' ) . '</strong></p></div>'; ?>
<form id="custom_css_form" method="post" action="options.php" style="margin-top: 15px;">
<?php settings_fields( 'cus_code_setting' ); ?>
<div id="custom_css_container">
<div name="custom_css" id="custom_css" style="border: 1px solid #DFDFDF; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; width: 100%; height: 200px; position: relative;"></div>
</div>
<textarea id="custom_css_textarea" name="custom_css" style="display: none;"><?php echo $custom_css; ?></textarea>
<p><input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ) ?>" /></p>
</form>
</div>
<?php
}
function custom_code_settings_callback() {}
}
?>
我觉得我正在弄乱register_setting()
/ settings_field()
,因为这些是用于处理保存设置选项的方法,但我不完全确定。
我已按照this tutorial尝试实施它。