This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
forked from jorditost/wp-reset-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·460 lines (360 loc) · 13.6 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
<?php
/**
* Functions File
*
* @copyright Copyright © 2013 Jordi Tost
* @license http://www.opensource.org/licenses/gpl-2.0.php GNU GPL version 2
* @version 2.0
*
* @Developer Jordi Tost (Follow Me: @jorditost), Guillaume Mutschler (guillaumemutschler.eu)
*
* Notes: PHP vars are lowercase.
* Vars that are passed to jQuery are camelcase.
*/
//////////////////
// Custom Utils
//////////////////
require_once('inc/utils/Mobile_Detect.php');
require_once('inc/wp-utils/wp-utils.php');
require_once('inc/wp-utils/wp-language-utils.php');
require_once('inc/wp-utils/wp-gallery-utils.php');
require_once('inc/wp-utils/wp-client-utils.php');
require_once('inc/custom-post-types.php');
//////////////////
// Detect dev environment
//////////////////
add_action('init', 'is_production');
function is_production(){
// Define Environments
$environments = array(
'local' => array('.local', 'local.','localhost',),
'development' => 'dev.',
'staging' => 'stage.',
'preview' => 'preview.',
);
// Get Server name
$server_name = $_SERVER['SERVER_NAME'];
foreach($environments AS $key => $env){
if(is_array($env)){
foreach ($env as $option){
if(stristr($server_name, $option)){
define('ENVIRONMENT', $key);
break 2;
}
}
} else {
if(stristr($server_name, $env)){
define('ENVIRONMENT', $key);
break;
}
}
}
// If no environment is set default to production
if(!defined('ENVIRONMENT')) define('ENVIRONMENT', 'production');
if(ENVIRONMENT == 'production'){
return true;
}else{
return false;
}
}
/////////////////////
// Inits & Globals
/////////////////////
// Load jQuery in Footer
global $load_jquery_in_footer;
$load_jquery_in_footer = true;
// Test vars
global $test;
$test = true;
//////////////////////
// Mobile Detection
//////////////////////
global $detect;
global $isMobile;
global $isIpad;
global $isTablet;
global $isIE;
$detect = new Mobile_Detect();
$isMobile = ( $detect->isMobile() ) ? true : false;
$isIpad = ( $detect->isIpad() ) ? true : false;
$isTablet = ( $detect->isTablet() ) ? true : false;
$isIE = ( $detect->isIE() ) ? true : false;
function mobile_class() {
global $isMobile;
global $isIpad;
global $isTablet;
if ($isMobile) {
echo " mobile";
}
if ($isIpad || $isTablet) {
echo " tablet";
}
}
////////////////////////
// Section Functions
////////////////////////
//add_action('wp_head', 'check_section');
function get_section_class() {
global $current_section;
return (isset($current_section) && !empty($current_section)) ? 'section-' . $current_section : '';
}
////////////////////////
// Language Functions
////////////////////////
global $sitetrings;
$siteStrings = array(
// Usage: 'text_id' => __('[:en]Text in English[:de]Text auf Deutsch')
);
function get_site_text($text_id) {
global $siteStrings;
return $siteStrings[$text_id];
}
function get_language_code() {
if (function_exists('qtrans_getLanguage')) {
return qtrans_getLanguage();
}
return '';
}
// Get a custom field for the current language
function get_post_custom_field_lang($custom_field) {
$lang = get_language_code();
$lang_ext = (!empty($lang)) ? '_' . $lang : '';
return get_post_custom_field($custom_field . $lang_ext);
}
///////////////////////
// Content Functions
///////////////////////
///////////////////////////////
// Theme Admin Customization
///////////////////////////////
// Edit admin menus
// http://wp.tutsplus.com/tutorials/creative-coding/customizing-your-wordpress-admin/
function edit_admin_menus() {
global $menu;
// Change Posts to Blog
//$menu[5][0] = 'Blog';
// Remove menus
//remove_menu_page('edit.php'); // Remove Posts
remove_menu_page('edit-comments.php'); // Remove Comments
//remove_menu_page('link-manager.php'); // Remove Links
//remove_menu_page('tools.php');
}
add_action( 'admin_menu', 'edit_admin_menus' );
// Custom Menu Order
function custom_menu_order($menu_ord) {
if (!$menu_ord) return true;
return array(
'index.php', // this represents the dashboard link
//'edit.php', //the posts tab
'edit.php?post_type=page', //the pages tab
//'edit.php?post_type=umfrage' //the "umfrage" tab
);
}
//add_filter('custom_menu_order', 'custom_menu_order');
//add_filter('menu_order', 'custom_menu_order');
// Remove category from edit/add new post screen
function my_list_terms_exclusions( $exclusions, $args ) {
global $pagenow;
if (in_array($pagenow,array('post.php','post-new.php'))) {
$exclusions = " {$exclusions} AND t.slug NOT IN ('twitter')";
}
return $exclusions;
}
//add_filter('list_terms_exclusions', 'my_list_terms_exclusions', 10, 2);
// Remove admin bar
add_filter('show_admin_bar', '__return_false');
///////////////////
// Notifications
///////////////////
// Uncomment to remove WP Version update notifications
//add_action('admin_menu','remove_wp_update_notifications');
// Remove notifications for login plugins
function filter_plugin_updates( $value ) {
unset( $value->response['akismet/akismet.php'] );
return $value;
}
add_filter( 'site_transient_update_plugins', 'filter_plugin_updates' );
// Avoid plugin deactivation
function disable_plugin_deactivation( $actions, $plugin_file, $plugin_data, $context ) {
// Remove edit link for all
if ( array_key_exists( 'edit', $actions ) )
unset( $actions['edit'] );
// Remove deactivate link for crucial plugins
if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(
//'plugin_folder/plugin_main_script.php'
)))
unset( $actions['deactivate'] );
return $actions;
}
//add_filter( 'plugin_action_links', 'disable_plugin_deactivation', 10, 4 );
/////////////////
// Theme Setup
/////////////////
add_action( 'after_setup_theme', 'my_theme_setup' );
if ( ! function_exists( 'my_theme_setup' ) ):
function my_theme_setup() {
// Tiny MCE styles
add_editor_style();
// This theme uses post thumbnails
add_theme_support( 'post-thumbnails' );
// Register extra featured images
// http://wordpress.org/plugins/multiple-post-thumbnails/
// https://github.com/voceconnect/multi-post-thumbnails
if (class_exists('MultiPostThumbnails')) {
$types = array('post', 'page', 'custom_pt');
foreach($types as $type) {
new MultiPostThumbnails(array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => $type
)
);
}
// Array with all registered IDs for compatibility with wp-gallery-utils.php
global $exclude_thumb_ids;
$exclude_thumb_ids = array(
'post' => array('secondary-image'),
'custom_pt' => array('secondary-image-cpt')
);
// Language images
// Translate post images for pages, eBook Services and Software Details
if (function_exists('qtrans_getLanguage')) {
global $q_config;
$types = array('page', 'ebook-service', 'software');
// Languages
$langs = qtrans_getSortedLanguages(); //array('en', 'es');
foreach($langs as $lang) {
// Use default thumbnail for default language (Deutsch)
if ($lang == $q_config['default_language']) continue;
// Post types
foreach($types as $type) {
new MultiPostThumbnails(
array(
'label' => 'Beitragsbild (' . strtoupper($lang) . ')',
'id' => 'thumbnail-'.$lang,
'post_type' => $type
)
);
// Add exclude
// If key exists, add thumbnail id
if ($exclude_thumb_ids[$type]) {
array_push($exclude_thumb_ids[$type], 'thumbnail-'.$lang);
// If not defined, create array key
} else {
$exclude_thumb_ids[$type] = array('thumbnail-'.$lang);
}
}
}
}
}
// Images size
if ( function_exists( 'add_image_size' ) ) {
//set_post_thumbnail_size( 320, 200 ); // Retrieved as 'post-thumbnail'
//add_image_size( 'single-column', 320, 9999 ); // 320 pixels wide (and unlimited height)
//add_image_size( 'primary-column', 660, 9999 ); // 660 pixels wide (and unlimited height)
}
// Add support for menus
register_nav_menus( array(
'main-menu' => 'Main menu',
'footer-menu' => 'Footer menu'
));
// Add default posts and comments RSS feed links to head
add_theme_support( 'automatic-feed-links' );
}
endif;
// Excerpt box for Pages
if ( function_exists('add_post_type_support') ) {
add_action('init', 'add_page_excerpts');
function add_page_excerpts() {
add_post_type_support( 'page', 'excerpt' );
}
}
//////////////////
// Init Scripts
//////////////////
function my_scripts_method() {
// Register jQuery
// =================
// by using the wp_enqueue_scripts hook (instead of the init hook which many articles reference),
// we avoid registering the alternate jQuery on admin pages, which will cause post editing (amongst other things)
// to break after upgrades often.
global $load_jquery_in_footer;
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ), false, false, $load_jquery_in_footer);
global $detect;
global $test;
global $isMobile;
global $isIpad;
global $isTablet;
global $isIE;
global $siteStrings;
if ( $isMobile ) {
} else {
}
$deps = array('jquery');
// Site functions
if ( $test ) {
wp_enqueue_script('theme_functions', get_template_directory_uri().'/js/functions.js', $deps, null, true);
} else {
wp_enqueue_script('theme_functions', get_template_directory_uri().'/js/functions.min.js', $deps, null, true);
}
// Localize script to use 'siteVars' in functions.js file
wp_localize_script(
'theme_functions',
'siteVars',
array(
'siteurl' => get_option('siteurl'),
'ajaxurl' => (function_exists('qtrans_getLanguage')) ?
admin_url('admin-ajax.php?lang=' . qtrans_getLanguage()) :
admin_url( 'admin-ajax.php' ),
'lang' => get_language_code(),
'siteStrings' => json_encode($siteStrings),
'isMobile' => $isMobile,
'isIpad' => $isIpad,
'isTablet' => $isTablet,
'isIE' => $isIE
)
);
// Remove "Comment Reply" scripts
wp_dequeue_script('comment-reply');
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method', 20 );
///////////////////
// Admin Scripts
///////////////////
function my_admin_scripts_method() {
//wp_register_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-datepicker', get_template_directory_uri().'/js/ui.datepicker.js', array('jquery','jquery-ui-core'));
wp_enqueue_script('admin-js-functions',get_template_directory_uri().'/js/admin.js', array('jquery','jquery-ui-core','jquery-ui-datepicker'));
// Path to jQuery UI theme stylesheet
wp_enqueue_style('jquery-ui','http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
wp_print_styles();
}
//add_action('admin_print_scripts', 'my_admin_scripts_method');
//////////////////////////////////////
// keeps crawlers out of the dev env
//////////////////////////////////////
function robots_access(){
if(is_production() && get_option('blog_public') == '0') update_option('blog_public', '1');
if(!is_production() && get_option('blog_public') == '1') update_option('blog_public', '0');
}
add_action('init', 'robots_access');
/////////////////////////////////
// HTML5 Reset initializations
/////////////////////////////////
// Clean up the <head>
function remove_head_links() {
remove_action('wp_head', 'rsd_link'); //Really Simple Discovery service: Used by 3rd party software posts (like phones wordpress app -> reactivate if needed)
remove_action('wp_head', 'wlwmanifest_link'); //Used by Windows Live Writer
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Display the links to the extra feeds such as category feeds
remove_action( 'wp_head', 'feed_links', 2 ); // Display the links to the general feeds: Post and Comment Feed
remove_action( 'wp_head', 'index_rel_link' ); // index link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); // Display relational links for the posts adjacent to the current post.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_generator' ); // Display the XHTML generator that is generated on the wp_head hook, WP version
}
add_action('init', 'remove_head_links');
//remove_action('wp_head', 'wp_generator');
?>