Custom blog settings

时间:2013-06-03 作者:jbx

将新的自定义博客设置添加到Wordpress常规设置(或其他一些设置页面)以便主题可以使用这些设置的最佳方法是什么?

例如,我希望Twitter URL和Facebook页面URL等简单设置可以配置,而不是在主题中硬编码。如果我可以通过bloginfo() 函数,但任何其他函数都会执行相同的操作。

2 个回复
最合适的回答,由SO网友:Charles Clarkson 整理而成

以下是我最近为客户编写的一个类的一部分:

if ( is_admin() )
    RevenueCalculatorAdminSettings::init();

/**
 * Call this class with:
 *
 *    RevenueCalculatorAdminSettings->init();
 *
 *     or
 *
 *    RevenueCalculatorAdminSettings->init( \'text_domain\' );
 *
 */
class RevenueCalculatorAdminSettings {

    public static $text_domain = \'\';

    /**
     * Add actions to produce new settings.
     *
     * @param string $text_domain The text domain to use for localization. Default is \'\'.
     */
    public static function init( $text_domain = \'\' ) {

        self::$text_domain = $text_domain;

        add_action( \'admin_menu\', array( __CLASS__, \'admin_settings_section\' ) );
        add_action( \'admin_init\', array( __CLASS__, \'register_settings\' ) );
    }

    /**
     * Register new settings.
     */
    public static function register_settings() {
        register_setting( \'general\', \'commissions_calculator_commission\', array( __CLASS__, \'validate_commission\' ) );
        register_setting( \'general\', \'commissions_calculator_month_format\', array( __CLASS__, \'validate_month_format\' ) );
    }

    /**
     * Adds new settings section to the Settings - General page.
     */
    public static function admin_settings_section() {
        add_settings_section( \'commissions-calculator\', __( \'Commissions Calculator\', self::$text_domain ), array( __CLASS__, \'commissions_calculator_fields\' ), \'general\' );
    }

    /**
     * Adds new settings field(s).
     */
    public static function commissions_calculator_fields() {
        add_settings_field( \'commissions-calculator-commission\', __( \'Commission\', self::$text_domain ), array( __CLASS__, \'commission\' ), \'general\', \'commissions-calculator\', array( \'label_for\' => \'commissions-calculator-commission\' ) );
        add_settings_field( \'commissions-calculator-month_format\', __( \'Month Format\', self::$text_domain ), array( __CLASS__, \'month_format\' ), \'general\', \'commissions-calculator\', array( \'label_for\' => \'commissions-calculator-month-format\' ) );
    }

    /**
     * Echo markup for Commission setting field.
     */
    public static function commission() {

        /**
         * id    commissions-calculator-commission
         * value get_option( \'commissions_calculator_commission\' )
         */

        printf( \'<input id="commissions-calculator-commission" name="commissions_calculator_commission" type="text" value="%s" class="small-text" /><label for="commissions-calculator-commission">%s</label>\', get_option( \'commissions_calculator_commission\' ),  __( \' For 60%, use 0.60.\', self::$text_domain ) );
    }

    /**
     * Validate Commission option.
     *
     * @param string|array $tainted_input Tainted input from the admin form. Do not use this data without cleaning.
     */
    public static function validate_commission( $tainted_input ) {

        if ( is_numeric( $tainted_input ) )
            return $tainted_input;

        add_settings_error( \'commissions-calculator\', \'commissions-calculator-commission\', __( \'Commission must be numeric. Defaulting to 60%\', self::$text_domain ) );

        return \'0.60\';
    }

    /**
     * Echo markup for Month Format setting field.
     */
    public static function month_format() {

        /**
         * id    commissions-calculator-month_format
         * value get_option( \'commissions_calculator_month_format\' )
         */

        /** Use \'F\' in date for full long Month names.
            Use \'M\' in date for short, 3-letter Month names. */

        printf(
            \'<label><input name="commissions_calculator_month_format" type="radio" value="F" %s/> <span>%s</span></label><br />
             <label><input name="commissions_calculator_month_format" type="radio" value="M" %s/> <span>%s</span><label>\',

             checked( get_option( \'commissions_calculator_month_format\' ), \'F\', false ),
             __( \'Full month names (January, Februrary, etc.) in top row.\', self::$text_domain ),

             checked( get_option( \'commissions_calculator_month_format\' ), \'M\', false ),
             __( \'Short month names (Jan, Feb, etc.) in top row.\', self::$text_domain )
        );
    }

    /**
     * Validate Month Format option.
     *
     * @param string|array $tainted_input Tainted input from the admin form. Do not use this data without cleaning.
     */
    public static function validate_month_format( $tainted_input ) {

        /** Default to \'M\'. Convert formats other than \'M\' and \'F\' to \'M\'. */
        if ( \'F\' == $tainted_input )
            return \'F\';
        else
            return \'M\';
    }

}

SO网友:jbx

好吧,我显然看错地方了。这个Settings API 提供注册新设置、添加分区、自定义管理设置表单等所有功能。

文档中不清楚的是如何在主题中需要这些设置时检索它们。如果我理解正确get_option() 函数起作用。

结束

相关推荐

注意:未定义的索引:在Options-Framework.php中

我在选项框架中生成以下通知。php。注意:未定义索引:在选项框架中。php我认为这与卫生处理有关,但不知道如何解决这个问题。我在选项中定义了以下数组。php// Pull all the custom taxonomies into an array $options_password_taxonomies = array(); $taxonomies_password_terms_obj = get_terms(\'password_gallery_category\'); fo