Created
October 24, 2012 20:11
-
-
Save krogsgard/3948540 to your computer and use it in GitHub Desktop.
Example settings page for featuring a specific post somewhere for client sites
This file contains hidden or 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 | |
/* | |
* Sample settings page | |
* forked from Tom McFarlin's more extensive example https://github.com/tommcfarlin/WordPress-Settings-Sandbox/ | |
* | |
* @author Brian Krogsgard | |
*/ | |
/** | |
* This function introduces the theme options into a top-level | |
* 'Infomedia' menu. | |
*/ | |
function infomedia_settings_theme_menu() { | |
add_menu_page( | |
'Infomedia Settings', // The value used to populate the browser's title bar when the menu page is active | |
'Infomedia Settings', // The text of the menu in the administrator's sidebar | |
'manage_options', // What roles are able to access the menu | |
'infomedia_menu', // The ID used to bind submenu items to this menu | |
'infomedia_display' // The callback function used to render this menu | |
); | |
} // end infomedia_settings_theme_menu | |
add_action( 'admin_menu', 'infomedia_settings_theme_menu' ); | |
/** | |
* Renders a simple page to display for the theme menu defined above. | |
*/ | |
function infomedia_display() { | |
?> | |
<!-- Create a header in the default WordPress 'wrap' container --> | |
<div class="wrap"> | |
<div id="icon-themes" class="icon32"></div> | |
<h2>Infomedia Options</h2> | |
<?php settings_errors(); ?> | |
<form method="post" action="options.php"> | |
<?php | |
settings_fields( 'infomedia_inputs' ); | |
do_settings_sections( 'infomedia_inputs' ); | |
submit_button(); | |
?> | |
</form> | |
</div><!-- /.wrap --> | |
<?php | |
} // end infomedia_display | |
/* ------------------------------------------------------------------------ * | |
* Setting Registration | |
* ------------------------------------------------------------------------ */ | |
/** | |
* Initializes the theme's input example by registering the Sections, | |
* Fields, and Settings. This particular group of options is used to demonstration | |
* validation and sanitization. | |
* | |
* This function is registered with the 'admin_init' hook. | |
*/ | |
function infomedia_initialize_inputs() { | |
if( false == get_option( 'infomedia_inputs' ) ) { | |
add_option( 'infomedia_inputs' ); | |
} // end if | |
add_settings_section( | |
'inputs_section', | |
'Infomedia custom options', | |
'infomedia_inputs_callback', | |
'infomedia_inputs' | |
); | |
add_settings_field( | |
'Feature Physician', | |
'Feature Physician', | |
'infomedia_feature_post_callback', | |
'infomedia_inputs', | |
'inputs_section' | |
); | |
register_setting( | |
'infomedia_inputs', | |
'infomedia_inputs', | |
'infomedia_validate_inputs' | |
); | |
} // end infomedia_initialize_inputs | |
add_action( 'admin_init', 'infomedia_initialize_inputs' ); | |
/* ------------------------------------------------------------------------ * | |
* Section Callbacks | |
* ------------------------------------------------------------------------ */ | |
/** | |
* This function provides a simple description for the Input Examples page. | |
* | |
* It's called from the 'infomedia_intialize_inputs_options' function by being passed as a parameter | |
* in the add_settings_section function. | |
*/ | |
function infomedia_inputs_callback() { | |
echo '<p>Choose custom options.</p>'; | |
} // end infomedia_general_options_callback | |
/* ------------------------------------------------------------------------ * | |
* Field Callbacks | |
* ------------------------------------------------------------------------ */ | |
function infomedia_feature_post_callback() { | |
$options = get_option( 'infomedia_inputs' ); | |
$args = array ( | |
'post_type' => 'post', | |
'no_found_rows' => true, | |
'posts_per_page' => -1, | |
'update_post_term_cache' => false, | |
'update_post_meta_cache' => false, | |
'orderby' => 'menu_order', | |
'order' => 'ASC' | |
); | |
$krogsquery = new WP_Query ( $args ); | |
$html = '<select id="feature_post" name="infomedia_inputs[feature_post]">'; | |
while( $krogsquery->have_posts() ) : $krogsquery->the_post(); | |
$html .= '<option value="' . get_the_ID() . '"' . selected( $options['feature_post'], get_the_ID(), false) . '>' . get_the_title() . '</option>'; | |
endwhile; | |
wp_reset_postdata(); // reset query | |
$html .= '</select>'; | |
echo $html; | |
} | |
/* ------------------------------------------------------------------------ * | |
* Setting Callbacks | |
* ------------------------------------------------------------------------ */ | |
function infomedia_validate_inputs( $input ) { | |
// Create our array for storing the validated options | |
$output = array(); | |
// Loop through each of the incoming options | |
foreach( $input as $key => $value ) { | |
// Check to see if the current option has a value. If so, process it. | |
if( isset( $input[$key] ) ) { | |
// Strip all HTML and PHP tags and properly handle quoted strings | |
$output[$key] = strip_tags( stripslashes( $input[ $key ] ) ); | |
} // end if | |
} // end foreach | |
// Return the array processing any additional functions filtered by this action | |
return apply_filters( 'infomedia_validate_inputs', $output, $input ); | |
} // end infomedia_validate_inputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment