This repository has been archived by the owner on Dec 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
269 lines (223 loc) · 8.11 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
<?php
/**
* Enqueue scripts and styles.
*
*/
function urn_material_scripts() {
// Load our main stylesheet.
wp_enqueue_style('main-style', get_stylesheet_uri());
// Add custom fonts, used in the main stylesheet.
wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400italic,600,600italic,700,700italic|Raleway:300');
// Add global JS scripts
wp_enqueue_script('nav-overflow', get_template_directory_uri() . '/js/nav-overflow.js', array('jquery'), false, true);
wp_enqueue_script('audio-boom-feed', get_template_directory_uri() . '/js/audio-boom-feed.js', array('jquery'), false, true);
wp_enqueue_script('listen-now', get_template_directory_uri() . '/js/listen-now.js', array('jquery'), false, true);
// Google Analytics
wp_enqueue_script('ga', get_template_directory_uri() . '/js/ga.js', array(), false, true);
// Add specific JS scripts
if ( is_home() || is_page ('schedule')) {
wp_enqueue_script('the-schedule', get_template_directory_uri() . '/js/schedule.js', array('jquery'), false, true);
}
if (is_page ('urn-tv') || is_page ('music')) {
wp_enqueue_script('youmax', get_template_directory_uri() . '/js/youmax.js', array('jquery'), false, true);
}
}
add_action( 'wp_enqueue_scripts', 'urn_material_scripts' );
/**
* Enable Menus under Appearance section in Wordpress
*/
add_theme_support( 'menus' );
if ( function_exists( 'register_nav_menus' ) ) {
// Add the main nav header menu
function register_my_menu() {
register_nav_menu('header-menu', __( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );
}
/**
* Add the responsiveness ('...') to the Header nav
*
* @param [type] $items [description]
* @param [type] $args [description]
*/
function add_extension_to_nav( $items, $args )
{
$items .= "<li class='nav-overflow'>";
$items .= "<a href='#'>···</a>";
$items .= "<ul class='nav-overflow-list'></ul>";
$items .= "</li>";
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_extension_to_nav', 10, 2 ); // 10: priority
/**
* Add Home item to Navbar by filtering wp_nav_menu() and adding additional links
*
* @param [type] $items [description]
*/
function add_additional_nav_items($items) {
if(wp_title('', false) == "") {
$is_current = "current";
} else {
$is_current = "";
}
$homelink = '<li class="nav-item ' . $is_current . '">' .
'<a href="' . home_url( '/' ) . '">' . __('Home') . '</a>' .
'</li>';
$items = $homelink . $items;
return $items;
}
// TODO add something like wp_nav_menu_items:header-nav filter, so this doesnt apply to every nav
add_filter( 'wp_nav_menu_items', 'add_additional_nav_items');
/**
* Format our own custom Nav menu, instead of using the default code WP provides for Navs
*/
class header_nav_walker extends Walker_Nav_Menu
{
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param array $args An array of arguments. @see wp_nav_menu()
* @param int $id ID of element
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 )
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
if($wp_query->post && $wp_query->post->post_title == $item->title) {
$is_current = "current";
} else {
$is_current = "";
}
$output .= $indent . "<li class='nav-item " . $is_current . "'>";
$title = $item->title;
$item_output = '<a href="' . esc_attr( $item->url) .'">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= '</a>';
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args, $id );
}
}
/**
* Shows our stylings inside of the Wordpress Post Editor (when writing a new post)
*
* Styles defined in editor-style.css
*/
add_editor_style();
/**
* Change Wordpress Post Editor Toolbar styles (when writing a new post)
* See more settings here https://codex.wordpress.org/TinyMCE#Customize_TinyMCE_with_Filters
*
* @param array $settings [description]
* @return array [description]
*/
function mytheme_tinymce_settings( $settings ) {
$settings['block_formats'] = 'Heading 1=h1;Heading 2=h2; Normal text=p';
$settings['toolbar1']='formatselect,bold,italic,|,bullist,numlist,link,|,undo,redo';
$settings['toolbar2']='';
$settings['toolbar3']='';
$settings['toolbar4']='';
return $settings;
}
add_filter( 'tiny_mce_before_init', 'mytheme_tinymce_settings' );
/**
* Change the Wordpress Login Logo to the URN logo
*
*/
function urn_login_logo() {
?>
<style type="text/css">
#login h1 a, .login h1 a {
background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/images/logo-black.png');
background-size: 100px;
height: 100px; /* Set same as bg-size */
width: 100%;
}
</style>
<?php
}
add_action( 'login_enqueue_scripts', 'urn_login_logo' );
/**
* Sends the message to the studio
* @param string $message Message to send
* @return bool True if success, false if not
*/
function message_studio($message) {
$sender_ip = $_SERVER['REMOTE_ADDR'];
$url = 'http://int.urn1350.net:8080/web/submit_message.php?type=web&message=' . urlencode($message);
$url .= '&sender=' . urlencode($sender_ip);
$data = file_get_contents($url);
return ($data == 'OK');
}
/**
* [get_posts_by_author description]
* @param int $posts_per_page [description]
* @param int $author [description]
* @param bool $with_excerpt [<description>]
* @return string [description]
*/
function get_posts_by_author($posts_per_page, $author, $with_excerpt = true) {
$output = "<ul class='blog-excerpt'>";
// Start the loop.
while ( have_posts() ) : the_post();
$args = array( 'posts_per_page' => $posts_per_page, 'author' => $author);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$output .= format_blog_excerpt($post, $with_excerpt);
endforeach;
wp_reset_postdata();
// End the loop.
endwhile;
$output .= "</ul>";
// Create Show All Button
$output .= "<div class='btn-wrapper'>";
$output .= "<a href='" . get_permalink( get_page_by_path( 'blogs' ) ) . "'><button class='btn'>Show all</button></a>";
$output .= "</div>";
return $output;
}
/**
* Prepares a blog except so they all match a standard
* @param object $post Post object
* @param bool $with_excerpt Print post excerpt also?
* @return string Html for that excerpt
*/
function format_blog_excerpt($post, $with_excerpt = true, $heading = 'h1') {
$url = get_permalink($post->ID);
$title = $post->post_title;
$date = get_the_date('', $post->ID);
$excerpt = get_the_excerpt();
$output = "<li>";
$output .= "<$heading><a href=" . $url . ">" . $title . "</a></$heading>";
$output .= "<p>" . $date . "</p>";
if ($with_excerpt) {
$output .= "<p>". $excerpt . "</p>";
}
$output .= "<a href=" . $url . "><button class='btn'>Read More</button></a>";
$output .= "</li>";
return $output;
}
/**
* Get the current page title, used with the <title> element.
* @return string Title of the current page
*/
function get_page_title() {
if (is_home()) {
return 'URN: University Radio Nottingham';
}
else {
return wp_title('') . ' - URN';
}
}
add_filter('wp_title', function ($title, $sep, $seplocation) {
if ( is_tax() ) {
$term_title = single_term_title( '', false );
if ( 'right' == $seplocation ) {
$title = $term_title . " $sep ";
} else {
$title = " $sep " . $term_title;
}
}
return $title;
}, 10, 3 );
?>