Skip to content
Ben Huson edited this page Feb 11, 2015 · 15 revisions

Use the Site Settings plugin to create admin fields for storing short text snippets for use in your theme templates. For example, an email or phone number that is displayed via your header.php template.

It can also be used for storing data which you may need to reference such as the ID of a page or a post category which you use to feature posts on your home page.

Storing these settings in the admin is preferable to hardcoding them into your templates so they can be updated easily.

Form Field Types

By default, any field you add will be a standard text input field.

Currently textarea fields are also supported via the input parameter - see the address example below.

Select menus are supported and can also be used for displaying a list of posts or taxonomy terms.

<?php

// Basic Select
Site_Settings_Admin_Screen::add_field( 'my_select', __( 'Select' ), array(
	'input' => 'select',
	'data'  => array(
		'item_one'   => 'Item One',
		'item_two'   => 'Item Two',
		'item_three' => 'Item Three'
	)
) );

// Page Select
Site_Settings_Admin_Screen::add_field( 'my_page', __( 'Page' ), array(
	'input'     => 'select',
	'post_type' => 'page'
) );

// Category Select
Site_Settings_Admin_Screen::add_field( 'my_category', __( 'Category' ), array(
	'input'    => 'select',
	'taxonomy' => 'category'
) );

?>

Example Usage

In your theme's functions.php file (or as a new file in the MU plugins folder) add the following PHP code to:

  1. Add a copyright field to the Site Settings admin page.
  2. Create a Contact section on the page with fields for email, phone and address.
<?php

function my_site_settings_register() {

	// Main Settings
	Site_Settings_Admin_Screen::add_field( 'copyright', __( 'Copyright' ) );

	// Contact Details
	Site_Settings_Admin_Screen::add_section( 'contact', __( 'Contact Details' ) );
	Site_Settings_Admin_Screen::add_field( 'email', __( 'Email' ), array(
		'section' => 'contact'
	) );
	Site_Settings_Admin_Screen::add_field( 'phone_number', __( 'Phone Number' ), array(
		'section' => 'contact'
	) );
	Site_Settings_Admin_Screen::add_field( 'address', __( 'Address' ), array(
		'section' => 'contact',
		'input'   => 'textarea',
		'rows'    => 5
	) );

}
add_action( 'site_settings_register', 'my_site_settings_register' );

?>

Then, to use the phone number in your template:

<?php echo Site_Settings::get( 'phone_number' ); ?>

API

  • Site_Settings::get( $option )
    Get a setting.
  • Site_Settings::get_options()
    Get all settings.
  • Site_Settings_Admin_Screen::add_section( $section, $title )
    Add an admin section.
  • Site_Settings_Admin_Screen::add_field( $name, $title, $args )
    Add an admin field.

Actions

  • site_settings_register
    Hook on to this action to register admin sections and fields using the API functions.

Clone this wiki locally