/
functions.php
590 lines (519 loc) · 20.3 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
/** Tell WordPress to run twentyten_setup() when the 'after_setup_theme' hook is run. */
add_action( 'after_setup_theme', 'twentyten_setup' );
if ( ! function_exists( 'twentyten_setup' ) ):
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which runs
* before the init hook. The init hook is too late for some features, such as indicating
* support post thumbnails.
*
* To override twentyten_setup() in a child theme, add your own twentyten_setup to your child theme's
* functions.php file.
*
* @uses add_theme_support() To add support for post thumbnails and automatic feed links.
* @uses register_nav_menus() To add support for navigation menus.
* @uses add_custom_background() To add support for a custom background.
* @uses add_editor_style() To style the visual editor.
* @uses load_theme_textdomain() For translation/localization support.
* @uses add_custom_image_header() To add support for a custom header.
* @uses register_default_headers() To register the default custom header images provided with the theme.
* @uses set_post_thumbnail_size() To set a custom post thumbnail size.
*
* @since Twenty Ten 1.0
*/
function twentyten_setup() {
// This theme styles the visual editor with editor-style.css to match the theme style.
add_editor_style();
// This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain( 'twentyten', TEMPLATEPATH . '/languages' );
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
require_once( $locale_file );
// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
'primary' => __( 'Primary Navigation', 'twentyten' ),
) );
define('HEADER_IMAGE', '%s/images/blank.png');
define('HEADER_IMAGE_WIDTH', 700);
define('HEADER_IMAGE_HEIGHT', 147);
define('NO_HEADER_TEXT', true);
set_post_thumbnail_size(684, 150, true);
add_image_size('issue-thumbnail', 172, 9999);
add_custom_image_header('', 'bw_admin_header_style');
register_default_headers(array(
'blank' => array(
'url' => '%s/images/blank.png',
'thumbnail_url' => '%s/images/blank-thumbnail.png',
/* translators: header image description */
'description' => __('Blank', 'bw')
)
));
}
endif;
function bw_admin_header_style() {
?>
<style type="text/css">
#headimg {
background: <?php echo bw_get_spotcolor(); ?>;
}
label img {
background: <?php echo bw_get_spotcolor(); ?>;
}
</style>
<?php
}
/**
* Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
*
* To override this in a child theme, remove the filter and optionally add
* your own function tied to the wp_page_menu_args filter hook.
*
* @since Twenty Ten 1.0
*/
function twentyten_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
/**
* Sets the post excerpt length to 40 characters.
*
* To override this length in a child theme, remove the filter and add your own
* function tied to the excerpt_length filter hook.
*
* @since Twenty Ten 1.0
* @return int
*/
function twentyten_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
/**
* Returns a "Continue Reading" link for excerpts
*
* @since Twenty Ten 1.0
* @return string "Continue Reading" link
*/
function twentyten_continue_reading_link() {
return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) . '</a>';
}
/**
* Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentyten_continue_reading_link().
*
* To override this in a child theme, remove the filter and add your own
* function tied to the excerpt_more filter hook.
*
* @since Twenty Ten 1.0
* @return string An ellipsis
*/
function twentyten_auto_excerpt_more( $more ) {
return ' …' . twentyten_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' );
/**
* Adds a pretty "Continue Reading" link to custom post excerpts.
*
* To override this link in a child theme, remove the filter and add your own
* function tied to the get_the_excerpt filter hook.
*
* @since Twenty Ten 1.0
* @return string Excerpt with a pretty "Continue Reading" link
*/
function twentyten_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= twentyten_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' );
if ( ! function_exists( 'twentyten_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own twentyten_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* @since Twenty Ten 1.0
*/
function twentyten_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<article id="comment-<?php comment_ID(); ?>">
<header>
<div class="comment-author vcard">
<?php echo get_avatar( $comment, 40 ); ?>
<?php printf( __( '%s <span class="says">said</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php
/* translators: 1: date, 2: time */
printf( __( 'on %1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a>:
</div><!-- .comment-author .vcard -->
</header>
<?php comment_text(); ?>
<footer>
<?php if ( $comment->comment_approved == '0' ) : ?>
<?php _e( 'This comment is awaiting moderation | ', 'twentyten' ); ?>
<?php endif; ?>
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
<?php edit_comment_link( __( 'Edit', 'twentyten' ), ' | ' ); ?>
</footer>
</article> <!-- #comment-<?php comment_ID(); ?> -->
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
/**
* Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
*
* To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
* function tied to the init hook.
*
* @since Twenty Ten 1.0
* @uses register_sidebar
*/
function twentyten_widgets_init() {
// Area 1, located at the top of the sidebar.
register_sidebar( array(
'name' => __( 'Primary Widget Area', 'twentyten' ),
'id' => 'primary-widget-area',
'description' => __( 'The primary widget area', 'twentyten' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
// Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
register_sidebar( array(
'name' => __( 'Secondary Widget Area', 'twentyten' ),
'id' => 'secondary-widget-area',
'description' => __( 'The secondary widget area', 'twentyten' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
// Front page widget area. Empty by default.
register_sidebar( array(
'name' => __( 'Home Widget Area', 'twentyten' ),
'id' => 'home-widget-area',
'description' => __( 'The front page widget area', 'twentyten' ),
'before_widget' => '<section id="%1$s" class="widget-container %2$s">',
'after_widget' => '</section>',
'before_title' => '<h1>',
'after_title' => '</h1>',
) );
}
/** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
add_action( 'widgets_init', 'twentyten_widgets_init' );
/**
* Removes the default styles that are packaged with the Recent Comments widget.
*
* To override this in a child theme, remove the filter and optionally add your own
* function tied to the widgets_init action hook.
*
* @since Twenty Ten 1.0
*/
function twentyten_remove_recent_comments_style() {
global $wp_widget_factory;
remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
}
add_action( 'widgets_init', 'twentyten_remove_recent_comments_style' );
/**
* Gets the author from the post meta tag "author" if it exists;
* otherwise, falls back to WordPress's "real" author information.
*/
function bw_get_author() {
$custom = get_post_custom();
if ($custom['author']) {
if ($custom['author'][0] == '@none') {
return '';
} else {
return $custom['author'][0];
}
}
return sprintf('<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
get_author_posts_url( get_the_author_meta( 'ID' ) ),
sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
get_the_author());
}
if ( ! function_exists( 'twentyten_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post—date/time and author.
*
* @since Twenty Ten 1.0
*/
function twentyten_posted_on() {
printf( __( '<span class="%1$s">Posted on</span> %2$s%3$s', 'twentyten' ),
'meta-prep meta-prep-author',
sprintf( '<a href="%1$s" rel="bookmark"><time datetime="%2$s" class="entry-date" pubdate>%3$s</time></a>',
get_permalink(),
esc_attr( get_the_time('c') ),
get_the_date()
),
(($author = bw_get_author()) ?
sprintf( __( ' <span class="meta-sep">by</span> %s', 'twentyten' ), $author ) :
''
)
);
}
endif;
if ( ! function_exists( 'twentyten_posted_in' ) ) :
/**
* Prints HTML with meta information for the current post (category, tags and permalink).
*
* @since Twenty Ten 1.0
*/
function twentyten_posted_in() {
// Retrieves tag list of current post, separated by commas.
$tag_list = get_the_tag_list( '', ', ' );
if ( $tag_list ) {
$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
} else {
$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' );
}
// Prints the string, replacing the placeholders.
printf(
$posted_in,
get_the_category_list( ', ' ),
$tag_list,
get_permalink(),
the_title_attribute( 'echo=0' )
);
}
endif;
function bw_get_spotcolor() {
global $post;
$spotcolor = '';
if (is_page()) {
$ancestors = $post->ancestors;
if ($ancestors) {
array_unshift($ancestors, $post->ID);
foreach ($ancestors as $ancestor_id) {
$new_spotcolor = get_post_meta($ancestor_id, 'spotcolor', true);
if ($new_spotcolor) {
$spotcolor = $new_spotcolor;
break;
}
}
}
}
if (!$spotcolor) {
$options = get_option('bw_theme_options');
switch ($options['spotcolor_from']) {
case 'latest_subpage_of':
$sc_posts = new WP_Query(array('posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'page',
'post_parent' => $options['spotcolor_page'],
'meta_key' => 'spotcolor'));
if ($sc_posts->have_posts()) {
$spotcolor = get_post_meta($sc_posts->post->ID,
'spotcolor', true);
}
break;
case 'specific_page':
$spotcolor = get_post_meta($options['spotcolor_page'],
'spotcolor', true);
break;
case 'constant':
$spotcolor = $options['spotcolor_constant'];
break;
}
}
return $spotcolor;
}
//function bw_get_featured_pages
function bw_theme_options_init() {
register_setting('bw_theme_options', 'bw_theme_options',
'bw_validate_theme_options');
}
function bw_theme_options_styles() {
echo "<link rel='stylesheet' href='" .
get_bloginfo( 'stylesheet_directory' ) .
"/admin.css' type='text/css' />\n";
}
function bw_theme_options_add_page() {
$page = add_theme_page(__('Theme Options'), __('Theme Options'),
'edit_theme_options',
'theme_options', 'bw_theme_options_page');
add_action('admin_print_styles', 'bw_theme_options_styles');
}
add_action('admin_init', 'bw_theme_options_init');
add_action('admin_menu', 'bw_theme_options_add_page');
function bw_theme_options_page() {
$options = get_option('bw_theme_options');
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __(' Theme Options') . "</h2>"; ?>
<?php if ( $_REQUEST['updated'] ) : ?>
<div id="message" class="updated"><p><?php _e('Options saved.'); ?></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields('bw_theme_options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Default spot color'); ?></th>
<td id="bw_theme_spotcolor"><fieldset><legend class="screen-reader-text"><span><?php _e( 'Default spot color' ); ?></span></legend>
<p>
<label for="bw_theme_spotcolor_latest_subpage_of">
<input id="bw_theme_spotcolor_latest_subpage_of" name="bw_theme_options[spotcolor_from]" type="radio" value="latest_subpage_of"<?php if ($options['spotcolor_from'] === 'latest_subpage_of') echo ' checked="checked"'; ?> />
<?php _e('Use the spot color of the first subpage of the page below'); ?>
</label>
</p>
<p>
<label for="bw_theme_spotcolor_specific_page">
<input id="bw_theme_spotcolor_specific_page" name="bw_theme_options[spotcolor_from]" type="radio" value="specific_page"<?php if ($options['spotcolor_from'] === 'specific_page') echo ' checked="checked"'; ?> />
<?php _e('Use the spot color of the specific page below'); ?>
</label>
</p>
<ul>
<li>
<?php printf(__('Reference page: %s'),
wp_dropdown_pages(array('name' => 'bw_theme_options[spotcolor_page]',
'show_option_none' => __( '— Select —' ),
'echo' => 0, 'option_none_value' => '0',
'selected' => $options['spotcolor_page'])));
?>
</li>
</ul>
<p>
<label for="bw_theme_spotcolor_constant">
<input id="bw_theme_spotcolor_constant" name="bw_theme_options[spotcolor_from]" type="radio" value="constant"<?php if ($options['spotcolor_from'] === 'constant') echo ' checked="checked"'; ?> />
<?php printf(__('Use a specific color: %s <span class="description">Hex triplet format (#rrggbb)</span>'),
'<input name="bw_theme_options[spotcolor_constant]" ' .
'type="text" size="7" value="' . esc_attr($options['spotcolor_constant']) . '" />'); ?>
</label>
</p>
</fieldset></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Front page feature carousel'); ?></th>
<td id="bw_theme_featured_pages"><fieldset><legend class="screen-reader-text"><span><?php _e('Front page feature carousel'); ?></span></legend>
<p>
<label for="bw_theme_featured_heading">
<?php printf(__('Carousel heading text: %s <span class="description">e.g. “Featured Articles”, “In This Issue”</span>'),
'<input id="bw_theme_featured_heading" name="bw_theme_options[featured_heading]" ' .
'type="text" size="20" value="' . esc_attr($options['featured_heading']) . '" />'); ?>
</label>
</p>
<p>
<?php _e('Display the following articles in the carousel:'); ?>
</p>
<ul>
<li>
<label for="bw_theme_featured_pages_root">
<input id="bw_theme_featured_pages_root" name="bw_theme_options[featured]" type="radio" value="root"<?php if ($options['featured'] === 'root') echo ' checked="checked"'; ?> />
<?php printf(__('Show the subpages of the first subpage of: %s'),
wp_dropdown_pages(array('name' => 'bw_theme_options[featured_root]',
'show_option_none' => __( '— Select —' ),
'echo' => 0, 'option_none_value' => '0',
'selected' => $options['featured_root'])));
?>
</label>
</li>
<li>
<label for="bw_theme_featured_pages_list">
<input id="bw_theme_featured_pages_list" name="bw_theme_options[featured]" type="radio" value="pages"<?php if ($options['featured'] === 'pages') echo ' checked="checked"'; ?> />
<?php _e('Show the following pages:'); ?>
</label>
<div id="bw_theme_featured_pages_container">
<ul>
<?php
// There's no easy way to build a hierarchical list of pages that don't have
// links, so we'll do it ourselves.
$page_list = wp_list_pages(array('title_li' => '', 'echo' => 0));
// Get rid of the <a> tags surrounding each item; replace them with <label>.
$page_list = preg_replace('/<(\/?)a.*?>/', '<$1label>', $page_list);
// Add a checkbox with the page ID from <li class="page-item-##">.
$page_list = preg_replace('/<li class=".*?page-item-([0-9]+).*?"><label>/',
'$0<input name="bw_theme_options[featured_pages][$1]" type="checkbox" /> ',
$page_list);
// Get already-selected values and add ' checked="checked"' to each.
foreach ($options['featured_pages'] as $page_id) {
$page_list = str_replace('name="bw_theme_options[featured_pages][' . $page_id . ']"',
'name="bw_theme_options[featured_pages][' . $page_id . ']" checked="checked"',
$page_list);
}
echo $page_list;
?>
</ul>
</div>
</li>
</ul>
</fieldset></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Options'); ?>" />
</p>
</form>
</div>
<?php
}
function bw_validate_theme_options($input) {
$options = get_option('bw_theme_options');
$pages = get_pages();
// Ensure the spot color preference is one of the three valid options.
if (in_array($input['spotcolor_from'],
array('latest_subpage_of', 'specific_page', 'constant'))) {
$options['spotcolor_from'] = $input['spotcolor_from'];
}
// Make sure the spot color reference page exists.
foreach ($pages as $page) {
if ($page->ID == $input['spotcolor_page']) {
$options['spotcolor_page'] = $input['spotcolor_page'];
}
}
// Is the constant spot color an actual spot color?
$spotcolor_constant = trim($input['spotcolor_constant']);
if (preg_match('/^#[0-9a-f]{6}$/i', $spotcolor_constant)) {
$options['spotcolor_constant'] = $spotcolor_constant;
}
// Ensure the feature preference is one of the two valid options.
if (in_array($input['featured'], array('root', 'pages'))) {
$options['featured'] = $input['featured'];
}
// Any feature carousel heading is valid.
$options['featured_heading'] = $input['featured_heading'];
// Make sure the selected featured pages exist.
foreach ($pages as $page) {
if ($page->ID == $input['featured_root']) {
$options['featured_root'] = $input['featured_root'];
}
}
$options['featured_pages'] = array();
foreach ($input['featured_pages'] as $page_id => $enabled) {
if (!$enabled) {
break;
}
foreach ($pages as $page) {
if ($page->ID == $page_id) {
$options['featured_pages'][] = $page_id;
}
}
}
return $options;
}