Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

page layout example with custom callback

  • Loading branch information...
commit ce409fa449f3abea0d3f9bd93f88083bae08c262 1 parent 8354e6c
@kovshenin authored
View
BIN  images/layout-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/layout-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN  images/layout-3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
176 post-options-api.php
@@ -19,13 +19,18 @@ public static function description( $args = array() ) {
}
// Checkbox
- public static function checkbox( $description = '', $label = '' ) {
+ public static function checkbox( $args ) {
+
+ $defaults = array(
+ 'label' => '',
+ 'description' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+
return array(
'function' => array( 'Post_Options_Fields', '_checkbox' ),
- 'args' => array(
- 'label' => $label,
- 'description' => $description
- )
+ 'args' => $args
);
}
@@ -37,7 +42,16 @@ public static function _checkbox( $args = array() ) {
}
// Regular text input
- public static function text( $description = '', $sanitize_callback = '' ) {
+ public static function text( $args ) {
+
+ $defaults = array(
+ 'description' => '',
+ 'sanitize_callback' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+ extract( $args, EXTR_SKIP );
+
return array(
'function' => array( 'Post_Options_Fields', '_text' ),
'sanitize_callback' => $sanitize_callback,
@@ -55,7 +69,17 @@ public static function _text ( $args = array() ) {
}
// Textarea input
- public static function textarea( $description = '', $rows = 4, $sanitize_callback = '' ) {
+ public static function textarea( $args ) {
+
+ $defaults = array(
+ 'description' => '',
+ 'rows' => 4,
+ 'sanitize_callback' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+ extract( $args, EXTR_SKIP );
+
return array(
'function' => array( 'Post_Options_Fields', '_textarea' ),
'sanitize_callback' => $sanitize_callback,
@@ -74,7 +98,17 @@ public static function _textarea( $args = array() ) {
}
// Select input
- public static function select( $description = '', $select_data = array(), $sanitize_callback = '' ) {
+ public static function select( $args ) {
+
+ $defaults = array(
+ 'description' => '',
+ 'select_data' => array(),
+ 'sanitize_callback' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+ extract( $args, EXTR_SKIP );
+
return array(
'function' => array( 'Post_Options_Fields', '_select' ),
'sanitize_callback' => $sanitize_callback,
@@ -97,7 +131,17 @@ public static function _select( $args = array() ) {
}
// A radio group input
- public static function radio( $description = '', $radio_data = array(), $sanitize_callback = '' ) {
+ public static function radio( $args ) {
+
+ $defaults = array(
+ 'description' => '',
+ 'radio_data' => array(),
+ 'sanitize_callback' => ''
+ );
+
+ $args = wp_parse_args( $args, $defaults );
+ extract( $args, EXTR_SKIP );
+
return array(
'function' => array( 'Post_Options_Fields', '_radio' ),
'sanitize_callback' => $sanitize_callback,
@@ -347,10 +391,10 @@ function post_options_test() {
'id' => 'a-checkbox',
'title' => 'A checkbox',
'section' => 'showing-off',
- 'callback' => Post_Options_Fields::checkbox(
- 'Any sort of description can go below this checkbox and <a href="#">HTML markup</a> too!',
- 'Check me to win $500'
- )
+ 'callback' => Post_Options_Fields::checkbox( array(
+ 'label' => 'Check me to win $500',
+ 'description' => 'Any sort of description can go below this checkbox and <a href="#">HTML markup</a> too!'
+ ) )
) );
// A text input with a sanitize callback
@@ -358,10 +402,10 @@ function post_options_test() {
'id' => 'an-input',
'title' => 'A text input',
'section' => 'showing-off',
- 'callback' => Post_Options_Fields::text(
- 'The text in this input is saved and sanitized using the <code>sanitize_title</code> sanitize callback, so try and input some caps, numbers, symbols and spaces.',
- 'sanitize_title'
- )
+ 'callback' => Post_Options_Fields::text( array(
+ 'description' => 'The text in this input is saved and sanitized using the <code>sanitize_title</code> sanitize callback, so try and input some caps, numbers, symbols and spaces.',
+ 'sanitize_callback' => 'sanitize_title'
+ ) )
) );
// A textarea
@@ -369,9 +413,9 @@ function post_options_test() {
'id' => 'a-textarea',
'title' => 'A textarea for larger text or perhaps code',
'section' => 'showing-off',
- 'callback' => Post_Options_Fields::textarea(
- 'A textarea might be useful for some custom code or perhaps an addition to the post, like a signature or something. Note how the field title flows nicely in to several lines.'
- )
+ 'callback' => Post_Options_Fields::textarea( array(
+ 'description' => 'A textarea might be useful for some custom code or perhaps an addition to the post, like a signature or something. Note how the field title flows nicely in to several lines.'
+ ) )
) );
// A radio group
@@ -379,14 +423,14 @@ function post_options_test() {
'id' => 'a-radio-group',
'title' => 'A radio group',
'section' => 'showing-off',
- 'callback' => Post_Options_Fields::radio(
- 'Radio groups accept a <code>$radio_data</code> argument where we pass in an array with values and captions for each item in the group.',
- array(
+ 'callback' => Post_Options_Fields::radio( array(
+ 'description' => 'Radio groups accept a <code>$radio_data</code> argument where we pass in an array with values and captions for each item in the group.',
+ 'radio_data' => array(
'option-1' => 'The first option',
'option-2' => 'Another option',
'option-3' => 'One more option'
)
- )
+ ) )
) );
// A drop-down select box
@@ -394,14 +438,14 @@ function post_options_test() {
'id' => 'a-select-input',
'title' => 'A drop-down select box',
'section' => 'showing-off',
- 'callback' => Post_Options_Fields::select(
- 'Select boxes are similar to radio when it comes to data input, so just provide an array of values and captions.',
- array(
+ 'callback' => Post_Options_Fields::select( array(
+ 'description' => 'Select boxes are similar to radio when it comes to data input, so just provide an array of values and captions.',
+ 'select_data' => array(
'option-1' => 'This is the first option',
'option-2' => 'Hurray for the second one',
'option-3' => 'There is room for a third'
)
- )
+ ) )
) );
// The real-world section
@@ -411,10 +455,10 @@ function post_options_test() {
'id' => 'hide-sidebar',
'title' => 'Hide sidebar',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::checkbox(
- 'Check this to hide the right sidebar on this post.',
- 'Hide sidebar on this post'
- )
+ 'callback' => Post_Options_Fields::checkbox( array(
+ 'label' => 'Hide sidebar on this post',
+ 'description' => 'Check this to hide the right sidebar on this post.'
+ ) )
) );
// Feature this post
@@ -422,10 +466,10 @@ function post_options_test() {
'id' => 'featured-post',
'title' => 'Featured post',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::checkbox(
- 'Check this to feature the post in the highlights section on the homepage.',
- 'This is a featured post'
- )
+ 'callback' => Post_Options_Fields::checkbox( array(
+ 'label' => 'This is a featured post',
+ 'description' => 'Check this to feature the post in the highlights section on the homepage.'
+ ) )
) );
// Hide sidebar
@@ -433,10 +477,10 @@ function post_options_test() {
'id' => 'hide-banners',
'title' => 'Hide banners',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::checkbox(
- 'You might want to hide all your banner advertising if you would like the visitor to focus on the content of this post.',
- 'Hide all banner ads on this post'
- )
+ 'callback' => Post_Options_Fields::checkbox( array(
+ 'label' => 'Hide all banner ads on this post',
+ 'description' => 'You might want to hide all your banner advertising if you would like the visitor to focus on the content of this post.'
+ ) )
) );
// Background image
@@ -445,9 +489,9 @@ function post_options_test() {
'id' => 'background-image',
'title' => 'Background image URL',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::text(
- 'Provide the background image URL to override on this post, useful to create outstanding landing pages without the use of templates.'
- )
+ 'callback' => Post_Options_Fields::text( array(
+ 'description' => 'Provide the background image URL to override on this post, useful to create outstanding landing pages without the use of templates.'
+ ) )
) );
// A textarea
@@ -455,9 +499,10 @@ function post_options_test() {
'id' => 'greeting-text',
'title' => 'Greeting text',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::textarea(
- 'Enter some text here to show a popup greeting message box as soon as this post loads.', 3
- )
+ 'callback' => Post_Options_Fields::textarea( array(
+ 'description' => 'Enter some text here to show a popup greeting message box as soon as this post loads.',
+ 'rows' => 3
+ ) )
) );
// A radio group
@@ -465,15 +510,15 @@ function post_options_test() {
'id' => 'navigation-style',
'title' => 'Navigation style',
'section' => 'real-world',
- 'callback' => Post_Options_Fields::radio(
- 'Customize the navigation style for this page.',
- array(
+ 'callback' => Post_Options_Fields::radio( array(
+ 'description' => 'Customize the navigation style for this page.',
+ 'radio_data' => array(
'option-1' => 'Default',
'option-2' => 'Full navigation menu and submenu',
'option-3' => 'Menu only, submenu on hover',
'option-4' => 'Submenu only'
)
- )
+ ) )
) );
// Did you know
@@ -483,6 +528,14 @@ function post_options_test() {
'section' => 'real-world',
'callback' => 'my_callback'
) );
+
+ // Page Layout
+ $post_options->register_post_option( array(
+ 'id' => 'page-layout',
+ 'title' => 'Page layout',
+ 'section' => 'real-world',
+ 'callback' => 'page_layout_callback'
+ ) );
}
// This function illustrates a custom callback
@@ -497,4 +550,27 @@ function my_callback( $args ) {
Go ahead and set it to whatever you like and see how it affects the value:<br />
<input class="large-text" type="text" name="<?php echo $args['name_attr']; ?>" value="<?php echo esc_attr( $args['value'] ); ?>" />
<?php
+}
+
+// Custom callback, page layout
+function page_layout_callback( $args ) {
+ $layouts = array(
+ 'layout-1' => 'Default',
+ 'layout-2' => 'Full-width',
+ 'layout-3' => 'Left sidebar'
+ );
+ ?>
+
+ <?php foreach( $layouts as $layout => $caption ): ?>
+ <div class="mg-color-scheme-item" style="float: left; margin-right: 14px; margin-bottom: 18px;">
+ <label style="float: left; clear: both;">
+ <input <?php echo checked( $layout == $args['value'] ); ?> type="radio" name="<?php echo $args['name_attr']; ?>" value="<?php echo $layout; ?>" style="margin-bottom: 4px;" /><br />
+ <img src="<?php echo plugins_url( 'images/' . $layout . '.png', __FILE__ ); ?>" style="border: solid 1px #ccc;" /><br />
+ <span class="description" style="margin-top: 8px; float: left;"><?php echo $caption; ?></span>
+ </label>
+ </div>
+ <?php endforeach; ?>
+ <br class="clear" />
+ <span class="description">Showing off how one would implement page templates.</span>
+ <?php
}
Please sign in to comment.
Something went wrong with that request. Please try again.