-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
278 lines (235 loc) · 8.05 KB
/
functions.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
function remove_wp_css() {
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
}
add_action('init', 'remove_wp_css');
function stoff_enqueue() {
// Update stylesheet ver number here to update on live site
// Currently cheats by setting the version number to the datetime of css file creation
$cssPath = get_template_directory_uri() . '/assets/css/stoff.css';
$scriptPath = get_template_directory_uri() . '/assets/js/script.js';
wp_enqueue_style(
'style',
$cssPath,
array(),
filemtime( get_template_directory() . '/assets/css/stoff.css' )
);
wp_enqueue_script(
'script',
$scriptPath,
array(),
filemtime( get_template_directory() . '/assets/js/script.js' ),
true
);
}
add_action('wp_enqueue_scripts', 'stoff_enqueue');
function stoff_setup(){
$features = [
['title-tag'],
['post-thumbnails'],
['featured-content'],
['automatic-feed-links'],
['menus'],
['html5',
[
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
]
],
['post-formats',
[
'standard',
'aside',
'chat',
'gallery',
'image',
]
],
];
foreach ($features as &$args) {
call_user_func_array('add_theme_support', $args);
}
// Dynamic menu locations
register_nav_menus( array(
'header-menu' => "Under stor header, anbefaler ikke mer en 5 punkt",
'big-categories' => "Sidemeny - Stor del med dropdowns",
'small-categories' => "Sidemeny - Liten del uten dropdowns",
));
}
add_action('after_setup_theme', 'stoff_setup');
function stoff_customize_register( $wp_customize ) {
// https://developer.wordpress.org/themes/customize-api/customizer-objects/#controls
// Add Section
$wp_customize->add_section('header', array(
'title' => __('Utseende', 'name-theme'),
'priority' => 70,
));
/**
* Add Settings
* https://developer.wordpress.org/reference/classes/wp_customize_setting/__construct/#parameters
*/
$settings = [
[ 'dark_header', ['default' => true] ],
[ 'header_bg', ['default' => false] ],
[ 'start_dark', ['default' => false] ],
[ 'ui_toggle', ['default' => false] ],
[ 'big_header', ['default' => true] ],
[ 'footer_txt', ['default' => 'STOFF arbeider etter Vær Varsom-plakatens regler for god presseskikk, og får støtte fra Velferdstinget Vest.'] ],
[ 'ad_setting_two', [] ],
];
foreach ($settings as &$args) {
// For all settings, call $wp_customize->add_setting('name', array(args))
call_user_func_array(array($wp_customize,'add_setting'), $args);
}
/**
* Add Controls
* https://developer.wordpress.org/reference/classes/WP_Customize_Control/__construct/
*/
$controls = [
[ 'dark_header', [
'label' => __( 'Mørk header' ),
'type' => 'checkbox',
'section' => 'header',
'settings' => 'dark_header',
]],
[ 'header_bg', [
'label' => __( 'Header-bakgrunn' ),
'type' => 'checkbox',
'section' => 'header',
'settings' => 'header_bg',
]],
[ 'start_dark', [
'label' => __( 'Start i mørk modus' ),
'type' => 'checkbox',
'section' => 'header',
'settings' => 'start_dark',
]],
/* //UI toggle feature is unifnished, don't allow activation
[ 'ui_toggle', [
'label' => __( 'Tillat å endre mørk/lys modus' ),
'type' => 'checkbox',
'section' => 'header',
'settings' => 'ui_toggle',
]], */
[ 'big_header', [
'label' => __( 'Bruk stor header' ),
'type' => 'checkbox',
'section' => 'header',
'settings' => 'big_header',
]],
[ 'footer_txt', [
'label' => __( 'Tekst i meny-footer' ),
'type' => 'textarea',
'section' => 'header',
'settings' => 'footer_txt',
]],
];
foreach ($controls as &$args) {
// For all controls, call $wp_customize->add_control('name', array(args))
call_user_func_array(array($wp_customize,'add_control'), $args);
}
}
add_action( 'customize_register', 'stoff_customize_register' );
// Don't put sticky posts before others
function ea_remove_sticky_from_main_loop( $query ) {
$query->set( 'ignore_sticky_posts', true );
}
add_action( 'pre_get_posts', 'ea_remove_sticky_from_main_loop' );
// Allow importing media/posts from same domain, etc from test to www
add_filter( 'http_request_host_is_external', '__return_true' );
// Custom excerpt length
add_filter( 'excerpt_length', function( $length ) { return 21; } );
// Custom excerpt more style
add_filter('excerpt_more', function( $more ) { return '...'; });
/*
// Remove width & height attributes from wp images
function remove_img_attr ($html) {
return preg_replace('/(width|height)="\d+"\s/', "", $html);
}
add_filter( 'post_thumbnail_html', 'remove_img_attr' );
*/
// Makes supre ads from advanced ads plugin are blocked by adblockers
function ad_wrap( $output ){
return '
<div class="gofollow">
<div class="annonse-tag">Annonse</div>'
. $output .
'</div>';
}
add_filter( 'advanced-ads-output-final', 'ad_wrap' );
// Walker for removing li tags from menus (jesus wordpress)
class Custom_Walker extends Walker_Nav_Menu {
// https://developer.wordpress.org/reference/classes/walker_nav_menu/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
global $currentID;
$currentID = $item->ID;
$classes = empty($item->classes) ? array () : (array) $item->classes;
$class_names = join(' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
// Could add some hacky customs support for color classes here
global $clrClass;
$clrClass = '';
if ( isset($args->colors) ) {
if ($args->colors && $depth == 0) {
// Make clrClass global for submenu to reach it
$clrClass = filter_var($item->title, FILTER_SANITIZE_STRING);
}
}
!empty ( $class_names ) and $class_names = ' class=" '. $clrClass .' '. esc_attr( $class_names ) . '"';
$output .= "<div id='menu-item-$item->ID' $class_names>";
$attributes = '';
!empty( $item->attr_title ) and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';
!empty( $item->target ) and $attributes .= ' target="' . esc_attr( $item->target ) .'"';
!empty( $item->xfn ) and $attributes .= ' rel="' . esc_attr( $item->xfn ) .'"';
!empty( $item->url ) and $attributes .= ' href="' . esc_attr( $item->url ) .'"';
$title = apply_filters( 'the_title', $item->title, $item->ID );
$expandBtn = '';
if ( isset($args->expands) ) {
if ($args->expands && $depth == 0) {
$expandBtn = "<button id='expand-$item->ID' class='dropdown-btn'>+</button>";
}
}
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a>'
. $expandBtn
. '</div>'
. $args->link_after
. $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function start_lvl( &$output, $depth = 0, $args = null ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
// Default class.
global $clrClass;
$classes = array( 'sub-menu', 'hidden', $clrClass );
$clrClass = '';
/**
* Filters the CSS class(es) applied to a menu list element.
*/
$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$parentID = '';
global $currentID;
if ( isset($currentID) ) {
$parentID = "id='submenu-". $currentID ."'";
}
$output .= "{$n}{$indent}<ul $parentID $class_names>{$n}";
}
}
?>