在主题所在目录下的functions.php中添加如下代码:
/**
* WordPress 添加额外选项字段到常规设置页面
*/
$new_general_setting = new new_general_setting();
class new_general_setting {
function new_general_setting( ) {
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
function register_fields() {
register_setting( 'general', 'favorite_color', 'esc_attr' );
add_settings_field('favorite_color', '<label for="favorite_color">'.__('最喜欢的颜色' ).'</label>' , array(&$this, 'fields_html') , 'general' );
}
function fields_html() {
$value = get_option( 'favorite_color', '' );
echo '<input type="text" id="favorite_color" name="favorite_color" value="' . $value . '" />';
}
}
* WordPress 添加额外选项字段到常规设置页面
*/
$new_general_setting = new new_general_setting();
class new_general_setting {
function new_general_setting( ) {
add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
}
function register_fields() {
register_setting( 'general', 'favorite_color', 'esc_attr' );
add_settings_field('favorite_color', '<label for="favorite_color">'.__('最喜欢的颜色' ).'</label>' , array(&$this, 'fields_html') , 'general' );
}
function fields_html() {
$value = get_option( 'favorite_color', '' );
echo '<input type="text" id="favorite_color" name="favorite_color" value="' . $value . '" />';
}
}
可以根据需要修改其中的字段名。
在前台使用get_option()函数调用
<?php echo get_option('favorite_color');?>