-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
234 lines (168 loc) · 6.74 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
<?php
if (!class_exists('Timber')){
add_action( 'admin_notices', function(){
echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . admin_url('plugins.php#timber') . '">' . admin_url('plugins.php') . '</a></p></div>';
});
return;
}
class StarterSite extends TimberSite {
function __construct(){
add_theme_support('post-formats');
add_theme_support('post-thumbnails');
add_theme_support('menus');
add_filter('timber_context', array($this, 'add_to_context'));
add_filter('get_twig', array($this, 'add_to_twig'));
add_action('init', array($this, 'register_post_types'));
add_action('init', array($this, 'register_taxonomies'));
add_action('init', array($this, 'tsk_acf_utils'));
parent::__construct();
}
// Note that the following included files only need to contain the taxonomy/CPT/Menu arguments and register_whatever function. They are initialized here.
// http://generatewp.com is nice
function register_post_types(){
require('lib/custom-types.php');
}
function register_taxonomies(){
require('lib/taxonomies.php');
}
function register_menus() {
require('lib/menus.php');
}
function tsk_acf_utils() {
require('lib/acf-utils.php');
}
function add_to_context($context){
$context['notes'] = 'These values are available everytime you call Timber::get_context();';
$context['menu'] = new TimberMenu();
$context['site'] = $this;
// Site-wide Settings
$context['site_callout_bool'] = get_field('site_callout_bool', 'options');
$context['site_callout_text'] = get_field('site_callout_text', 'options');
$context['site_footer_copyright'] = get_field('site_footer_copyright', 'options');
$context['site_footer_credits'] = get_field('site_footer_credits', 'options');
return $context;
}
function add_to_twig($twig){
/* this is where you can add your own fuctions to twig */
$twig->addExtension(new Twig_Extension_StringLoader());
// $twig->addFilter('myfoo', new Twig_Filter_Function('myfoo'));
return $twig;
}
}
new StarterSite();
/*
**************************
* Custom Theme Functions *
**************************
*
* Namespaced "tsk" - find and replace with your own three-letter-thing.
*
*/
// Enqueue scripts
function tsk_scripts() {
// Use jQuery from CDN, enqueue in footer
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', array(), null, true);
wp_enqueue_script('jquery');
}
// Enqueue stylesheet and scripts. Use minified for production.
// NOTE: will need to change this to get_stylesheet_directory_uri() to allow for child themes later.
if( WP_ENV == 'production' ) {
wp_enqueue_style( 'tsk-styles', get_stylesheet_directory_uri() . '/assets/css/build/main.min.css', 1.0);
wp_enqueue_script( 'tsk-js', get_stylesheet_directory_uri() . '/assets/js/build/scripts.min.js', array('jquery'), '1.0.0', true );
} else {
wp_enqueue_style( 'tsk-styles', get_stylesheet_directory_uri() . '/assets/css/main.css', 1.0);
wp_enqueue_script( 'tsk-js', get_stylesheet_directory_uri() . '/assets/js/build/scripts.js', array('jquery'), '1.0.0', true );
}
}
add_action( 'wp_enqueue_scripts', 'tsk_scripts' );
/*
*
* Nice to Haves
*
*/
// Change Title field placeholders for Custom Post Types
// (You'll need to register the types, of course)
function tsk_title_placeholder_text ( $title ) {
if ( get_post_type() == 'service' ) {
$title = __( 'Service Name' );
} else if ( get_post_type() == 'cast-study' ) {
$title = __( 'Case Study Name' );
} else if ( get_post_type() == 'testimonial' ) {
$title = __( 'Testimonial Nickname' );
}
return $title;
}
// add_filter( 'enter_title_here', 'tsk_title_placeholder_text' );
// Customize the editor style
// It's just the Bootstrap typography, but I like it. Got the idea from Roots.io.
function tsk_editor_styles() {
add_editor_style( 'assets/css/editor-style.css' );
}
add_action( 'after_setup_theme', 'tsk_editor_styles' );
// Add excerpts to pages
function tsk_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', 'tsk_add_excerpts_to_pages' );
// Remove inline gallery styles
add_filter( 'use_default_gallery_style', '__return_false' );
/*
*
* Plugin Helpers
*
*/
// Load Gravity Forms JS in the footer...really? Sheesh.
// https://bjornjohansen.no/load-gravity-forms-js-in-footer
function nl_wrap_gform_cdata_open( $content = '' ) {
$content = 'document.addEventListener( "DOMContentLoaded", function() { ';
return $content;
}
add_filter( 'gform_cdata_open', 'nl_wrap_gform_cdata_open' );
function nl_wrap_gform_cdata_close( $content = '' ) {
$content = ' }, false );';
return $content;
}
add_filter( 'gform_cdata_close', 'nl_wrap_gform_cdata_close' );
// Make custom fields work with Yoast SEO (only impacts the light, but helpful!)
// https://imperativeideas.com/making-custom-fields-work-yoast-wordpress-seo/
if ( is_admin() ) { // check to make sure we aren't on the front end
add_filter('wpseo_pre_analysis_post_content', 'tsk_add_custom_to_yoast');
function nl_add_custom_to_yoast( $content ) {
global $post;
$pid = $post->ID;
$custom = get_post_custom($pid);
unset($custom['_yoast_wpseo_focuskw']); // Don't count the keyword in the Yoast field!
foreach( $custom as $key => $value ) {
if( substr( $key, 0, 1 ) != '_' && substr( $value[0], -1) != '}' && !is_array($value[0]) && !empty($value[0])) {
$custom_content .= $value[0] . ' ';
}
}
$content = $content . ' ' . $custom_content;
return $content;
remove_filter('wpseo_pre_analysis_post_content', 'tsk_add_custom_to_yoast'); // don't let WP execute this twice
}
}
// Google Analytics snippet from HTML5 Boilerplate
// Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
define('GOOGLE_ANALYTICS_ID', 'UA-XXXXXX-X');
function mtn_google_analytics() { ?>
<script>
<?php if (WP_ENV === 'production') : ?>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php else : ?>
function ga() {
console.log('GoogleAnalytics: ' + [].slice.call(arguments));
}
<?php endif; ?>
ga('create','<?php echo GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
</script>
<?php }
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
add_action('wp_footer', 'mtn_google_analytics', 20);
}