-
Notifications
You must be signed in to change notification settings - Fork 67
/
functions.php
394 lines (308 loc) · 12.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
//Add thumbnail, automatic feed links and title tag support
add_theme_support( 'post-thumbnails' );
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'title-tag' );
//Add content width (desktop default)
if ( ! isset( $content_width ) ) {
$content_width = 768;
}
//Add menu support and register main menu
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'main_menu' => 'Main Menu'
)
);
}
// filter the Gravity Forms button type
add_filter('gform_submit_button', 'form_submit_button', 10, 2);
function form_submit_button($button, $form){
return "<button class='button btn' id='gform_submit_button_{$form["id"]}'><span>{$form['button']['text']}</span></button>";
}
// Register sidebar
add_action('widgets_init', 'theme_register_sidebar');
function theme_register_sidebar() {
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'id' => 'sidebar-1',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
));
}
}
// Bootstrap_Walker_Nav_Menu setup
add_action( 'after_setup_theme', 'bootstrap_setup' );
if ( ! function_exists( 'bootstrap_setup' ) ):
function bootstrap_setup(){
add_action( 'init', 'register_menu' );
function register_menu(){
register_nav_menu( 'top-bar', 'Bootstrap Top Menu' );
}
class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat( "\t", $depth );
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
}
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if (!is_object($args)) {
return; // menu has not been configured
}
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$li_attributes = '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = ($args->has_children) ? 'dropdown' : '';
$classes[] = ($item->current || $item->current_item_ancestor) ? 'active' : '';
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names . $li_attributes . '>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$attributes .= ($args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= ($args->has_children) ? ' <b class="caret"></b></a>' : '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
if ( !$element )
return;
$id_field = $this->db_fields['id'];
//display this element
if ( is_array( $args[0] ) )
$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
else if ( is_object( $args[0] ) )
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
$id = $element->$id_field;
// descend only when the depth is right and there are childrens for this element
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
foreach( $children_elements[ $id ] as $child ){
if ( !isset($newlevel) ) {
$newlevel = true;
//start the child delimiter
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
}
unset( $children_elements[ $id ] );
}
if ( isset($newlevel) && $newlevel ){
//end the child delimiter
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
}
}
}
endif;
// START THEME OPTIONS
// custom theme options for user in admin area - Appearance > Theme Options
function pu_theme_menu()
{
add_theme_page( 'Theme Option', 'Theme Options', 'manage_options', 'pu_theme_options.php', 'pu_theme_page');
}
add_action('admin_menu', 'pu_theme_menu');
function pu_theme_page()
{
?>
<div class="section panel">
<h1>Custom Theme Options</h1>
<form method="post" enctype="multipart/form-data" action="options.php">
<hr>
<?php
settings_fields('pu_theme_options');
do_settings_sections('pu_theme_options.php');
echo '<hr>';
?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
add_action( 'admin_init', 'pu_register_settings' );
/**
* Function to register the settings
*/
function pu_register_settings()
{
// Register the settings with Validation callback
register_setting( 'pu_theme_options', 'pu_theme_options' );
// Add settings section
add_settings_section( 'pu_text_section', 'Social Links', 'pu_display_section', 'pu_theme_options.php' );
// Create textbox field
$field_args = array(
'type' => 'text',
'id' => 'twitter_link',
'name' => 'twitter_link',
'desc' => 'Twitter Link - Example: http://twitter.com/username',
'std' => '',
'label_for' => 'twitter_link',
'class' => 'css_class'
);
// Add twitter field
add_settings_field( 'twitter_link', 'Twitter', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
$field_args = array(
'type' => 'text',
'id' => 'facebook_link',
'name' => 'facebook_link',
'desc' => 'Facebook Link - Example: http://facebook.com/username',
'std' => '',
'label_for' => 'facebook_link',
'class' => 'css_class'
);
// Add facebook field
add_settings_field( 'facebook_link', 'Facebook', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
$field_args = array(
'type' => 'text',
'id' => 'gplus_link',
'name' => 'gplus_link',
'desc' => 'Google+ Link - Example: http://plus.google.com/user_id',
'std' => '',
'label_for' => 'gplus_link',
'class' => 'css_class'
);
// Add Google+ field
add_settings_field( 'gplus_link', 'Google+', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
$field_args = array(
'type' => 'text',
'id' => 'youtube_link',
'name' => 'youtube_link',
'desc' => 'Youtube Link - Example: https://www.youtube.com/channel/channel_id',
'std' => '',
'label_for' => 'youtube_link',
'class' => 'css_class'
);
// Add youtube field
add_settings_field( 'youtube_ink', 'Youtube', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
$field_args = array(
'type' => 'text',
'id' => 'linkedin_link',
'name' => 'linkedin_link',
'desc' => 'LinkedIn Link - Example: http://linkedin.com/in/username',
'std' => '',
'label_for' => 'linkedin_link',
'class' => 'css_class'
);
// Add LinkedIn field
add_settings_field( 'linkedin_link', 'LinkedIn', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
$field_args = array(
'type' => 'text',
'id' => 'instagram_link',
'name' => 'instagram_link',
'desc' => 'Instagram Link - Example: http://instagram.com/username',
'std' => '',
'label_for' => 'instagram_link',
'class' => 'css_class'
);
// Add Instagram field
add_settings_field( 'instagram_link', 'Instagram', 'pu_display_setting', 'pu_theme_options.php', 'pu_text_section', $field_args );
// Add settings section title here
add_settings_section( 'section_name_here', 'Section Title Here', 'pu_display_section', 'pu_theme_options.php' );
// Create textarea field
$field_args = array(
'type' => 'textarea',
'id' => 'settings_field_1',
'name' => 'settings_field_1',
'desc' => 'Setting Description Here',
'std' => '',
'label_for' => 'settings_field_1'
);
// section_name should be same as section_name above (line 116)
add_settings_field( 'settings_field_1', 'Setting Title Here', 'pu_display_setting', 'pu_theme_options.php', 'section_name_here', $field_args );
// Copy lines 118 through 129 to create additional field within that section
// Copy line 116 for a new section and then 118-129 to create a field in that section
}
// allow wordpress post editor functions to be used in theme options
function pu_display_setting($args)
{
extract( $args );
$option_name = 'pu_theme_options';
$options = get_option( $option_name );
switch ( $type ) {
case 'text':
$options[$id] = stripslashes($options[$id]);
$options[$id] = esc_attr( $options[$id]);
echo "<input class='regular-text$class' type='text' id='$id' name='" . $option_name . "[$id]' value='$options[$id]' />";
echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";
break;
case 'textarea':
$options[$id] = stripslashes($options[$id]);
//$options[$id] = esc_attr( $options[$id]);
$options[$id] = esc_html( $options[$id]);
printf(
wp_editor($options[$id], $id,
array('textarea_name' => $option_name . "[$id]",
'style' => 'width: 200px'
))
);
// echo "<textarea id='$id' name='" . $option_name . "[$id]' rows='10' cols='50'>".$options[$id]."</textarea>";
// echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";
break;
}
}
function pu_validate_settings($input)
{
foreach($input as $k => $v)
{
$newinput[$k] = trim($v);
// Check the input is a letter or a number
if(!preg_match('/^[A-Z0-9 _]*$/i', $v)) {
$newinput[$k] = '';
}
}
return $newinput;
}
// Add custom styles to theme options area
add_action('admin_head', 'custom_style');
function custom_style() {
echo '<style>
.appearance_page_pu_theme_options .wp-editor-wrap {
width: 75%;
}
.regular-textcss_class {
width: 50%;
}
.appearance_page_pu_theme_options h3 {
font-size: 2em;
padding-top: 40px;
}
</style>';
}
// END THEME OPTIONS
/**
* Load site scripts.
*/
function bootstrap_theme_enqueue_scripts() {
$template_url = get_template_directory_uri();
// jQuery.
wp_enqueue_script( 'jquery' );
// Bootstrap
wp_enqueue_script( 'bootstrap-script', $template_url . '/js/bootstrap.min.js', array( 'jquery' ), null, true );
wp_enqueue_style( 'bootstrap-style', $template_url . '/css/bootstrap.min.css' );
//Main Style
wp_enqueue_style( 'main-style', get_stylesheet_uri() );
// Load Thread comments WordPress script.
if ( is_singular() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'bootstrap_theme_enqueue_scripts', 1 );
?>