zoukankan      html  css  js  c++  java
  • WordPress 后台添加额外选项字段到常规设置页面

    有时候我们需要添加一些额外的设置选项到常规设置(后台 > 设置 > 常规)页面,下面是一个简单的范例:

    add-field-to-general-settings-page-wpdaxue_com

    直接添加到主题的 functions.php 即可:

     
    /**
    * WordPress 添加额外选项字段到常规设置页面
    * http://www.wpdaxue.com/add-field-to-general-settings-page.html
    */

    $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('fav_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 . '" />';
      }
    }
    根据自己的需要修改 11、12、15、16 行的代码即可,这里只是简单的 input 表单,你也可以直接添加其他表单类型。

    使用 get_option() 函数即可调用选项字段的值,比如上面的例子用 get_option( 'favorite_color' );

    http://www.wpdaxue.com/add-field-to-general-settings-page.html





  • 相关阅读:
    闭包 (Closure)
    RSA算法
    HTTPS
    SSH
    HDU1754 I hate it_线段树(入门级别)
    HDU1166 敌兵布阵_线段树
    c++运算符优先级表
    归并排序练习.
    HDU 1969 精度二分
    uva10944 状态压缩bfs or DP
  • 原文地址:https://www.cnblogs.com/huangtailang/p/4265998.html
Copyright © 2011-2022 走看看