Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4384579 to your computer and use it in GitHub Desktop.
Save anonymous/4384579 to your computer and use it in GitHub Desktop.
<?php
/**
* This function introduces the theme options into the 'Appearance' menu and into a top-level
* 'Sandbox Theme' menu.
*/
function ccms_developer_theme_menu() {
$options = get_option('ccms_developer_options');
if( $options['show_admin_dev'] ==1 ){
add_menu_page(
'', // The value used to populate the browser's title bar when the menu page is active
'', // The label of this submenu item displayed in the menu
'administrator', // What roles are able to access this submenu item
'ccms-dev', // The ID used to represent this submenu item
'ccms_developer_display' // The callback function used to render the options for this submenu item
);
} else {
add_submenu_page(
'ccms', // The ID of the top-level menu page to which this submenu item belongs
__( 'Developer Options', 'ccms' ), // The value used to populate the browser's title bar when the menu page is active
__( 'Developer Options', 'ccms' ), // The label of this submenu item displayed in the menu
'administrator', // What roles are able to access this submenu item
'ccms-dev', // The ID used to represent this submenu item
'ccms_developer_display' // The callback function used to render the options for this submenu item
);
}
} // end ccms_developer_theme_menu
add_action( 'admin_menu', 'ccms_developer_theme_menu' );
/**
* Renders a simple page to display for the theme menu defined above.
*/
function ccms_developer_display() {
?>
<!-- Create a header in the default WordPress 'wrap' container -->
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h2><?php _e( 'Composite CMS Developer Options', 'ccms' ); ?></h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'ccms_developer_options' );
do_settings_sections( 'ccms_developer_options' );
submit_button();
?>
</form>
</div><!-- /.wrap -->
<?php
} // end ccms_developer_display
/**
* Provides default values for the Developer Options.
*/
function ccms_default_developer_options() {
$defaults = array(
'show_admin_dev' => '0'
);
return apply_filters( 'ccms_default_developer_options', $defaults );
} // end ccms_default_developer_options
/**
* Initializes the theme's display options page by registering the Sections,
* Fields, and Settings.
*
* This function is registered with the 'admin_init' hook.
*/
function ccms_initialize_developer_options() {
// If the theme options don't exist, create them.
if( false == get_option( 'ccms_developer_options' ) ) {
add_option( 'ccms_developer_options', apply_filters( 'ccms_default_developer_options', ccms_default_developer_options() ) );
} // end if
// First, we register a section. This is necessary since all future options must belong to a
add_settings_section(
'ccms_hide_pages_section', // ID used to identify this section and with which to register options
__( 'Developer Options', 'ccms' ), // Title to be displayed on the administration page
'ccms_developer_options_callback', // Callback used to render the description of the section
'ccms_developer_options' // Page on which to add this section of options
);
// Next, we'll introduce the fields for toggling the visibility of content elements.
add_settings_field(
'show_admin_dev', // ID used to identify the field throughout the theme
__( 'Developer Options (This Page)', 'ccms' ), // The label to the left of the option interface element
'ccms_toggle_developer_page_callback', // The name of the function responsible for rendering the option interface
'ccms_developer_options', // The page on which this option will be displayed
'ccms_hide_pages_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( 'If this page is hidden it can still be accessed at <a href="'.get_admin_url().'admin.php?page=ccms-dev">' .get_admin_url().'admin.php?page=ccms-dev</a>', 'ccms' ),
)
);
// Finally, we register the fields with WordPress
register_setting(
'ccms_developer_options',
'ccms_developer_options'
);
} // end ccms_initialize_developer_options
add_action( 'admin_init', 'ccms_initialize_developer_options' );
function ccms_developer_options_callback() {
echo '<p>' . __( 'Select which pages you want to show in the admin sidebar.', 'ccms' ) . '</p>';
} // end ccms_developer_options_callback
function ccms_toggle_developer_page_callback($args) {
// First, we read the options collection
$options = get_option('ccms_developer_options');
// Next, we update the name attribute to access this element's ID in the context of the display options array
// We also access the show_admin_dev element of the options collection in the call to the checked() helper function
$html = '<input type="checkbox" id="show_admin_dev" name="ccms_developer_options[show_admin_dev]" value="1" ' . checked( 1, isset( $options['show_admin_dev'] ) ? $options['show_admin_dev'] : 0, false ) . '/>';
// Here, we'll take the first argument of the array and add it to a label next to the checkbox
$html .= '<label for="show_admin_dev">&nbsp;' . $args[0] . '</label>';
echo $html;
} // end ccms_toggle_developer_page_callback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment