-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtk-fixed-top-nav.php
executable file
·86 lines (77 loc) · 2.34 KB
/
htk-fixed-top-nav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/*
Plugin Name: Hacktoolkit Fixed Top Nav
Plugin URI: https://www.hacktoolkit.com/htk-wordpress-plugins
Description: Sets the top nav fixed position style
Author: Hacktoolkit
Version: 0.1
Author URI: https://www.hacktoolkit.com
*/
class HtkFixedTopNav {
function HtkFixedTopNav() {
// https://codex.wordpress.org/Function_Reference/add_shortcode
add_shortcode('htk_fixed_top_nav', array($this, 'insert_fixed_top_nav_css'));
}
function insert_fixed_top_nav_css($atts) {
// https://codex.wordpress.org/Function_Reference/show_admin_bar
$WP_ADMIN_BAR_HEIGHT = 32;
$DEFAULT_TOP_NAV_HEIGHT = 31;
$topnav_height = isset($atts['height'])? intval($atts['height']) : $DEFAULT_TOP_NAV_HEIGHT;
$top = is_admin_bar_showing() ? $WP_ADMIN_BAR_HEIGHT : 0;
// TODO: how is #wrapper accomodating for wp_admin_bar ?
//$wrapper_margin_top = $top + $topnav_height;
$wrapper_margin_top = $topnav_height;
$style = <<<EOF
<!-- HTK Fixed Top Nav -->
<style type="text/css">
#top {
position: fixed;
top: ${top}px;
height: 31px;
width: 100%;
z-index: 1000000;
background-color: rgba(255, 255, 255, 0.9);
}
@media only screen and (min-width: 768px) {
#wrapper {
/* offset for fixed top nav */
margin-top: ${wrapper_margin_top}px;
}
}
</style>
<!-- / HTK Fixed Top Nav -->
EOF;
echo $style;
}
function display_menu($atts) {
$args = array(
'echo' => false,
'depth' => 0,
);
$menu_html = wp_nav_menu($args);
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="UTF-8">' . $menu_html);
$dom = new DOMXpath($doc);
$elements = $dom->query("//li[contains(@class, 'current-menu-item')]");
$html = '';
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
$is_first = true;
foreach ($nodes as $node) {
if ($is_first) {
// skip
$is_first = false;
continue;
} else {
$html .= $doc->saveHTML($node);
}
}
break;
}
}
return $html;
}
}
$obj = new HtkFixedTopNav();
?>