/
functions.php
434 lines (302 loc) · 11.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* The functions file is used to initialize everything in the theme. It controls how the theme is loaded and
* sets up the supported features, default actions, and default filters. If making customizations, users
* should create a child theme and make changes to its functions.php file (not this one). Friends don't let
* friends modify parent theme files. ;)
*
* Child themes should do their setup on the 'after_setup_theme' hook with a priority of 11 if they want to
* override parent theme features. Use a priority of 9 if wanting to run before the parent theme.
*
* @package Happy
* @subpackage Functions
* @version 0.1.0
* @author Brian Krogsgard <Brian@Krogsgard.com>
* @copyright Copyright (c) 2011, Brian Krogsgard
* @link http://krogsgard.com
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Load the core theme framework. */
require_once( trailingslashit( TEMPLATEPATH ) . 'library/hybrid.php' );
$theme = new Hybrid();
/* Do theme setup on the 'after_setup_theme' hook. */
add_action( 'after_setup_theme', 'happy_theme_setup' );
/**
* Theme setup function. This function adds support for theme features and defines the default theme
* actions and filters.
*
* @since 0.1.0
*/
function happy_theme_setup() {
/* Get action/filter hook prefix. */
$prefix = hybrid_get_prefix();
/* Add theme support for core framework features. */
add_theme_support( 'hybrid-core-menus', array( 'primary', 'secondary', 'subsidiary' ) );
add_theme_support( 'hybrid-core-sidebars', array( 'primary', 'secondary', 'header', 'subsidiary', 'after-singular' ) );
add_theme_support( 'hybrid-core-widgets' );
add_theme_support( 'hybrid-core-shortcodes' );
add_theme_support( 'hybrid-core-theme-settings', array( 'about', 'footer' ) );
add_theme_support( 'hybrid-core-meta-box-footer' );
add_theme_support( 'hybrid-core-template-hierarchy' );
/* Add theme support for extensions. */
add_theme_support( 'theme-layouts', array( '1c', '2c-l', '2c-r' ) );
add_theme_support( 'dev-stylesheet' );
add_theme_support( 'loop-pagination' );
add_theme_support( 'get-the-image' );
add_theme_support( 'entry-views' );
add_theme_support( 'cleaner-gallery' );
/* Add theme support for WordPress features. */
/* Register support for some post formats */
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'image', 'link', 'quote', 'video' ) );
/* Wraps <blockquote> around quote posts. */
add_filter( 'the_content', 'happy_quote_content' );
/* Includes attached image if user doesn't include content on image post format */
add_filter( 'the_content', 'happy_image_content' );
/* Add infinity symbol for asides. Priority 9 to beat wpautop */
add_filter( 'the_content', 'happy_aside_infinity', 9 );
add_theme_support( 'automatic-feed-links' );
/* Register sidebars. */
add_action( 'widgets_init', 'happy_register_sidebars', 9 );
/* Embed width/height defaults. */
add_filter( 'embed_defaults', 'happy_embed_defaults' );
/* Filter the sidebar widgets. */
add_filter( 'sidebars_widgets', 'happy_disable_sidebars' );
add_action( 'template_redirect', 'happy_one_column' );
/* Change default comment status for pages to false. */
add_action( 'load-page-new.php', 'happy_change_comment_status' );
add_action( 'wp_enqueue_scripts', 'happy_scripts' );
/* change defaults for sidebar parameters */
add_filter( "{$prefix}_sidebar_defaults", 'happy_change_sidebar_defaults' );
/* enable this filter to remove the home page entry title "Home". Don't forget to remove the function as well */
add_filter ( "{$prefix}_entry_title", 'happy_remove_entry_title' );
/* uncomment to add the feature-home-feature template below the logo - also note the function below infobase_home_feature
* by default the home feature template grabs the feature sidebar on the page set as the front page in the reading settings.
* to override this, copy the feature-home-feature.php template from the parent theme and put it in the child theme
* here you can change whatever you like.
*/
add_action( "{$prefix}_before_main", 'happy_home_feature');
}
/**
* Registers scripts for the theme and enqueue those used sitewide.
*
* @since 0.1.0.
*/
function happy_scripts() {
wp_register_script( 'happy-custom', get_template_directory_uri() . '/js/happy-custom.js', array( 'jquery', 'fitvids' ), '20120206', true );
wp_register_script( 'small-menu', get_template_directory_uri() . '/js/small-menu.js', array( 'jquery' ), '20120206', true );
wp_register_script( 'small-menu-secondary', get_template_directory_uri() . '/js/small-menu-secondary.js', array( 'jquery' ), '20120206', true );
wp_register_script( 'fitvids', get_template_directory_uri() . '/js/fitvids.js', array( 'jquery' ), '20120206', true );
wp_enqueue_script( 'happy-custom' );
wp_enqueue_script( 'small-menu' );
wp_enqueue_script( 'small-menu-secondary' );
wp_enqueue_script( 'fitvids' );
/*
* Loads Google font CSS file.
*
* To disable in a child theme, use wp_dequeue_style()
* function happychildtheme_dequeue_fonts() {
* wp_dequeue_style( 'happy-fonts' );
* }
* add_action( 'wp_enqueue_scripts', 'happychildtheme_dequeue_fonts', 11 );
*/
$protocol = is_ssl() ? 'https' : 'http';
wp_enqueue_style( 'happy-fonts', "$protocol://fonts.googleapis.com/css?family=Open+Sans", array(), null );
}
/**
* Registers new sidebars for the theme.
*
* @since 0.1.0.
*/
function happy_register_sidebars() {
register_sidebar(
array(
'name' => __( 'Feature', 'happy' ),
'id' => 'feature',
'description' => __( 'Displayed in the feature area.', 'happy' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s widget-%2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
/**
* Filters Hybrid Core sidebar defaults for the theme.
*
* @since 0.1.0.
*/
function happy_change_sidebar_defaults() {
$defaults = array(
'before_widget' => '<aside id="%1$s" class="widget %2$s widget-%2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
);
return $defaults;
}
/**
* Sets default comment and ping status
* to off for "Page" post types
*
*/
function happy_change_comment_status() {
add_filter( 'option_default_comment_status', '__return_false' );
add_filter( 'option_default_ping_status', '__return_false' );
}
/**
* Function for deciding which pages should have a one-column layout.
*
* @since 0.1.0
*/
function happy_one_column() {
if ( ! is_active_sidebar( 'primary' ) && ! is_active_sidebar( 'secondary' ) )
add_filter( 'get_theme_layout', 'happy_post_layout_one_column' );
elseif ( is_attachment() )
add_filter( 'get_theme_layout', 'happy_post_layout_one_column' );
}
/**
* Filters 'get_post_layout' by returning 'layout-1c'.
*
* @since 0.1.0
*/
function happy_post_layout_one_column( $layout ) {
return 'layout-1c';
}
/**
* Disables sidebars if viewing a one-column page.
*
* @since 0.1.0
*/
function happy_disable_sidebars( $sidebars_widgets ) {
global $wp_query;
if ( current_theme_supports( 'theme-layouts' ) ) {
if ( 'layout-1c' == theme_layouts_get_layout() ) {
$sidebars_widgets['primary'] = false;
$sidebars_widgets['secondary'] = false;
}
}
return $sidebars_widgets;
}
/**
* Overwrites the default widths for embeds. This is especially useful for making sure videos properly
* expand the full width on video pages. This function overwrites what the $content_width variable handles
* with context-based widths.
*
* @since 0.1.0
*/
function happy_embed_defaults( $args ) {
if ( current_theme_supports( 'theme-layouts' ) ) {
$layout = theme_layouts_get_layout();
if ( 'layout-3c-l' == $layout || 'layout-3c-r' == $layout || 'layout-3c-c' == $layout )
$args['width'] = 500;
elseif ( 'layout-1c' == $layout )
$args['width'] = 928;
else
$args['width'] = 650;
} else {
$args['width'] = 650;
}
return $args;
}
/**
* Enable this function w/ the corresponding filter above to remove the home page entry title "Home"
*
* @since 0.1.0.
*/
function happy_remove_entry_title( $title ) {
if ( is_front_page() ) {
$title = '';
}
return $title;
}
/**
* Enable the home feature widget area
*
* @since 0.1.0.
*/
function happy_home_feature() {
get_template_part('feature', 'home-feature');
}
/**
* Wraps the output of the quote post format content in a <blockquote> element if the user hasn't added a
* <blockquote> in the post editor.
*
* @note This function is used from Justin Tadlock's Theme Hybrid community
* @since 0.1.0
* @param string $content The post content.
* @return string $content
*/
function happy_quote_content( $content ) {
if ( has_post_format( 'quote' ) ) {
preg_match( '/<blockquote.*?>/', $content, $matches );
if ( empty( $matches ) )
$content = "<blockquote>{$content}</blockquote>";
}
return $content;
}
/**
* Grabs the first URL from the post content of the current post. This is meant to be used with the link post
* format to easily find the link for the post.
*
* @since 0.1.0
* @return string The link if found. Otherwise, the permalink to the post.
*
* @note This is copied from Justin Tadlock's Theme Hybrid. He modified it from twenty eleven - see below.
* @note This is a modified version of the twentyeleven_url_grabber() function in the TwentyEleven theme.
* @author wordpressdotorg
* @copyright Copyright (c) 2011 - 2012, wordpressdotorg
* @link http://wordpress.org/extend/themes/twentyeleven
* @license http://wordpress.org/about/license
*/
function happy_url_grabber() {
if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', get_the_content(), $matches ) )
return get_permalink( get_the_ID() );
return esc_url_raw( $matches[1] );
}
/**
* Returns the number of images attached to the current post in the loop.
*
* @since 0.1.0
* @return int
* @note This is copied from Justin Tadlock's Theme Hybrid theme, Picturesque.
* @author Justin Tadlock
* @copyright Copyright (c), Justin Tadlock
* @link http://themehybrid.com
*/
function happy_get_image_attachment_count() {
$images = get_children( array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1 ) );
return count( $images );
}
/**
* Returns the featured image for the image post format if the user didn't add any content to the post.
*
* @note This function is used from Justin Tadlock's Theme Hybrid community
* @since 0.1.0
* @param string $content The post content.
* @return string $content
*/
function happy_image_content( $content ) {
if ( has_post_format( 'image' ) && '' == $content ) {
if ( is_singular() )
$content = get_the_image( array( 'size' => 'full', 'meta_key' => false, 'link_to_post' => false ) );
else
$content = get_the_image( array( 'size' => 'full', 'meta_key' => false ) );
}
return $content;
}
/**
* Adds infinity symbol to asides
*
* This function filters the content and, if it's the "aside" post format, adds the infinity symbol
* It runs at priority nine so that it goes before wpautop()
*
* @note credit Justin Tadlock - http://justintadlock.com/archives/2012/09/06/post-formats-aside
* @since 0.1.0
* @param string $content The post content.
* @return string $content
*/
function happy_aside_infinity( $content ) {
if ( has_post_format( 'aside' ) && !is_singular() )
$content .= ' <a href="' . get_permalink() . '"> ∞ </a>';
return $content;
}