As this is my first proper PHP class, I'm looking for suggestions of ways that I could refactor or do things more efficiently. Perhaps places where I could write code differently/use different functions to improve it. I'm not asking for a full rewrite – it's part of my learning process to do it myself.
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptionsThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
As this is my first proper PHP class, I'm looking for suggestions of ways that I could refactor or do things more efficiently. Perhaps places where I could write code differently/use different functions to improve it. I'm not asking for a full rewrite – it's part of my learning process to do it myself.
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
As this is my first proper PHP class, I'm looking for suggestions of ways that I could refactor or do things more efficiently. Perhaps places where I could write code differently/use different functions to improve it.
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
*/
class ThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
foreach ( $this->pages as if$page ($page['type']=='menu')
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
else/**
if * Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections($page['type'])
== 'submenu' {
foreach( $this->sections as $section )
{
add_submenu_pageadd_settings_section(
$page['parent'],
__( $page['title'] )$section['id'],
__( $page['title'] $section['title']),
'administrator''', // Add the description for the section in $page['id'],later
array( &$this, 'displayOptionsPage' )$section['page']
);
}
}
}
/**
* Add the settings sectionsfields
*
* @since 1.0
*/
private function addOptionsSections()
{
*/
foreach( $this->sections asprivate $sectionfunction addOptionsFields()
{
add_settings_sectionforeach ($this->fields as $section => $field)
{
$section['id'] foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($section['title']$option['label']),
'' array(&$this, //'display' Add. theucfirst(strtolower($option['type'])) description. for'Field'),
the section in later $section,
$section['page'] $option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
}
/**
* AddRegister the settings fieldswith register_setting()
*
* @since 1.0
*/
private function addOptionsFields()
{
*/
foreach ($this->fields as $sectionprivate =>function $fieldregisterSettings()
{
foreach ($field$this->pages as $option$page)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
arrayregister_setting(
'option' => $section$page['id'],
'optionName' => $option['name']
)$page['id']
);
}
}
}
/**
* Register the settings with* register_setting()Display options page
*
* @since 1.0
*/
private function registerSettings()
{
public foreachfunction displayOptionsPage($this->pages as $page)
{
register_settingif (isset($_GET['tab']))
{
$page['id'] $activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
$page['id']?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
}
/**
* Display options page/**
*
* @since 1.0
*/
public function displayOptionsPage()
{
Set the pages, sections ifand (isset($_GET['tab']))fields
{ *
* @since 1.0
$activeTab = $_GET['tab'];
*/
}
public function elseinitializeThemeOptions()
{
$activeTab = strtolower( str_replace('_', '$this-', $_GET['page']) >setOptionsDefaults();
}
?>
<div class="wrap">
// <divCreate id="icon-themes"the class="icon32"></div>sections
<h2><?php _e( $this->pageTitle >addOptionsSections(); ?></h2>
<?php
// Create the settings_errors();fields
$this->displayOptionsTabs>addOptionsFields( $activeTab ); ?>
<form method="post" action="options.php">
// Register the settings
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button>registerSettings(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();}
} /**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab$output = '' )
{
if ('<h2 count($thisclass="nav->pages) > 0 )
{ tab-wrapper">';
$output foreach ( $this->pages as $page )
{
$currentTab = '<h2strtolower(str_replace('_', class="nav'-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-wrapper">';active' : '';
foreach ( $this->pages as $page ) $output .= sprintf(
{
$currentTab'<a =href="?page=%1$s&tab=%2$s" strtolower(str_replace('_',class="nav-tab%4$s" 'id="%2$s-'tab">%3$s</a>', $page['id']));,
$activeClass = $activeTab == $currentTab,
? ' nav-tab-active' : ''; $page['title'],
$activeClass
);
}
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s<'</a>',
$page['id'],
$currentTab,
$page['title'],h2>';
$activeClass
echo );$output;
}
}
/**
$output * Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.=0
'< */h2>';
private function displayOptionsSettings( $activeTab echo= $output;'' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
}
/**
* Display the sections and* fieldsHTML accordingoutput tofor thetext pagefield
*
* @param stringarray $activeTab;$option;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{*/
$currentTabpublic =function strtolowerdisplayTextField( str_replace('-',$option '_',= $activeTabarray() );
settings_fields({
$currentTab );
do_settings_sections $value = get_option( $currentTab$option['option'] );
}
/**
* HTML output for text field
* printf(
* @param array $option;
* @since 1.0
*/ '<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
public function displayTextField( $option = array str_replace()'_', '-', $option['optionName']),
{ $option['option'],
$value = get_option $option['optionName'],
sanitize_text_field($value[$option['optionName']])
$option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );}
printf(/**
* HTML output '<inputfor type="url"url id="%1$s"field
class="regular-text" name="%2$s[%3$s]" value="%4$s">',
*
* str_replace('_',@param '-',array $option['optionName']),$option;
* @since 1.0
$option['option'],*/
public function displayUrlField( $option $option['optionName'],= array() )
{
esc_url($value[$option['optionName']])
$value = get_option( $option['option'] );
}
/** printf(
* Set default values for the settings. '<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
* str_replace('_', '-', $option['optionName']),
* @since 1.0 $option['option'],
*/ $option['optionName'],
private function setOptionsDefaults esc_url($value[$option['optionName']])
{
$defaults = array();
}
foreach/**
($this->fields as $optionsName => $options* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
$defaults[$optionsName][$option['name']]if( =get_option( '';$option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
foreach/**
* Validate the input depending on it's type. Used in register_setting($defaults).
as $option => $value *
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
return }$input;
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator', $page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab ); ?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>', $page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}
<?php
/**
* A PHP Wrapper class to help set up an options page for the Wordpress API.
*
* @package Wordpress
* @subpackage ThemeName
* @version 1.0
*
* @author Paul Brophy <[email protected]>
*/
class PBThemeOptions
{
// Declare class variables
public $pages = array();
public $sections = array();
public $fields = array();
/**
* Construct
*
* @since 1.0
*/
public function __construct()
{
// Set the pages, sections and fields
add_action( 'admin_menu', array( &$this, 'addOptionsPages' ) );
add_action( 'admin_init', array( &$this, 'initializeThemeOptions' ) );
}
/**
* Add the options pages
*
* @since 1.0
* @todo Add support for other menu types
*/
public function addOptionsPages()
{
foreach ( $this->pages as $page )
{
if ($page['type']=='menu')
{
add_menu_page(
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
else if ($page['type'] == 'submenu')
{
add_submenu_page(
$page['parent'],
__( $page['title'] ),
__( $page['title'] ),
'administrator',
$page['id'],
array( &$this, 'displayOptionsPage' )
);
}
}
}
/**
* Add the settings sections
*
* @since 1.0
*/
private function addOptionsSections()
{
foreach( $this->sections as $section )
{
add_settings_section(
$section['id'],
__($section['title']),
'', // Add the description for the section in later
$section['page']
);
}
}
/**
* Add the settings fields
*
* @since 1.0
*/
private function addOptionsFields()
{
foreach ($this->fields as $section => $field)
{
foreach ($field as $option)
{
add_settings_field(
$option['name'],
__($option['label']),
array(&$this, 'display' . ucfirst(strtolower($option['type'])) . 'Field'),
$section,
$option['section'],
array(
'option' => $section,
'optionName' => $option['name']
)
);
}
}
}
/**
* Register the settings with register_setting()
*
* @since 1.0
*/
private function registerSettings()
{
foreach ($this->pages as $page)
{
register_setting(
$page['id'],
$page['id']
);
}
}
/**
* Display options page
*
* @since 1.0
*/
public function displayOptionsPage()
{
if (isset($_GET['tab']))
{
$activeTab = $_GET['tab'];
}
else
{
$activeTab = strtolower( str_replace('_', '-', $_GET['page']) );
}
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2><?php _e( $this->pageTitle ); ?></h2>
<?php
settings_errors();
$this->displayOptionsTabs( $activeTab );
?>
<form method="post" action="options.php">
<?php $this->displayOptionsSettings( $activeTab ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Set the pages, sections and fields
*
* @since 1.0
*/
public function initializeThemeOptions()
{
$this->setOptionsDefaults();
// Create the sections
$this->addOptionsSections();
// Create the fields
$this->addOptionsFields();
// Register the settings
$this->registerSettings();
}
/**
* Display the tabs
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsTabs( $activeTab = '' )
{
if ( count($this->pages) > 0 )
{
$output = '<h2 class="nav-tab-wrapper">';
foreach ( $this->pages as $page )
{
$currentTab = strtolower(str_replace('_', '-', $page['id']));
$activeClass = $activeTab == $currentTab ? ' nav-tab-active' : '';
$output .= sprintf(
'<a href="?page=%1$s&tab=%2$s" class="nav-tab%4$s" id="%2$s-tab">%3$s</a>',
$page['id'],
$currentTab,
$page['title'],
$activeClass
);
}
$output .= '</h2>';
echo $output;
}
}
/**
* Display the sections and fields according to the page
*
* @param string $activeTab;
* @since 1.0
*/
private function displayOptionsSettings( $activeTab = '' )
{
$currentTab = strtolower( str_replace('-', '_', $activeTab) );
settings_fields( $currentTab );
do_settings_sections( $currentTab );
}
/**
* HTML output for text field
*
* @param array $option;
* @since 1.0
*/
public function displayTextField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="text" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
sanitize_text_field($value[$option['optionName']])
);
}
/**
* HTML output for textarea field
*
* @param array $option;
* @since 1.0
*/
public function displayTextareaField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<textarea id="%1$s" name="%2$s[%3$s]" rows="5" cols="60">%4$s</textarea>',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_textarea($value[$option['optionName']])
);
}
/**
* HTML output for url field
*
* @param array $option;
* @since 1.0
*/
public function displayUrlField( $option = array() )
{
$value = get_option( $option['option'] );
printf(
'<input type="url" id="%1$s" class="regular-text" name="%2$s[%3$s]" value="%4$s">',
str_replace('_', '-', $option['optionName']),
$option['option'],
$option['optionName'],
esc_url($value[$option['optionName']])
);
}
/**
* Set default values for the settings.
*
* @since 1.0
*/
private function setOptionsDefaults()
{
$defaults = array();
foreach ($this->fields as $optionsName => $options)
{
$defaults[$optionsName] = array();
foreach($options as $option)
{
$defaults[$optionsName][$option['name']] = '';
}
}
foreach ($defaults as $option => $value)
{
if( get_option( $option ) == false )
{
add_option(
$option,
apply_filters( $option . '_defaults', $value )
);
}
}
}
/**
* Validate the input depending on it's type. Used in register_setting().
*
* @since 1.0
* @todo Write the function!!!
*/
public function validateOptions( $input )
{
return $input;
}
}