-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
280 lines (224 loc) · 8.94 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
<?php
//Register the New Student and Old Student Menus
function register_all_menu() {
register_nav_menus ( array (
'ns-menu' => 'New Student Menu',
'os-menu' => 'Old Student Menu'
) );
}
add_action( 'init', 'register_all_menu' );
//Customize the wp-login.php page (only shown during a failed login at mobile menu)
function my_login_logo_url() {
return home_url();
}
add_filter( 'login_headerurl', 'my_login_logo_url' );
function my_login_logo_url_title() {
return 'Dharā Dhamma';
}
add_filter( 'login_headertitle', 'my_login_logo_url_title' );
function my_login_logo() {
?>
<style type="text/css">
body.login div#login h1 a {
background-image: url(<?php echo get_stylesheet_directory_uri();?>/images/aniwheel.gif );
background-size: 80px 80px;
}
body.login div#login_error a {
display: none;
}
body.login p#nav {
display: none;
}
</style>
<?php
}
add_action( 'login_enqueue_scripts', 'my_login_logo' );
// Local news feeds
function local_news_feed ( $name, $categoryID ) {
$recent_posts = wp_get_recent_posts ( array ( 'category' => $categoryID, 'post_status' => 'publish' ) );
if ( !empty ( $recent_posts ) ) {
echo "<h2>$name News</h2>";
echo '<ul class="local-page-news-items">';
foreach ( $recent_posts as $recent ) {
echo '<li class="home-page-news-item">';
echo '<a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a>';
echo '</li>';
}
echo '</ul>';
}
wp_reset_query();
}
//Useful functions for checking if we're on an os-page or we want the os-menu, for use in other places in theme
function prefers_os_menu ( $wp ) {
$current_url = home_url ( add_query_arg ( array(), $wp->request ) );
if ( preg_match ( "|/os|i", $current_url ) ) {
return true;
} else if ( preg_match ( "|/category/|i", $current_url ) ) {
$current_announcements_ID = 50;
$old_announcements_ID = 49;
$prefers_ns_categories = array ( $current_announcements_ID, $old_announcements_ID );
$cat_ID = get_queried_object()->term_id;
return !in_array ( $cat_ID, $prefers_ns_categories );
} else if ( is_single() ) {
return true;
} else if ( is_search() ) {
return true;
} else if ( is_content_restricted() ) {
return true;
}
return false;
}
function is_os_page ( $wp ) {
$is_os_page = false;
if ( is_user_logged_in() && prefers_os_menu ( $wp ) ) {
$is_os_page = true;
}
return $is_os_page;
}
function is_content_restricted ( ) {
//get the slug requested
$slug = $_SERVER [ 'REQUEST_URI' ];
$post_restricted = false;
$page_restricted = false;
//First check if it's a page and restricted
$page = get_page_by_path ( $slug );
if ( !is_null ( $page ) ) {
$page_id = $page->ID;
$page_restricted = is_restricted_rs ( $page_id );
} else {
//Then check if it's a post and restricted
$args = array(
'name' => $slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts ( $args );
if ( $my_posts ) {
$post_id = $my_posts[0]->ID;
$post_restricted = is_restricted_rs ( $post_id );
}
}
return $post_restricted || $page_restricted;
}
// This function returns the destination URL after a logout as a string.
function url_after_logout( $wp ) {
$url = home_url();
if ( is_search() ) {
// Search is a good place to stay after logging out.
// It only needs special handling because both
// get_permalink() and weird about search.
$url = get_search_link();
} else if ( is_category() ) {
$category = get_queried_object();
$url = get_category_link ( $category->term_id );
} else {
$permalink = get_permalink();
if ( $permalink ) {
// Proper page/post with permalink. Stay there.
$url = $permalink;
}
}
return $url;
}
//Add current_page_ancestor to news top-level menu item
function cpt_archive_classes( $classes , $item ){
if ( is_archive() || is_single() ) {
$classes = str_replace( 'menu-item-' . get_theme_mod('dhamma_news_menu_item_id'), 'menu-item-4942 current_page_ancestor', $classes );
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'cpt_archive_classes', 10, 2 );
//This removes the admin bar from everyone but admins.
add_action('init', 'remove_admin_bar');
function remove_admin_bar() {
if (!current_user_can('administrator') && !is_admin()) {
show_admin_bar(false);
}
}
//This blocks non-administrative users from accessing the dashboard, redirects to home page instead
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
/**
* Improves the caption shortcode with HTML5 figure & figcaption; microdata & wai-aria attributes
*
* @param string $val Empty
* @param array $attr Shortcode attributes
* @param string $content Shortcode content
* @return string Shortcode output
*/
function dhamma_img_caption_shortcode_filter($val, $attr, $content = null) {
extract(shortcode_atts(array(
'id' => '',
'align' => 'aligncenter',
'width' => '',
'caption' => ''
), $attr));
// No caption, no dice... But why width?
if ( 1 > (int) $width || empty($caption) )
return $val;
if ( $id )
$id = esc_attr( $id );
// Add itemprop="contentURL" to image - Ugly hack
$content = str_replace('<img', '<img itemprop="contentURL"', $content);
$retMe = "";
if ( $align == "aligncenter" ) $retMe .= "<div class='image-caption-center'>";
else if ( $align == "alignleft" ) $retMe .= "<div class='image-caption-left'>";
else if ( $align == "alignright" ) $retMe .= "<div class='image-caption-right'>";
$retMe .= '<figure id="' . $id . '" aria-describedby="figcaption_' . $id . '" class="wp-caption ' . esc_attr($align) . '" itemscope itemtype="http://schema.org/ImageObject" style="width: ' . (0 + (int) $width) . 'px">' . do_shortcode( $content ) . '<figcaption id="figcaption_'. $id . '" class="wp-caption-text" itemprop="description">' . $caption . '</figcaption></figure>';
$retMe .= "</div>";
return $retMe;
}
add_filter( 'img_caption_shortcode', 'dhamma_img_caption_shortcode_filter', 10, 3 );
//Customize the "[...]" read more link at the end of the_excerpt()
function new_excerpt_more( $more ) {
return '<p><a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read the rest of this entry »</a></p>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
/* Add theme customization interface to control panel */
function dhara_customize_register( $wp_customize ) {
/**
* Adds textarea support to the theme customizer
*/
class dhara_textarea_control extends WP_Customize_Control {
public $type = 'textarea';
public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
</label>
<?php
}
}
$wp_customize->add_section(
'dhara_customizer',
array(
'title' => 'Theme Customization',
'priority' => 199
)
);
$wp_customize->add_setting('dhamma_short_location', array('transport' => 'refresh'));
$wp_customize->add_control('dhamma_short_location', array('section' => 'dhara_customizer', 'label' => 'Header Short Location', 'type' => 'text'));
$wp_customize->add_setting('dhamma_schedule_link', array('transport' => 'refresh', 'sanitize_callback' => 'dhara_sanitize_uri'));
$wp_customize->add_control('dhamma_schedule_link', array('section' => 'dhara_customizer', 'label' => 'Schedule Link (for mobile menu)', 'type' => 'text'));
$wp_customize->add_setting('dhamma_picture_icon', array('transport' => 'refresh', 'sanitize_callback' => 'dhara_sanitize_uri'));
$wp_customize->add_control('dhamma_picture_icon', array('section' => 'dhara_customizer', 'label' => 'Picture Icon URL (Top Left)', 'type' => 'text'));
$wp_customize->add_setting('dhamma_news_menu_item_id', array('transport' => 'refresh'));
$wp_customize->add_control('dhamma_news_menu_item_id', array('section' => 'dhara_customizer', 'label' => 'News Menu Item Post ID (For Current Page Menu Highlighting)', 'type' => 'text'));
$wp_customize->add_setting('dhamma_title_separator', array('transport' => 'refresh', 'sanitize_callback' => 'sanitize_text_field'));
$wp_customize->add_control('dhamma_title_separator', array('section' => 'dhara_customizer', 'label' => 'Separator for Browser Title', 'type' => 'text'));
}
add_action( 'customize_register', 'dhara_customize_register' );
function dhara_sanitize_uri($uri){
if('' === $uri){
return '';
}
return esc_url_raw($uri);
}