mystickymenu
Last commit date
mystickymenu.css
12 years ago
mystickymenu.js
12 years ago
mystickymenu.php
12 years ago
readme.txt
12 years ago
screenshot-1.png
12 years ago
screenshot-2.png
12 years ago
mystickymenu.php
376 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: myStickymenu |
| 4 | Plugin URI: http://wordpress.transformnews.com/plugins/mystickymenu-simple-sticky-fixed-on-top-menu-implementation-for-twentythirteen-menu-269 |
| 5 | Description: Simple sticky (fixed on top) menu implementation for default Twentythirteen navigation menu. For other themes, after install go to Settings / myStickymenu and change navigation class to .your_navbar_class or #your_navbar_id. |
| 6 | Version: 1.3 |
| 7 | Author: m.r.d.a |
| 8 | License: GPLv2 or later |
| 9 | */ |
| 10 | // Block direct acess to the file |
| 11 | defined('ABSPATH') or die("Cannot access pages directly."); |
| 12 | |
| 13 | // Add plugin admin settings by Otto |
| 14 | class MyStickyMenuPage |
| 15 | { |
| 16 | /** |
| 17 | * Holds the values to be used in the fields callbacks |
| 18 | */ |
| 19 | private $options; |
| 20 | |
| 21 | /** |
| 22 | * Start up |
| 23 | */ |
| 24 | public function __construct() |
| 25 | { |
| 26 | add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); |
| 27 | add_action( 'admin_init', array( $this, 'page_init' ) ); |
| 28 | add_action( 'admin_init', array( $this, 'mysticky_default_options' ) ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Add options page |
| 33 | */ |
| 34 | public function add_plugin_page() |
| 35 | { |
| 36 | // This page will be under "Settings" |
| 37 | |
| 38 | add_options_page( |
| 39 | 'Settings Admin', |
| 40 | 'myStickymenu', |
| 41 | 'manage_options', |
| 42 | 'my-stickymenu-settings', |
| 43 | array( $this, 'create_admin_page' ) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Options page callback |
| 49 | */ |
| 50 | public function create_admin_page() |
| 51 | { |
| 52 | // Set class property |
| 53 | $this->options = get_option( 'mysticky_option_name'); |
| 54 | ?> |
| 55 | <div class="wrap"> |
| 56 | <?php screen_icon(); ?> |
| 57 | <h2>myStickymenu Settings</h2> |
| 58 | <form method="post" action="options.php"> |
| 59 | <?php |
| 60 | // This prints out all hidden setting fields |
| 61 | settings_fields( 'mysticky_option_group' ); |
| 62 | do_settings_sections( 'my-stickymenu-settings' ); |
| 63 | submit_button(); |
| 64 | ?> |
| 65 | </form> |
| 66 | </div> |
| 67 | <?php |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Load Defaults |
| 72 | */ |
| 73 | public function mysticky_default_options() { |
| 74 | |
| 75 | global $options; |
| 76 | |
| 77 | if ( false === get_option('mysticky_option_name') ) { |
| 78 | |
| 79 | $default = array( |
| 80 | |
| 81 | 'mysticky_class_selector' => '.navbar', |
| 82 | 'myfixed_zindex' => '1000000', |
| 83 | 'myfixed_width' => '', |
| 84 | 'myfixed_bgcolor' => '#F39A30', |
| 85 | 'myfixed_opacity' => '95', |
| 86 | 'myfixed_transition_time' => '0.3', |
| 87 | 'myfixed_disable_small_screen' => false, |
| 88 | 'myfixed_disable_admin_bar' => false |
| 89 | |
| 90 | ); |
| 91 | |
| 92 | add_option( 'mysticky_option_name', $default ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Register and add settings |
| 98 | */ |
| 99 | public function page_init() |
| 100 | { |
| 101 | register_setting( |
| 102 | 'mysticky_option_group', // Option group |
| 103 | 'mysticky_option_name', // Option name |
| 104 | array( $this, 'sanitize' ) // Sanitize |
| 105 | ); |
| 106 | |
| 107 | add_settings_field( $id, $title, $callback, $page, $section = 'default', $args = array() ); |
| 108 | |
| 109 | add_settings_section( |
| 110 | 'setting_section_id', // ID |
| 111 | 'myStickymenu Options', // Title |
| 112 | array( $this, 'print_section_info' ), // Callback |
| 113 | 'my-stickymenu-settings' // Page |
| 114 | ); |
| 115 | |
| 116 | add_settings_field( |
| 117 | 'mysticky_class_selector', // ID |
| 118 | 'Sticky Class', // Title |
| 119 | array( $this, 'mysticky_class_selector_callback' ), // Callback |
| 120 | 'my-stickymenu-settings', // Page |
| 121 | 'setting_section_id' // Section |
| 122 | ); |
| 123 | |
| 124 | add_settings_field( |
| 125 | 'myfixed_zindex', |
| 126 | 'Sticky z-index', |
| 127 | array( $this, 'myfixed_zindex_callback' ), |
| 128 | 'my-stickymenu-settings', |
| 129 | 'setting_section_id' |
| 130 | ); |
| 131 | |
| 132 | add_settings_field( |
| 133 | 'myfixed_width', |
| 134 | 'Sticky Width', |
| 135 | array( $this, 'myfixed_width_callback' ), |
| 136 | 'my-stickymenu-settings', |
| 137 | 'setting_section_id' |
| 138 | ); |
| 139 | |
| 140 | add_settings_field( |
| 141 | 'myfixed_bgcolor', |
| 142 | 'Sticky Background Color', |
| 143 | array( $this, 'myfixed_bgcolor_callback' ), |
| 144 | 'my-stickymenu-settings', |
| 145 | 'setting_section_id' |
| 146 | ); |
| 147 | |
| 148 | add_settings_field( |
| 149 | 'myfixed_opacity', |
| 150 | 'Sticky Opacity', |
| 151 | array( $this, 'myfixed_opacity_callback' ), |
| 152 | 'my-stickymenu-settings', |
| 153 | 'setting_section_id' |
| 154 | ); |
| 155 | |
| 156 | add_settings_field( |
| 157 | 'myfixed_transition_time', |
| 158 | 'Sticky Transition Time', |
| 159 | array( $this, 'myfixed_transition_time_callback' ), |
| 160 | 'my-stickymenu-settings', |
| 161 | 'setting_section_id' |
| 162 | ); |
| 163 | add_settings_field( |
| 164 | 'myfixed_disable_small_screen', |
| 165 | 'Enable at Small Screen Sizes', |
| 166 | array( $this, 'myfixed_disable_small_screen_callback' ), |
| 167 | 'my-stickymenu-settings', |
| 168 | 'setting_section_id' |
| 169 | ); |
| 170 | add_settings_field( |
| 171 | 'myfixed_disable_admin_bar', |
| 172 | 'Remove CSS Rules for Static Admin Bar while Sticky', |
| 173 | array( $this, 'myfixed_disable_admin_bar_callback' ), |
| 174 | 'my-stickymenu-settings', |
| 175 | 'setting_section_id' |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sanitize each setting field as needed |
| 181 | * |
| 182 | * @param array $input Contains all settings fields as array keys |
| 183 | */ |
| 184 | public function sanitize( $input ) |
| 185 | { |
| 186 | $new_input = array(); |
| 187 | if( isset( $input['mysticky_class_selector'] ) ) |
| 188 | $new_input['mysticky_class_selector'] = sanitize_text_field( $input['mysticky_class_selector'] ); |
| 189 | |
| 190 | if( isset( $input['myfixed_zindex'] ) ) |
| 191 | $new_input['myfixed_zindex'] = absint( $input['myfixed_zindex'] ); |
| 192 | |
| 193 | if( isset( $input['myfixed_width'] ) ) |
| 194 | $new_input['myfixed_width'] = sanitize_text_field( $input['myfixed_width'] ); |
| 195 | |
| 196 | if( isset( $input['myfixed_bgcolor'] ) ) |
| 197 | $new_input['myfixed_bgcolor'] = sanitize_text_field( $input['myfixed_bgcolor'] ); |
| 198 | |
| 199 | if( isset( $input['myfixed_opacity'] ) ) |
| 200 | $new_input['myfixed_opacity'] = sanitize_text_field( $input['myfixed_opacity'] ); |
| 201 | |
| 202 | if( isset( $input['myfixed_transition_time'] ) ) |
| 203 | $new_input['myfixed_transition_time'] = sanitize_text_field( $input['myfixed_transition_time'] ); |
| 204 | |
| 205 | if( isset( $input['myfixed_disable_small_screen'] ) ) |
| 206 | $new_input['myfixed_disable_small_screen'] = sanitize_text_field( $input['myfixed_disable_small_screen'] ); |
| 207 | |
| 208 | if( isset( $input['myfixed_disable_admin_bar'] ) ) |
| 209 | $new_input['myfixed_disable_admin_bar'] = sanitize_text_field( $input['myfixed_disable_admin_bar'] ); |
| 210 | |
| 211 | return $new_input; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Print the Section text |
| 216 | */ |
| 217 | public function print_section_info() |
| 218 | { |
| 219 | print 'Change myStickymenu options to suite your needs. Default plugin settings work for Twenty Thirteen theme. For other themes you will probably need to change sticky class, please note that some options may be overriden by your theme css. Use .myfixed class in theme or theme child stylesheet for sticky menu if you need extra css settings.'; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the settings option array and print one of its values |
| 224 | */ |
| 225 | public function mysticky_class_selector_callback() |
| 226 | { |
| 227 | printf( |
| 228 | '<input type="text" id="mysticky_class_selector" name="mysticky_option_name[mysticky_class_selector]" value="%s" /> .navbar for Twenty Thirteen template, for other templates inspect your code to find apropriate menu/navigation bar class or id.', |
| 229 | isset( $this->options['mysticky_class_selector'] ) ? esc_attr( $this->options['mysticky_class_selector']) : '' |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | public function myfixed_zindex_callback() |
| 234 | { |
| 235 | printf( |
| 236 | '<input type="text" id="myfixed_zindex" name="mysticky_option_name[myfixed_zindex]" value="%s" /> sticky z-index, default 1000000', |
| 237 | isset( $this->options['myfixed_zindex'] ) ? esc_attr( $this->options['myfixed_zindex']) : '' |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | public function myfixed_width_callback() |
| 242 | { |
| 243 | printf( |
| 244 | '<input type="text" id="myfixed_width" name="mysticky_option_name[myfixed_width]" value="%s" /> sticky width in px or percentage, leave empty for default' , |
| 245 | isset( $this->options['myfixed_width'] ) ? esc_attr( $this->options['myfixed_width']) : '' |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | public function myfixed_bgcolor_callback() |
| 250 | { |
| 251 | printf( |
| 252 | '<input type="text" id="myfixed_bgcolor" name="mysticky_option_name[myfixed_bgcolor]" value="%s" /> default #F39A30' , |
| 253 | isset( $this->options['myfixed_bgcolor'] ) ? esc_attr( $this->options['myfixed_bgcolor']) : '' |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | public function myfixed_opacity_callback() |
| 258 | { |
| 259 | printf( |
| 260 | '<input type="text" id="myfixed_opacity" name="mysticky_option_name[myfixed_opacity]" value="%s" /> numbers 1-100, default 95', |
| 261 | isset( $this->options['myfixed_opacity'] ) ? esc_attr( $this->options['myfixed_opacity']) : '' |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | public function myfixed_transition_time_callback() |
| 266 | { |
| 267 | printf( |
| 268 | '<input type="text" id="myfixed_transition_time" name="mysticky_option_name[myfixed_transition_time]" value="%s" /> in seconds, default 0.3', |
| 269 | isset( $this->options['myfixed_transition_time'] ) ? esc_attr( $this->options['myfixed_transition_time']) : '' |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | public function myfixed_disable_small_screen_callback() |
| 274 | { |
| 275 | printf( |
| 276 | '<input id="%1$s" name="mysticky_option_name[myfixed_disable_small_screen]" type="checkbox" %2$s /> Enable mysticky menu on small resolutions, less than 359px, default unchecked.', |
| 277 | 'myfixed_disable_small_screen', |
| 278 | checked( isset( $this->options['myfixed_disable_small_screen'] ), true, false ) |
| 279 | ); |
| 280 | } |
| 281 | public function myfixed_disable_admin_bar_callback() |
| 282 | { |
| 283 | printf( |
| 284 | '<input id="%1$s" name="mysticky_option_name[myfixed_disable_admin_bar]" type="checkbox" %2$s /> Select this only if your theme does not show fixed admin bar on frontpage, default unchecked.', |
| 285 | 'myfixed_disable_admin_bar', |
| 286 | checked( isset( $this->options['myfixed_disable_admin_bar'] ), true, false ) |
| 287 | ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if( is_admin() ) |
| 292 | $my_settings_page = new MyStickyMenuPage(); |
| 293 | |
| 294 | // end plugin admin settings |
| 295 | |
| 296 | |
| 297 | // Remove default option for more link that jumps at the midle of page and its covered by menu |
| 298 | |
| 299 | function mysticky_remove_more_jump_link($link) { |
| 300 | $offset = strpos($link, '#more-'); |
| 301 | if ($offset) { |
| 302 | $end = strpos($link, '"',$offset); |
| 303 | } |
| 304 | if ($end) { |
| 305 | $link = substr_replace($link, '', $offset, $end-$offset); |
| 306 | } |
| 307 | return $link; |
| 308 | } |
| 309 | add_filter('the_content_more_link', 'mysticky_remove_more_jump_link'); |
| 310 | |
| 311 | // Create style from options |
| 312 | |
| 313 | function mysticky_build_stylesheet_content() { |
| 314 | $mysticky_options = get_option( 'mysticky_option_name' ); |
| 315 | echo |
| 316 | '<style type="text/css"> |
| 317 | '; |
| 318 | if ($mysticky_options ['myfixed_disable_admin_bar'] == false ){ |
| 319 | echo |
| 320 | '#wpadminbar { |
| 321 | position: absolute !important; |
| 322 | top: 0px !important; |
| 323 | } |
| 324 | '; |
| 325 | } |
| 326 | echo |
| 327 | '.myfixed { |
| 328 | position: fixed; |
| 329 | top: 0px!important; |
| 330 | margin-top: 0px!important; |
| 331 | z-index: '. $mysticky_options ['myfixed_zindex'] .'; |
| 332 | -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=' . $mysticky_options ['myfixed_opacity'] . ')"; |
| 333 | filter: alpha(opacity=' . $mysticky_options ['myfixed_opacity'] . '); |
| 334 | opacity:.' . $mysticky_options ['myfixed_opacity'] . '; |
| 335 | '; |
| 336 | if ($mysticky_options ['myfixed_width'] >= 1 ){ |
| 337 | echo |
| 338 | 'width:' . $mysticky_options ['myfixed_width'] . '!important; |
| 339 | '; |
| 340 | } |
| 341 | echo |
| 342 | 'background-color: ' . $mysticky_options ['myfixed_bgcolor'] . '!important; |
| 343 | -webkit-transition: ' . $mysticky_options ['myfixed_transition_time'] . 's; |
| 344 | -moz-transition: ' . $mysticky_options ['myfixed_transition_time'] . 's; |
| 345 | -o-transition: ' . $mysticky_options ['myfixed_transition_time'] . 's; |
| 346 | transition: ' . $mysticky_options ['myfixed_transition_time'] . 's; |
| 347 | } |
| 348 | '; |
| 349 | if ($mysticky_options ['myfixed_disable_small_screen'] == false ){ |
| 350 | echo |
| 351 | '@media (max-width: 359px) {.myfixed {position: static!important;}} |
| 352 | '; |
| 353 | } |
| 354 | echo '</style> |
| 355 | '; |
| 356 | } |
| 357 | add_action('wp_head', 'mysticky_build_stylesheet_content'); |
| 358 | |
| 359 | |
| 360 | function mystickymenu_script() { |
| 361 | |
| 362 | $mysticky_options = get_option( 'mysticky_option_name' ); |
| 363 | |
| 364 | // Register scripts |
| 365 | wp_register_script('mystickymenu', WP_PLUGIN_URL. '/mystickymenu/mystickymenu.js', false,'1.0.0', true); |
| 366 | wp_enqueue_script( 'mystickymenu' ); |
| 367 | |
| 368 | // Localize mystickymenu.js script with myStickymenu options |
| 369 | $mysticky_translation_array = array( 'mysticky_string' => $mysticky_options['mysticky_class_selector'] ); |
| 370 | |
| 371 | wp_localize_script( 'mystickymenu', 'mysticky_name', $mysticky_translation_array ); |
| 372 | |
| 373 | } |
| 374 | add_action( 'wp_enqueue_scripts', 'mystickymenu_script' ); |
| 375 | |
| 376 | ?> |