forked from interglobalvision/niceshirtfilms-com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
267 lines (215 loc) · 6.91 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
<?php
function scripts_and_styles_method() {
$templateuri = get_template_directory_uri() . '/js/';
$libJs = $templateuri . 'library.js';
wp_enqueue_script( 'js-lib', $libJs,'','',true);
$mainJs = $templateuri . 'main.min.js';
wp_register_script( 'js-main', $mainJs, null, false, true);
$jsVariables = array(
'themeUrl' => get_template_directory_uri()
);
wp_localize_script( 'js-main', 'wp_variables', $jsVariables );
wp_enqueue_script( 'js-main' );
wp_enqueue_style( 'site', get_stylesheet_directory_uri() . '/css/site.min.css' );
// dashicons for admin
if(is_admin()){
wp_enqueue_style( 'dashicons' );
}
}
add_action('wp_enqueue_scripts', 'scripts_and_styles_method');
if( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
if( function_exists( 'add_image_size' ) ) {
add_image_size( 'admin-thumb', 150, 150, false );
add_image_size( 'admin-video-thumb', 150, 84, true );
add_image_size( 'opengraph', 1200, 630, true );
add_image_size( 'post-background', 700, 280, true );
add_image_size( 'post-background-large', 1100, 440, true );
add_image_size( 'post-background-extra-large', 1920, 768, true );
add_image_size( 'grid-thumb', 250, 167, true );
add_image_size( 'grid-thumb-large', 400, 267, true );
add_image_size( 'grid-thumb-larger', 700, 467, true );
add_image_size( 'grid-thumb-largest', 900, 600, true );
add_image_size( 'stills', 763, 9999, false );
add_image_size( 'full', 2000, 9999, false );
add_image_size( 'width-500', 500, 9999, false );
add_image_size( 'width-250', 250, 9999, false );
}
// Register Nav Menus
/*
register_nav_menus( array(
'menu_location' => 'Location Name',
) );
*/
get_template_part( 'lib/gallery' );
get_template_part( 'lib/post-types' );
get_template_part( 'lib/meta-boxes' );
add_action( 'init', 'cmb_initialize_cmb_meta_boxes', 100 );
function cmb_initialize_cmb_meta_boxes() {
// Add CMB2 plugin
if( ! class_exists( 'cmb2_bootstrap_202' ) )
require_once 'lib/CMB2/init.php';
// Add CMB2 Attached Posts Field plugin
if ( ! function_exists( 'cmb2_attached_posts_fields_render' ) ) {
require_once 'lib/cmb2-attached-posts/cmb2-attached-posts-field.php';
}
// Add CMB2 Gallery field
if ( ! function_exists( 'pw_gallery_field' ) ) {
define( 'PW_GALLERY_URL', get_stylesheet_directory_uri() . '/lib/cmb-field-gallery/' );
require_once 'lib/cmb-field-gallery/cmb-field-gallery.php';
}
}
// Disable that freaking admin bar
add_filter('show_admin_bar', '__return_false');
// Turn off version in meta
function no_generator() { return ''; }
add_filter( 'the_generator', 'no_generator' );
// Show thumbnails in admin lists
add_filter('manage_posts_columns', 'new_add_post_thumbnail_column');
function new_add_post_thumbnail_column($cols){
$cols['new_post_thumb'] = __('Thumbnail');
return $cols;
}
add_action('manage_posts_custom_column', 'new_display_post_thumbnail_column', 5, 2);
function new_display_post_thumbnail_column($col, $id){
switch($col){
case 'new_post_thumb':
if( function_exists('the_post_thumbnail') ) {
echo the_post_thumbnail( 'admin-thumb' );
}
else
echo 'Not supported in theme';
break;
}
}
// remove automatic <a> links from images in blog
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);
// Exact match search
add_filter( 'posts_search', 'exact_match_search', 20, 2 );
function exact_match_search( $search, $wp_query ) {
global $wpdb;
if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$search = $searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( $wpdb->esc_like( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]') OR ($wpdb->posts.post_content REGEXP '[[:<:]]{$term}[[:>:]]')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
// custom login logo
function change_my_wp_login_image() {
echo "
<style>
body.login #login h1 a {
background: url('".get_bloginfo('template_url')."/img/dist/niceshirt-logo.svg') 0 no-repeat transparent;
height:72px;
width:312px;
}
</style>
";
}
add_action("login_head", "change_my_wp_login_image");
// UTILITY FUNCTIONS
// get ID of page by slug
function get_id_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if($page) {
return $page->ID;
} else {
return null;
}
}
// is_single for custom post type
function is_single_type($type, $post) {
if (get_post_type($post->ID) === $type) {
return true;
} else {
return false;
}
}
// ALTER QUERY
add_action('pre_get_posts','alter_query');
function alter_query($query) {
global $wp_query;
if ( !$query->is_main_query() ) {
return;
}
if (is_home()) {
$query-> set('post_type' , array('post', 'video'));
}
remove_all_actions ( '__after_loop');
}
// Director name from ID
function echoDirectorName($id) {
$director = get_post($id);
if ($director) {
echo $director->post_title;
}
}
// Get permalink to director page for video
function get_video_permalink($post, $meta) {
$directorPermalink = get_the_permalink($meta['_igv_director'][0]);
return $directorPermalink . '#video-' . $post->post_name;
}
// Excerpt custom ...
function new_excerpt_more( $more ) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
// Check if the posts' content matched the search
function matched_content( $post ) {
global $wp_query;
$search_terms = $wp_query->query_vars['search_terms'];
foreach( $search_terms as $search_term ) {
if( stripos( $post->title, $search_term ) ) {
return FALSE;
}
if( stripos( $post->post_content, $search_term ) ) {
return find_sentence_with_needle( $post->post_content, $search_term );
}
}
return FALSE;
}
// Find and return the sentence from the haystack containing the needle
function find_sentence_with_needle( $haystack, $needle ) {
$sentences = explode('.', $haystack);
// REGEX: Word boundaries around the needle and case insensitive
$word_needle = '/\b' . $needle . '\b/i';
foreach ( $sentences as $sentence ) {
if ( preg_match( $word_needle, $sentence, $matches ) ) {
$sentence = str_ireplace( $needle, '<strong>' . $matches[0] . '</strong>', $sentence);
return $sentence . ".";
}
}
}
// to replace file_get_contents
function url_get_contents($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode !== 200) {
return false;
}
return $output;
}