Created
August 5, 2015 15:17
-
-
Save avantegarde/46a3cea480602b77d04e to your computer and use it in GitHub Desktop.
WordPress dashboard widget with options panel for quick access to theme options and only viewable by admins.
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 | |
/* ---------------------------------------------------------- | |
* ---------------------------------------------------------- | |
Featured Grid Dashboard Widget | |
------------------------------------------------------------- * | |
------------------------------------------------------------- */ | |
/** | |
* Add a widget to the dashboard. | |
*/ | |
function featured_grid_dashboard_widget() { | |
wp_add_dashboard_widget( | |
'featured_grid_dashboard_widget', // Widget slug. | |
'Featured Grid Dashboard Widget', // Title. | |
'featured_grid_dashboard_widget_function' // Display function. | |
); | |
} | |
add_action( 'wp_dashboard_setup', 'featured_grid_dashboard_widget' ); | |
/** | |
* Create the function to output the contents of our Dashboard Widget. | |
*/ | |
/* ---------------------------------------------------------- | |
Declare vars | |
------------------------------------------------------------- */ | |
$dwName = "Dashboard Widget Name"; | |
$dwShort = "shortname"; | |
/* --------------------------------------------------------- | |
Declare Dashboard Widget options | |
----------------------------------------------------------- */ | |
$dw_options = array ( | |
array( | |
"name" => $dwName." Options", | |
"type" => "title" | |
), | |
/* --------------------------------------------------------- | |
General section | |
----------------------------------------------------------- */ | |
array( | |
"name" => "Featured Posts", | |
"type" => "section" | |
), | |
array( "type" => "open"), | |
array( | |
"name" => "Post #1", | |
"desc" => "Enter the post ID", | |
"id" => $dwShort."_post1", | |
"type" => "number", | |
"std" => "" | |
), | |
array( | |
"name" => "Post #2", | |
"desc" => "Enter the post ID", | |
"id" => $dwShort."_post2", | |
"type" => "number", | |
"std" => "" | |
), | |
array( | |
"name" => "Post #3", | |
"desc" => "Enter the post ID", | |
"id" => $dwShort."_post3", | |
"type" => "number", | |
"std" => "" | |
), | |
array( | |
"name" => "Post #4", | |
"desc" => "Enter the post ID", | |
"id" => $dwShort."_post4", | |
"type" => "number", | |
"std" => "" | |
), | |
array( | |
"name" => "Post #5", | |
"desc" => "Enter the post ID", | |
"id" => $dwShort."_post5", | |
"type" => "number", | |
"std" => "" | |
), | |
array( "type" => "close") | |
); | |
/*--------------------------------------------------- | |
Dashboard Widget Panel Output | |
----------------------------------------------------*/ | |
function featured_grid_dashboard_widget_function() { | |
global $dwName,$dw_options; | |
$i=0; | |
$message=''; | |
if ( 'save' == $_REQUEST['action'] ) { | |
foreach ($dw_options as $value) { | |
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); | |
} | |
foreach ($dw_options as $value) { | |
if( isset( $_REQUEST[ $value['id'] ] ) ) { | |
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); | |
} else { | |
delete_option( $value['id'] ); | |
} | |
} | |
$message='saved'; | |
} | |
else if( 'reset' == $_REQUEST['action'] ) { | |
foreach ($dw_options as $value) { | |
delete_option( $value['id'] ); | |
} | |
$message='reset'; | |
} | |
?> | |
<div class="featured_grid_options options_wrap"> | |
<?php | |
if ( $message=='saved' ) echo '<div class="updated settings-error" id="setting-error-settings_updated"> | |
<p>'.$dwName.' settings saved.</strong></p></div>'; | |
if ( $message=='reset' ) echo '<div class="updated settings-error" id="setting-error-settings_updated"> | |
<p>'.$dwName.' settings reset.</strong></p></div>'; | |
?> | |
<div class="content_options"> | |
<form method="post"> | |
<?php foreach ($dw_options as $value) { | |
switch ( $value['type'] ) { | |
case "open": ?> | |
<?php break; | |
case "close": ?> | |
</div> | |
</div><br /> | |
<?php break; | |
case "title": ?> | |
<div class="message"> | |
<p>To update the <?php echo $dwName;?> simply type in the post ID number into each field.</p> | |
</div> | |
<?php break; | |
case 'text': ?> | |
<div class="option_input option_text"> | |
<label for="<?php echo $value['id']; ?>"> | |
<?php echo $value['name']; ?></label> | |
<input id="" type="<?php echo $value['type']; ?>" name="<?php echo $value['id']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>" /> | |
<small><?php echo $value['desc']; ?></small> | |
<div class="clearfix"></div> | |
</div> | |
<?php break; | |
case 'number': ?> | |
<div class="option_input option_number"> | |
<label for="<?php echo $value['id']; ?>"> | |
<?php echo $value['name']; ?></label> | |
<input id="" type="<?php echo $value['type']; ?>" name="<?php echo $value['id']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>" /> | |
<small><?php echo $value['desc']; ?></small> | |
<div class="clearfix"></div> | |
</div> | |
<?php break; | |
case 'textarea': ?> | |
<div class="option_input option_textarea"> | |
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label> | |
<textarea name="<?php echo $value['id']; ?>" rows="" cols=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?></textarea> | |
<small><?php echo $value['desc']; ?></small> | |
<div class="clearfix"></div> | |
</div> | |
<?php break; | |
case 'select': ?> | |
<div class="option_input option_select"> | |
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label> | |
<select name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"> | |
<?php foreach ($value['options'] as $option) { ?> | |
<option <?php if (get_settings( $value['id'] ) == $option) { echo 'selected="selected"'; } ?>><?php echo $option; ?></option> | |
<?php } ?> | |
</select> | |
<small><?php echo $value['desc']; ?></small> | |
<div class="clearfix"></div> | |
</div> | |
<?php break; | |
case "checkbox": ?> | |
<div class="option_input option_checkbox"> | |
<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label> | |
<?php if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = "";} ?> | |
<input id="<?php echo $value['id']; ?>" type="checkbox" name="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> /> | |
<small><?php echo $value['desc']; ?></small> | |
<div class="clearfix"></div> | |
</div> | |
<?php break; | |
case "section": | |
$i++; ?> | |
<div class="input_section"> | |
<div class="input_title"> | |
<h3><?php echo $value['name']; ?></h3> | |
<div class="clearfix"></div> | |
</div> | |
<div class="all_options"> | |
<?php break; | |
} | |
}?> | |
<span class="submit"><input name="save<?php echo $i; ?>" type="submit" class="button-primary" value="Save changes" /></span> | |
<input type="hidden" name="action" value="save" /> | |
</form> | |
<form method="post"> | |
<p class="submit"> | |
<input name="reset" type="submit" value="Reset" /> | |
<input type="hidden" name="action" value="reset" /> | |
</p> | |
</form> | |
</div> | |
<div class="footer-credit"> | |
<p>Author <a href="http://kaelsteinert.com/" target="_blank">Kael Steinert</a></p> | |
</div> | |
</div> | |
<?php | |
} | |
// Hide Dashboard Widget for everyone except Admins | |
function remove_dashboard_widgets() { | |
global $wp_meta_boxes; | |
unset($wp_meta_boxes['dashboard']['normal']['core']['featured_grid_dashboard_widget']); | |
} | |
if (!current_user_can('manage_options')) { | |
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment