Created
August 23, 2013 08:55
-
-
Save dnaber-de/6317043 to your computer and use it in GitHub Desktop.
Settings-API-Helper example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example\Theme; | |
class Theme_Options { | |
/** | |
* the option key | |
* | |
* @const string | |
*/ | |
const KEY = '_ex_theme_header'; | |
/** | |
* the group key | |
* | |
* @const string | |
*/ | |
const SECTION = '_ex_theme_header_section'; | |
/** | |
* settings api object | |
* | |
* @var \Settings_API_Helper | |
*/ | |
public $settings; | |
/** | |
* init the settings API | |
* | |
* @wp_hook admin_init | |
* @return void | |
*/ | |
public function init_settings() { | |
$this->settings = new \Settings_API_Helper( | |
self::KEY, | |
self::SECTION, # use this as identifier to render the fields in the view using do_settings_section() | |
__( 'Title', 'ex-theme' ), | |
__( 'Caption', 'ex-theme' ) | |
); | |
# Testinput field | |
$this->settings->add_text( | |
'slug', | |
__( 'Label', 'ex-theme' ), | |
array( | |
'default' => '', | |
'output_callback' => 'esc_attr', | |
'class' => 'large-text', | |
'html_after' => '<p>Various stuff</p>' | |
) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example\Theme; | |
class Theme_Options_Admin_Page { | |
/** | |
* the slug of the parent page | |
* | |
* @var string | |
*/ | |
public $parent_slug; | |
/** | |
* the title of the page | |
* | |
* @var string | |
*/ | |
public $page_title; | |
/** | |
* the title of the menu item | |
* | |
* @var string | |
*/ | |
public $menu_title; | |
/** | |
* required capability to view the page | |
* | |
* @var string | |
*/ | |
public $capability; | |
/** | |
* the menu slug | |
* | |
* @var string | |
*/ | |
public $menu_slug; | |
/** | |
* constructor | |
* | |
* @return Example\Theme\Theme_Options_Admin_Page | |
*/ | |
public function __construct() { | |
$this->parent_slug = 'themes.php'; | |
$this->page_title = __( 'Theme-Settings', 'ex-theme' ); | |
$this->menu_title = __( 'Theme-Settings', 'ex-theme' ); | |
$this->capability = 'edit_themes'; | |
$this->menu_slug = 'ex_theme_options'; | |
} | |
/** | |
* register the page | |
* | |
* @return void | |
*/ | |
public function register() { | |
add_submenu_page( | |
$this->parent_slug, | |
$this->page_title, | |
$this->menu_title, | |
$this->capability, | |
$this->menu_slug, | |
array( $this, 'render_page' ) | |
); | |
} | |
/** | |
* the page template | |
* | |
* @return void | |
*/ | |
public function render_page() { | |
?> | |
<div class="wrap"> | |
<h2><?php echo $this->page_title;?></h2> | |
<div id="poststuff" class="metabox-holder has-right-sidebar"> | |
<div id="post-body"> | |
<div id="post-body-content"> | |
<div id="normal-sortables" class="meta-box-sortables ui-sortable"> | |
<div class="postbox inside"> | |
<form method="post" action="options.php"> | |
<?php | |
#displays hidden fields and stuff | |
settings_fields( \Example\Theme\Theme_Options::SECTION ); | |
do_settings_sections( \Example\Theme\Theme_Options::SECTION ); | |
?> | |
<div class="inside"> | |
<p> | |
<input type="submit" class="button-primary" name="submit" value="<?php esc_attr_e( 'Save', 'ex-theme' ); ?>" /> | |
</p> | |
</div> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment