-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
executable file
·226 lines (164 loc) · 7.13 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
<?php
// If the Timber plugin isn't activated, print a notice in the admin.
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="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
} );
return;
}
// Create our version of the TimberSite object
class StarterSite extends TimberSite {
// This function applies some fundamental WordPress setup, as well as our functions to include custom post types and taxonomies.
function __construct() {
add_theme_support( 'post-formats',
array(
'image',
'gallery',
'video',
'quote',
'link'
) );
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 400, 400, false );
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, 'register_menus' ) );
add_action( 'init', array( $this, 'register_widgets' ) );
parent::__construct();
}
// Abstracting long chunks of code.
// The following included files only need to contain the arguments and register_whatever functions. They are applied to WordPress in these functions that are hooked to init above.
// The point of having separate files is solely to save space in this file. Think of them as a standard PHP include or require.
function register_post_types(){
require('lib/custom-types.php');
}
function register_taxonomies(){
require('lib/taxonomies.php');
}
function register_menus(){
require('lib/menus.php');
}
function register_widgets(){
require('lib/widgets.php');
}
// Access data site-wide.
// This function adds data to the global context of your site. In less-jargon-y terms, any values in this function are available on any view of your website. Anything that occurs on every page should be added here.
function add_to_context( $context ) {
// Our menu occurs on every page, so we add it to the global context.
$context['menu'] = new TimberMenu();
if (!is_archive()) {
$context['sidebar'] = Timber::get_sidebar('sidebar.php');
}
// This 'site' context below allows you to access main site information like the site title or description.
$context['site'] = $this;
// All Categories
$context['cats'] = Timber::get_terms('category');
// Posts page link
$context['posts_page_link'] = get_permalink(get_option('page_for_posts' ));
$context['logged_in'] = is_user_logged_in();
return $context;
}
// Here you can add your own fuctions to Twig. Don't worry about this section if you don't come across a need for it.
// See more here: http://twig.sensiolabs.org/doc/advanced.html
function add_to_twig( $twig ) {
$twig->addExtension( new Twig_Extension_StringLoader() );
$twig->addFilter( 'myfoo', new Twig_Filter_Function( 'myfoo' ) );
return $twig;
}
}
new StarterSite();
/*
*
* My Functions (not from Timber)
*
*/
// Enqueue scripts
function sd_scripts() {
// Use jQuery from a CDN
wp_deregister_script('jquery');
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js', array(), null, true);
// Enqueue our stylesheet and JS file with a jQuery dependency.
// Note that we aren't using WordPress' default style.css, and instead enqueueing the file of compiled Sass.
// Use minified on staging and prod, unminified on dev.
if (WP_ENV == 'development') {
wp_enqueue_style( 'sd-styles', get_template_directory_uri() . '/assets/css/main.css', 1.0);
wp_enqueue_script( 'sd-js', get_template_directory_uri() . '/assets/js/build/scripts.js', array('jquery'), '1.0.0', true );
} else {
wp_enqueue_style( 'sd-styles', get_template_directory_uri() . '/assets/css/main.min.css', 1.0);
wp_enqueue_script( 'sd-js', get_template_directory_uri() . '/assets/js/build/scripts.min.js', array('jquery'), '1.0.0', true );
}
}
add_action( 'wp_enqueue_scripts', 'sd_scripts' );
// Show notice on Dev. and Staging
function sd_env_notice() {
if (WP_ENV != 'production') {
echo '<p class="' . WP_ENV . ' env-notice">' . WP_ENV . '</p>';
}
}
add_action('wp_head', 'sd_env_notice');
// Customize the editor style
// It's just the Bootstrap typography, but I like it. Got the idea from Roots.io.
function twc_editor_styles() {
add_editor_style( 'assets/css/editor-style.css' );
}
add_action( 'after_setup_theme', 'twc_editor_styles' );
// Move excerpt box top top of post editor
// https://wpartisan.me/tutorials/wordpress-how-to-move-the-excerpt-meta-box-above-the-editor
// Removes the regular excerpt box. We're not getting rid of it, we're just moving it above the wysiwyg editor
function oz_remove_normal_excerpt() {
remove_meta_box( 'postexcerpt', array('post', 'book', 'recipe'), 'normal' );
}
add_action( 'admin_menu' , 'oz_remove_normal_excerpt' );
// Add the excerpt meta box back in with a custom screen location
function oz_add_excerpt_meta_box( $post_type ) {
if ( in_array( $post_type, array( 'post', 'recipe', 'book' ) ) ) {
add_meta_box(
'oz_postexcerpt',
__( 'Summary', 'thetab-theme' ),
'sd_post_excerpt_meta_box',
$post_type,
'after_title',
'high'
);
}
}
add_action( 'add_meta_boxes', 'oz_add_excerpt_meta_box' );
function sd_post_excerpt_meta_box($post) {
?>
<label class="screen-reader-text" for="excerpt"><?php _e('Excerpt') ?></label><textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt; // textarea_escaped ?></textarea>
<p><?php _e('A sentence or two summary of this post.'); ?></p>
<?php
}
// You can't actually add meta boxes after the title by default in WP so we're being cheeky. We've registered our own meta box position `after_title` onto which we've regiestered our new meta boxes and are now calling them in the `edit_form_after_title` hook which is run after the post tile box is displayed.
function oz_run_after_title_meta_boxes() {
global $post, $wp_meta_boxes;
# Output the `below_title` meta boxes:
do_meta_boxes( get_current_screen(), 'after_title', $post );
}
add_action( 'edit_form_after_title', 'oz_run_after_title_meta_boxes' );
// ACF Functions
/*
Add a 'Links Only' toolbar style for the following fields:
- Text/Image Block: text_caption
- Site-wide Settings: site_callout_text, site_footer_credits
Credit: http://www.advancedcustomfields.com/resources/customize-the-wysiwyg-toolbars/
*/
function sd_acf_wysiwyg_toolbar( $toolbars ) {
$toolbars['Links Only'] = array();
$toolbars['Text Based'] = array();
// Only one row of buttons
$toolbars['Links Only'][1] = array('link', 'unlink' );
$toolbars['Text Based'][1] = array('formatselect', 'bold', 'italic', 'link', 'unlink', 'bullist', 'numlist' );
return $toolbars;
}
add_filter( 'acf/fields/wysiwyg/toolbars' , 'sd_acf_wysiwyg_toolbar' );
// Add stylesheet with custom ACF styles
function sd_acf_admin_head() {
?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/assets/css/acf.css">
<?php
}
add_action('acf/input/admin_head', 'sd_acf_admin_head');