-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.php
120 lines (99 loc) · 3.28 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
<?php
// Define paths
define( 'DOCS_URI', get_stylesheet_directory_uri() . '/_docpress' );
define( 'DOCS_PATH', dirname(__FILE__) . '/_docpress' );
// Require theme functions
require_once( 'functions/enqueue-styles.php' );
require_once( 'functions/enqueue-scripts.php' );
// Helpers
require_once( 'functions/get-current-url.php' );
// Docpress
require_once( 'functions/docpress-get-dirs.php' );
require_once( 'functions/docpress-get-file-path.php' );
require_once( 'functions/docpress-make-absolute-urls.php' );
define( 'DOCS_FILE_PATH', docpress_get_file_path() );
// Enables themes to manage the document title tag
add_action( 'after_setup_theme', function() {
add_theme_support( 'title-tag' );
} );
// Remove unused scripts & styles.
add_action('init', function() {
global $pagenow;
if ( 'wp-login.php' !== $pagenow && ! is_admin() ) {
wp_deregister_script( 'wp-embed' );
wp_deregister_script( 'jquery' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
}
} );
// Block 301 redirect.
add_filter( 'redirect_canonical', function( $redirect_url, $requested_url ) {
if ( strpos( $requested_url, 'api/tide/v1' ) === false ) {
return '';
}
}, 10, 2 );
/**
* Filter to override a 404.
*
* The links are actually 404 errors, the pages don't exist really. We're faking status 200
* We just use the routes to pass in info on what file we want to include within index.php.
*/
add_filter( 'template_redirect', function() {
global $wp_query;
if ( file_exists( DOCS_FILE_PATH ) ) {
add_filter( 'wp_title', function( $title ) {
$title = 'Tide Docs';
$fileparts = explode( '/', get_current_url() );
$page = end( $fileparts );
$new_title = ucwords( str_replace( '-', ' ', $page ) );
if ( $new_title ) {
if ( 'Gcp' === $new_title ) {
$new_title = 'Google Cloud Platform';
} else if ( 'Aws' === $new_title ) {
$new_title = 'Amazon Web Services';
} else if ( 'Search' === $new_title ) {
$new_title = 'API Search';
} else if ( 'Api' === $new_title ) {
$new_title = 'API';
} else if ( 'Phpcs Server' === $new_title ) {
$new_title = 'PHPCS Server';
}
$title = "$title — $new_title";
}
return $title;
}, 10 );
status_header( 200 );
$wp_query->is_404 = false;
}
} );
/**
* Filter to override a 404.
*
* The links are actually 404 errors, the pages don't exist really. We're faking status 200
* We just use the routes to pass in info on what file we want to include within index.php.
*/
add_filter( 'body_class', function( $classes ) {
global $wp_query;
if ( strpos( DOCS_FILE_PATH, '404.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) {
$classes[] = 'error404';
}
if ( strpos( DOCS_FILE_PATH, 'search.html' ) !== false && file_exists( DOCS_FILE_PATH ) ) {
$classes[] = 'is-search';
}
return $classes;
} );
/**
* Add GA tracking code to the HEAD.
*/
add_action( 'wp_head', function() {
if ( strpos( get_home_url(), 'wptide.org' ) !== false ) { ?>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-135952573-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-135952573-1');
</script>
<?php }
}, 0 );