Custom blog settings

What is the best way to add new custom blog settings to the WordPress general settings (or some other settings page for that matter), such that these settings are available to the theme to use?

For example I would like simple settings like the Twitter URL and Facebook Page URL to be configurable, rather than hardcoded in the theme. It would be nice if I could then access them via the bloginfo() function, but any other function will do just the same.

Related posts

2 comments

  1. Here is part of a class I wrote for client recently:

    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';
        }
    
    }
    
  2. OK, I was evidently looking at the wrong place. The Settings API offers all the functions to register new settings, adding sections, customising the admin settings forms etc.

    What isn’t clear from the documentation is how to retrieve those settings when you need them in your theme. If I understood correctly the get_option() function does the trick.

Comments are closed.