-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
336 lines (283 loc) · 10.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
<?php
if(file_exists(get_template_directory() . '/extensions/cmb2/init.php')){
require_once get_template_directory() . '/extensions/cmb2/init.php';
}
if(file_exists(get_template_directory() . '/extensions/tgm/tgm-configuration.php')){
require_once get_template_directory() . '/extensions/tgm/tgm-configuration.php';
}
add_theme_support('post-thumbnails');
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) );
add_theme_support( 'automatic-feed-links' );
function vhr_scripts_load(){
wp_enqueue_script('jquery');
wp_enqueue_script('main', get_template_directory_uri() . '/assets/js/main.js');
wp_enqueue_style('style', get_bloginfo('stylesheet_url'));
wp_enqueue_style('normalize', get_template_directory_uri() . '/assets/css/normalize.css');
wp_enqueue_style('skeleton', get_template_directory_uri() . '/assets/css/skeleton.css');
wp_enqueue_style('font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.min.css');
if ( is_singular() && get_option( 'thread_comments' ) ){
wp_enqueue_script( 'comment-reply', '', '', array(), true );
}
}
add_action('wp_enqueue_scripts', 'vhr_scripts_load');
function vhr_register_menu(){
register_nav_menus( array(
'topo' => 'Menu do topo'
) );
}
add_action( 'init', 'vhr_register_menu' );
add_filter('document_title_separator', 'poxanine_blog_title_separator');
function poxanine_blog_title_separator($sep) {
if (is_home()) {
$sep = "";
}
return $sep;
}
function vhr_paginate(){
global $wp_query;
$current = max( 1, absint( get_query_var( 'paged' ) ) );
$pagination = paginate_links( array(
'base' => str_replace( PHP_INT_MAX, '%#%', esc_url( get_pagenum_link( PHP_INT_MAX ) ) ),
'format' => '?paged=%#%',
'current' => $current,
'total' => $wp_query->max_num_pages,
'type' => 'array',
'prev_text' => '«',
'next_text' => '»',
) );
if ( ! empty( $pagination ) ) : ?>
<div class="pagination">
<ul class="pagination-list">
<?php foreach ( $pagination as $key => $page_link ) : ?>
<?php
if ( strpos( $page_link, 'current' ) !== false ) :
?>
<li class="pagination-item">
<a href="<?php echo $_SERVER['REQUEST_URI']; ?>" class="pagination-link active"><?php echo $current; ?></a>
</li>
<?php else:
$dom = new DomDocument();
$dom->loadHTML($page_link);
$urls = $dom->getElementsByTagName('a');
$links = array();
foreach($urls as $link) {
$links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}
?>
<li class="pagination-item">
<a href="<?php echo ($links[0]['text'] != '') ? $links[0]['url'] : 'javascript:void(0);'; ?>" class="pagination-link<?php if ( strpos( $page_link, 'current' ) !== false ) { echo ' active'; } ?>"><?php echo ($links[0]['text'] != '') ? $links[0]['text'] : '...'; ?></a>
</li>
<?php endif; ?>
<?php endforeach ?>
</ul>
</div>
<?php endif;
}
/**
* Add a sidebar.
*/
function vhr_theme_slug_widgets_init() {
register_sidebar( array(
'name' => 'Sidebar Lateral',
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
) );
register_sidebar( array(
'name' => 'Instagram Footer',
'id' => 'instagram-footer',
'description' => 'Show instagram in footer',
'before_widget' => '<div id="%1$s" class="instagram-footer %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
) );
}
add_action( 'widgets_init', 'vhr_theme_slug_widgets_init' );
function vhr_comment_list($comment, $args, $depth){
?>
<li <?php comment_class() ?> id="div-comment-<?php comment_ID(); ?>">
<div class="comment-item">
<div class="author-img">
<?php echo get_avatar( get_comment_author_email(), 50) ?>
</div>
<div class="comment-text">
<span class="reply"><?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?></span>
<span class="author"><?php echo get_comment_author() ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
- <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></em>
<?php endif; ?>
</span>
<span class="date"><?php comment_date() ?></span>
<?php comment_text(); ?>
</div>
</div>
</li>
<?php
}
add_action('pre_get_posts', 'vhr_query_offset', 1 );
function vhr_query_offset(&$query) {
if(! $query->is_main_query()){
return;
}
if ( ! $query->is_archive() && ! $query->is_home() ) {
return;
}
$fp = 5;
$ppp = 8;
if ( $query->is_paged ) {
$offset = $fp + ( ($query->query_vars['paged'] - 2) * $ppp );
$query->set('offset', $offset );
$query->set('posts_per_page', $ppp );
} else {
$query->set('posts_per_page', $fp );
}
}
add_filter('found_posts', 'vhr_adjust_offset_pagination', 1, 2 );
function vhr_adjust_offset_pagination($found_posts, $query) {
$fp = 5;
$ppp = 8;
if ( $query->is_home() && $query->is_main_query() || $query->is_archive() && $query->is_main_query() ) {
if ( $query->is_paged ) {
return ( $found_posts + ( $ppp - $fp ) );
}
}
return $found_posts;
}
function vhr_excerpt_more($more) {
global $post;
return ' ...';
}
add_filter('excerpt_more', 'vhr_excerpt_more');
function vhr_related_posts(){
global $post;
$tags = wp_get_post_tags($post->ID);
if($tags){
$tags_ids = array();
foreach($tags as $individual_tags){
$tags_ids[] = $individual_tags->term_id;
}
$args = array(
'tag__in' => $tags_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 3,
'orderby' => 'rand',
'ignore_sticky_posts' => 0
);
$my_related = new WP_Query($args);
if($my_related->found_posts > 0){
?>
<div class="post-related">
<div class="post-box">
<h4 class="post-box-title">Você também pode gostar</h4>
</div>
<?php
}
while($my_related->have_posts()){
$my_related->the_post();
?>
<div class="item-related">
<?php if(has_post_thumbnail()): ?>
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail( 'full' ) ?>
</a>
<?php endif; ?>
<h3><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h3>
<span class="date"><?php the_date() ?></span>
</div>
<?php
}
wp_reset_query();
echo '</div>';
}
}
function vhr_post_author(){
global $post;
if(is_page()){
return $post;
}
?>
<div class="post-author">
<div class="author-img">
<?php echo get_avatar($post->post_author) ?>
</div>
<div class="author-content">
<h5><?php the_author_link() ?></h5>
<p><?php echo get_the_author_meta('user_description', $post->post_author) ?></p>
</div>
</div>
<?php
}
function vhr_share_links(){
global $post;
$title = urlencode(get_the_title($post->ID));
?>
<span class="share-text">Compartilhe:</span>
<div class="fb-like" data-href="<?=get_permalink($post->ID)?>" data-layout="button" data-action="like" data-size="small" data-show-faces="false"></div>
<a href="https://twitter.com/share" class="twitter-share-button">Tweet</a>
<?php
}
function vhr_post_tags(){
global $post;
$tags = wp_get_post_tags($post->ID);
if($tags){
?>
<div class="post-tags">
<?php the_tags( 'Tags: ', $sep = ', ', $after = '' ) ?>
</div>
<?php
}
}
//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
}
add_filter('language_attributes', 'add_opengraph_doctype');
//Lets add Open Graph Meta Info
function insert_fb_in_head() {
global $post;
if ( !is_singular() ) //if it is not a post or a page
return;
echo '<meta property="fb:admins" content="100000514697944"/>';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:site_name" content="' . get_bloginfo("name") . '"/>';
echo '<meta property="og:description" content="' . get_the_excerpt() . '" />';
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
$default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library
echo '<meta property="og:image" content="' . $default_image . '"/>';
} else {
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
}
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );
function fb_like_button_footer(){
?>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/pt_BR/sdk.js#xfbml=1&version=v2.9&appId=1475751812723202";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<?php
}
add_action('wp_footer', 'fb_like_button_footer');
function twitter_js_async(){
?>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<?php
}
add_action('wp_footer', 'twitter_js_async');
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}