/
functions.php
254 lines (214 loc) · 7.54 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
<?php
// ____ _ _
// | ___| __ _ _ __ __| | |__ _____ __
// |___ \ / _` | '_ \ / _` | '_ \ / _ \ \/ /
// ___) | (_| | | | | (_| | |_) | (_) > <
// |____/ \__,_|_| |_|\__,_|_.__/ \___/_/\_\
//
// Requires and includes
//
require_once 'lib/Mobile_Detect.php';
//
// Enqueue scripts
//
// Javascript
function sandbox_enqueue_scripts() {
$application_js = sandbox_is_local() ? 'application.js' : 'application.min.js';
$javascript_version = filemtime( get_stylesheet_directory() . '/js/build/' . $application_js);
wp_enqueue_script('jquery');
wp_enqueue_script(
'application',
get_stylesheet_directory_uri() . '/js/build/' . $application_js,
array('jquery'),
$javascript_version,
true // insert in footer
);
$css_version = filemtime( get_stylesheet_directory() . '/style.css');
wp_enqueue_style(
'sandbox-stylesheet',
get_stylesheet_directory_uri() . '/style.css',
array(),
$css_version,
'all'
);
}
add_action( 'wp_enqueue_scripts', 'sandbox_enqueue_scripts' );
//
// Enables
//
if( function_exists('acf_add_options_page') ) {
acf_add_options_page();
}
// Custom menus
add_theme_support('menus');
// Custom Image Sizes (Name, Width, Height, Hard Crop boolean)
add_image_size( 'small', 400, 300, false );
//
// Disables
//
// Disable the rich text visual editor
// add_filter('user_can_richedit' , create_function('' , 'return false;') , 50);
// Disable Admin Bar
add_filter('show_admin_bar', '__return_false');
// Disable Wordpress Generator meta tag
function sandbox_version_info() {
return '';
}
add_filter('the_generator', 'sandbox_version_info');
// Remove unnecessary wp_head items
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
// Remove meta boxes from dashboard
function sandbox_remove_dashboard_widgets(){
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);}
add_action('wp_dashboard_setup', 'sandbox_remove_dashboard_widgets' );
// Remove unneccesary admin menu panels. Uncomment to disable
function sandbox_remove_menus(){
// remove_menu_page( 'index.php' ); //Dashboard
// remove_menu_page( 'edit.php' ); //Posts
// remove_menu_page( 'upload.php' ); //Media
// remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
// remove_menu_page( 'themes.php' ); //Appearance
// remove_menu_page( 'plugins.php' ); //Plugins
// remove_menu_page( 'users.php' ); //Users
// remove_menu_page( 'tools.php' ); //Tools
// remove_menu_page( 'options-general.php' ); //Settings
}
add_action( 'admin_menu', 'sandbox_remove_menus' );
//
// Custom functions
//
function fanton_list_mode() {
$detect = new Mobile_Detect;
if ( isset( $_GET['grid'] ) ) {
$truth = false;
} else if ( $detect->isMobile() && !$detect->isTablet() ) {
$truth = true;
} else if ( isset( $_GET['list'] ) ) {
$truth = true;
} else {
$truth = false;
}
return $truth;
}
function fanton_grid_mode() {
return !fanton_list_mode();
}
// Check if running on localhost
function sandbox_is_local() {
$localhost_whitelist = array( '127.0.0.1', '::1' );
if( in_array($_SERVER['REMOTE_ADDR'], $localhost_whitelist) ) {
return true;
} else {
return false;
}
}
function sandbox_get_id_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}
// Get an <img> at size from an ACF image field
function sandbox_image($acf_image_field_name='image', $image_size='large', $classes='') {
$image = get_field($acf_image_field_name); // check for a top level field
if(empty($image)) $image = get_sub_field($acf_image_field_name); // check for a sub-field
$alt = $image['alt'];
if(empty($alt)) $alt = $image['title'];
$size = $image_size;
$url = $image['sizes'][$size];
$width = $image['sizes'][$size.'-width'];
$height = $image['sizes'][$size.'-height'];
echo '<img src="'.$url.'" data-src="'.$url.'" width="'.$width.'" height="'.$height.'" alt="'.$alt.'" class="lazyload '.$classes.'">';
}
// Get <img> tags from a ACF repeater
function sandbox_images($acf_repeater='images', $acf_image_field_name='image', $image_size='large', $classes='') {
if(have_rows($acf_repeater)):
while (have_rows($acf_repeater)): the_row();
sandbox_image($acf_image_field_name, $image_size, $classes);
endwhile;
endif;
}
// Function to create slug out of text
function sandbox_slugify( $text ) {
$str = strtolower( trim( $text ) );
$str = preg_replace( '/[^a-z0-9-]/', '-', $str );
$str = preg_replace( '/-+/', "-", $str );
return trim( $str, '-' );
}
// Custom excerpt size
function sandbox_custom_excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
// Limit content
function sandbox_content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
}
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
//
// Filters
//
// Add page slug to body class
function sandbox_add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'sandbox_add_slug_body_class' );
function sandbox_add_slug_class_to_menu_item($output){
$ps = get_option('permalink_structure');
if(!empty($ps)){
$idstr = preg_match_all('/<li id="menu-item-(\d+)/', $output, $matches);
foreach($matches[1] as $mid){
$id = get_post_meta($mid, '_menu_item_object_id', true);
$slug = basename(get_permalink($id));
$output = preg_replace('/menu-item-'.$mid.'">/', 'menu-item-'.$mid.'" data-slug="'.$slug.'">', $output, 1);
}
}
return $output;
}
add_filter('wp_nav_menu', 'sandbox_add_slug_class_to_menu_item');
//
// Shortcode functions
//
function sandbox_indented_shortcode( $atts, $content = null ) {
$a = shortcode_atts( array(
'character' => ' '
), $atts );
return '<div class="indented-character"><div class="indented-character__character">'.$a['character'].'</div><div class="indented-character__content">'.$content.'</div></div>';
}
add_shortcode( 'indented', 'sandbox_indented_shortcode' );
?>