-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
250 lines (209 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
<?php
/**
* Timber starter-theme
* https://github.com/timber/starter-theme
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/
/**
* If you are installing Timber as a Composer dependency in your theme, you'll need this block
* to load your dependencies and initialize Timber. If you are using Timber via the WordPress.org
* plug-in, you can safely delete this block.
*/
/*$composer_autoload = dirname($_SERVER['DOCUMENT_ROOT']).'/vendor/autoload.php';
if ( file_exists($composer_autoload) ) {
require_once( $composer_autoload );
$timber = new Timber\Timber();
}
*/
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
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>';
});
add_filter('template_include', function( $template ) {
return get_stylesheet_directory() . '/static/no-timber.html';
});
return;
}
/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = array( 'templates', 'views' );
/**
* By default, Timber does NOT autoescape values. Want to enable Twig's autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;
/**
* We're going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php's include("MySite.php")
*/
class Frugal extends Timber\Site {
/** Add timber support. */
public function __construct() {
add_action( 'after_setup_theme', array( $this, 'theme_supports' ) );
add_filter( 'timber/context', array( $this, 'add_to_context' ) );
add_filter( 'timber/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, 'unbloat' ) );
add_action( 'widgets_init', array( $this, 'frugal_widgets_init' ) );
add_action( 'init', array( $this, 'custom_post_type') );
add_filter( 'get_search_form', array( $this, 'frugal_search_form') );
add_filter('upload_mimes', array( $this, 'cc_mime_types') );
add_filter( 'wpseo_sitemap_entries_per_page', array( $this, 'max_entries_per_sitemap' ) );
add_filter( 'post_thumbnail_html', array( $this, 'remove_width_attribute'));
add_filter( 'image_send_to_editor', array( $this, 'remove_width_attribute') );
add_filter( 'post_thumbnail_html', array( $this, 'remove_thumbnail_dimensions') );
//add_action( 'wp_enqueue_scripts', array( $this, 'scripts') );
parent::__construct();
}
/** This is where you can register custom post types. */
public function register_post_types() {
}
/** This is where you can register custom taxonomies. */
public function register_taxonomies() {
}
public function unbloat() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
}
/**
* Register our sidebars and widgetized areas.
*
*/
public function frugal_widgets_init() {
register_sidebar(
array(
'name' => 'Sidebar',
'id' => 'sidebar',
'before_title' => '<h2>',
'after_title' => '</h2>',
)
);
register_sidebar(
array(
'name' => 'Navbar',
'id' => 'navbar',
)
);
register_sidebar(
array(
'name' => 'All Posts Page',
'id' => 'allpostspage',
)
);
register_sidebar(
array(
'name' => 'Footer',
'id' => 'footer',
)
);
}
public function frugal_search_form( $form ) {
$form = '<form role="search" method="get" id="searchform" class="frugal-search" action="' . home_url( '/' ) . '" >
<input type="search" class="search-input" aria-label="Search" placeholder="Search" value="' . get_search_query() . '" name="s" id="s" />
<button type="submit" class="search-submit" id="searchsubmit"><ion-icon name="search"></ion-icon></button>
</form>';
return $form;
}
public function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
public function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
/** This is where you add some context
*
* @param string $context context['this'] Being the Twig's {{ this }}.
*/
public function add_to_context( $context ) {
$context['foo'] = 'bar';
$context['stuff'] = 'I am a value set in your functions.php file';
$context['notes'] = 'These values are available everytime you call Timber::context();';
$context['menu'] = new Timber\Menu();
$context['site'] = $this;
return $context;
}
public function theme_supports() {
// Add default posts and comments RSS feed links to head.
//add_theme_support( 'automatic-feed-links' );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5', array(
'comment-form',
'comment-list',
'gallery',
'caption',
)
);
/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
'post-formats', array(
'aside',
'image',
'video',
'quote',
'link',
'gallery',
'audio',
)
);
add_theme_support( 'menus' );
}
public function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
public function max_entries_per_sitemap() {
return 10000;
}
/** This Would return 'foo bar!'.
*
* @param string $text being 'foo', then returned 'foo bar!'.
*/
public function myfoo( $text ) {
$text .= ' bar!';
return $text;
}
/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig( $twig ) {
$twig->addExtension( new Twig_Extension_StringLoader() );
$twig->addFilter( new Twig_SimpleFilter( 'myfoo', array( $this, 'myfoo' ) ) );
return $twig;
}
}
new Frugal();