-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
281 lines (250 loc) · 9.72 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
<?php
/**
* pw functions and definitions.
*
* Sets up the theme and provides some helper functions, which are used
* in the theme as custom template tags. Others are attached to action and
* filter hooks in WordPress to change core functionality.
*
* When using a child theme (see http://codex.wordpress.org/Theme_Development and
* http://codex.wordpress.org/Child_Themes), you can override certain functions
* (those wrapped in a function_exists() call) by defining them first in your child theme's
* functions.php file. The child theme's functions.php file is included before the parent
* theme's file, so the child theme functions would be used.
*
* Functions that are not pluggable (not wrapped in function_exists()) are instead attached
* to a filter or action hook.
*
* For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
*
* @package WordPress
* @subpackage pw
* @since pw 1.0
*/
if ( function_exists('register_sidebar') ) register_sidebar(array(
'name' => __( 'Header Sidebar', 'pw' ),
'id' => 'header-sidebar',
'description' => __( 'Widgets in this area will be shown on the header.', 'pw' )
));
if ( ! isset( $content_width ) ) $content_width = 1100;
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'post-thumbnails' );
add_editor_style();
add_theme_support( 'custom-background', array(
'default-color' => 'e6e6e6',
) );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
require( get_template_directory() . '/inc/custom-header.php' );
/**
* Customized the_excerpt()
*/
function new_excerpt_more( $more ) {
return ' ...';
}
add_filter('excerpt_more', 'new_excerpt_more');
function custom_excerpt_length($length) {
return 30;
}
add_filter('excerpt_length', 'custom_excerpt_length');
/**
* Function to manipulate dates
*/
function pw_date() {
global $post;
return '<time datetime="'.get_the_time('Y-m-d').'" class="updated">'.get_the_time('d').' de <span>'.get_the_time('F').' de '.get_the_time('Y').'</span></time>'.PHP_EOL;
}
/**
* overwrite comments_popup_link
*/
function add_nofollow_to_comments_popup_link() {
return ' rel="nofollow" ';
}
add_filter( 'comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link' );
/**
* overwrite wp_list_categories
*/
function new_wp_list_categories($more) {
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
$li = '';
foreach($categories as $category)
{
$li.= '<li><a href="'.get_category_link( $category->term_id ).'" title="'.$category->name.' '.$category->description.'">'.$category->name.'</a></li> ';
}
return $li;
}
add_filter('wp_list_categories', 'new_wp_list_categories');
function replace_cat_tag ( $text ) {
$text = str_replace(array('rel="category"','rel="category tag"'), "", $text); return $text;
}
add_filter( 'the_category', 'replace_cat_tag' );
/**
* overwrite wp_list_categories
*/
add_filter('next_posts_link_attributes', 'get_next_posts_link_attributes');
add_filter('previous_posts_link_attributes', 'get_previous_posts_link_attributes');
if (!function_exists('get_next_posts_link_attributes')){
function get_next_posts_link_attributes($attr){
$attr = 'rel="next" title="Posts mais antigos"';
return $attr;
}
}
if (!function_exists('get_previous_posts_link_attributes')){
function get_previous_posts_link_attributes($attr){
$attr = 'rel="prev" title="Posts mais recentes"';
return $attr;
}
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'featured-thumb', 460, 190, true );
add_image_size( 'list-thumb', 300, 125, true );
}
/**
* Add a rel="nofollow" to the comment reply links
*/
function add_nofollow_to_reply_link( $link ) {
return str_replace( '")\'>', '")\' rel=\'nofollow\'>', $link );
}
add_filter( 'comment_reply_link', 'add_nofollow_to_reply_link' );
if ( ! function_exists( 'parent_comment' ) ) :
/**
* Template for comments and pingbacks.
*
* To override this walker in a child theme without modifying the comments template
* simply create your own twentyten_comment(), and that function will be used instead.
*
* Used as a callback by wp_list_comments() for displaying the comments.
*
* @since pw 0.0.1
*/
function parent_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>"> <?php echo get_avatar( $comment, 32 ); ?>
<cite><?php comment_text() ?></cite>
<?php _e('por', 'pw'); ?>
<?php comment_author_link() ?>
—
<time datetime="<?php echo $comment->comment_date; ?>">
<?php comment_date() ?>
@ <a href="#comment-<?php comment_ID() ?>">
<?php comment_time() ?>
</a>
</time>
<?php edit_comment_link(__('Editar', 'pw'), ' | '); ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php _e( 'Seu comentário está aguardando revisão', 'pw' ); ?></em>
<?php endif; ?>
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</li>
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'pw' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Editar)', 'pw' ), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img = get_template_directory_uri()."/images/img-post-default.png";
}
return $first_img;
}
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 20; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
return $the_excerpt;
}
/**
* Shortcodes.
*
* @since pw 0.0.1
*/
function sourcecode ($atts, $content = null) {
extract( shortcode_atts( array(
'language' => ''
), $atts ));
$content = str_replace(Array('“', '”'), '"', $content);
return '<pre class="'.$language.'">'.$content.'</pre>';
}
add_shortcode('sourcecode', 'sourcecode');
add_shortcode('source', 'sourcecode');
/**
* Don't auto-p wrap shortcodes that stand alone
*
* Ensures that shortcodes are not wrapped in <<p>>...<</p>>.
*
* @since 2.9.0
*
* @param string $pee The content.
* @return string The filtered content.
*/
function shortcode_unauto( $pee ) {
global $shortcode_tags;
if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
return $pee;
}
$tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
$pattern =
'/'
. '<p>' // Opening paragraph
. '\\s*+' // Optional leading whitespace
. '(' // 1: The shortcode
. '\\[' // Opening bracket
. "($tagregexp)" // 2: Shortcode name
. '(?![\\w-])' // Not followed by word character or hyphen
// Unroll the loop: Inside the opening shortcode tag
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. '(?:'
. '\\/(?!\\])' // A forward slash not followed by a closing bracket
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. ')*?'
. '(?:'
. '\\/\\]' // Self closing tag and closing bracket
. '|'
. '\\]' // Closing bracket
. '(?:' // Unroll the loop: Optionally, anything between the opening and closing shortcode tags
. '[^\\[]*+' // Not an opening bracket
. '(?:'
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
. '[^\\[]*+' // Not an opening bracket
. ')*+'
. '\\[\\/\\2\\]' // Closing shortcode tag
. ')?'
. ')'
. ')'
. '\\s*+' // optional trailing whitespace
. '<\\/p>' // closing paragraph
. '/s';
return preg_replace( $pattern, '$1', $pee );
}
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 99);
add_filter( 'the_content', 'shortcode_unauto',100 );
?>