Advertisement
firoze

Add option page in plugin

Oct 20th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.42 KB | None | 0 0
  1. function add_ppmscrollbar_options_framwrork()  
  2. {  
  3.     add_options_page('Sticky aleart settings', 'Sticky aleart settings', 'manage_options', 'stickyaleart-settings','ppmscrollbar_options_framwrork');  
  4. }  
  5. add_action('admin_menu', 'add_ppmscrollbar_options_framwrork');
  6.  
  7.  
  8. add_action( 'admin_enqueue_scripts', 'wptuts_add_color_picker' );
  9. function wptuts_add_color_picker( $hook ) {
  10.  
  11.     if( is_admin() ) {
  12.      
  13.         // Add the color picker css file      
  14.         wp_enqueue_style( 'wp-color-picker' );
  15.          
  16.         // Include our custom jQuery file with WordPress Color Picker dependency
  17.         wp_enqueue_script( 'custom-script-handle', plugins_url( '/inc/color-pickr.js', __FILE__ ), array( 'wp-color-picker' ), false, true );
  18.     }
  19. }
  20.  
  21.  
  22. if ( is_admin() ) : // Load only if we are viewing an admin page
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. function ppmscrollbar_register_settings() {
  30.     // Register settings and call sanitation functions
  31.     register_setting( 'ppmscrollbar_p_options', 'ppmscrollbar_options', 'ppmscrollbar_validate_options' );
  32. }
  33.  
  34. add_action( 'admin_init', 'ppmscrollbar_register_settings' );
  35.  
  36. // Default options values
  37. $ppmscrollbar_options = array(
  38.     'sticky_bar_bg' => '#cc0000',
  39.     'sticky_bar_text_color' => '#fff'
  40. );
  41.  
  42. // Function to generate options page
  43. function ppmscrollbar_options_framwrork() {
  44.     global $ppmscrollbar_options, $auto_hide_mode, $where_visible_scrollbar;
  45.  
  46.     if ( ! isset( $_REQUEST['updated'] ) )
  47.         $_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>
  48.  
  49.     <div class="wrap">
  50.  
  51.    
  52.     <h2>Custom Scrollbar Options</h2>
  53.  
  54.     <?php if ( false !== $_REQUEST['updated'] ) : ?>
  55.     <div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
  56.     <?php endif; // If the form has just been submitted, this shows the notification ?>
  57.  
  58.     <form method="post" action="options.php">
  59.  
  60.     <?php $settings = get_option( 'ppmscrollbar_options', $ppmscrollbar_options ); ?>
  61.    
  62.     <?php settings_fields( 'ppmscrollbar_p_options' );
  63.     /* This function outputs some hidden fields required by the form,
  64.     including a nonce, a unique number used to ensure the form has been submitted from the admin page
  65.     and not somewhere else, very important for security */ ?>
  66.  
  67.    
  68.     <table class="form-table"><!-- Grab a hot cup of coffee, yes we're using tables! -->
  69.    
  70.    
  71.  
  72.  
  73.         <tr valign="top">
  74.             <th scope="row"><label for="sticky_bar_bg">Sticky Bar Background</label></th>
  75.             <td>
  76.                 <input id="sticky_bar_bg" type="text" name="ppmscrollbar_options[sticky_bar_bg]" value="<?php echo stripslashes($settings['sticky_bar_bg']); ?>" class="color-field" /><p class="description">Select scrollbar color here. You can also add html HEX color code.</p>
  77.             </td>
  78.         </tr>  
  79.  
  80.         <tr valign="top">
  81.             <th scope="row"><label for="sticky_bar_text_color">Sticky Bar text color</label></th>
  82.             <td>
  83.                 <input id="sticky_bar_text_color" type="text" name="ppmscrollbar_options[sticky_bar_text_color]" value="<?php echo stripslashes($settings['sticky_bar_text_color']); ?>" class="color-field" /><p class="description">Select scrollbar color here. You can also add html HEX color code.</p>
  84.             </td>
  85.         </tr>  
  86.  
  87.            
  88.     </table>
  89.  
  90.     <p class="submit"><input type="submit" class="button-primary" value="Save Options" /></p>
  91.  
  92.     </form>
  93.  
  94.     </div>
  95.  
  96.     <?php
  97. }
  98.  
  99.  
  100. function ppmscrollbar_validate_options( $input ) {
  101.     global $ppmscrollbar_options, $auto_hide_mode;
  102.  
  103.     $settings = get_option( 'ppmscrollbar_options', $ppmscrollbar_options );
  104.    
  105.     // We strip all tags from the text field, to avoid vulnerablilties like XSS
  106.  
  107.     $input['sticky_bar_bg'] = wp_filter_post_kses( $input['sticky_bar_bg'] );
  108.     $input['sticky_bar_text_color'] = wp_filter_post_kses( $input['sticky_bar_text_color'] );
  109.  
  110.        
  111.        
  112.    
  113.     return $input;
  114. }
  115.  
  116.  
  117. endif;  // EndIf is_admin()
  118.  
  119.  
  120.  
  121. function get_data_form_plugin() {
  122.  
  123. ?>
  124.  
  125. <?php global $ppmscrollbar_options; $ppmscrollbar_settings = get_option( 'ppmscrollbar_options', $ppmscrollbar_options ); ?>
  126.  
  127. <style type="text/css">
  128.     div.alert-box {background-color:<?php echo $ppmscrollbar_settings['sticky_bar_bg']; ?> !important}
  129.     div.alert-box a {color: <?php echo $ppmscrollbar_settings['sticky_bar_text_color']; ?> !important}
  130. </style>
  131.  
  132. <?php
  133.  
  134. }
  135.  
  136. add_action('wp_head', 'get_data_form_plugin');
  137.  
  138. //Color picker js file code
  139.  
  140. (function( $ ) {
  141.  
  142.     // Add Color Picker to all inputs that have 'color-field' class
  143.     $(function() {
  144.         $('.color-field').wpColorPicker();
  145.     });
  146.      
  147. })( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement