forked from mayeenulislam/WP-CodeSpring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
248 lines (212 loc) · 7.53 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
<?php
/**
* WordPress Functions
* @author: Mayeenul Islam (@mayeenulislam)
*
* This function page can work under any WordPress theme
* The code here, are collected from various sources
* where the code is collected from somewhere, is mentioned as a comment
* where it's solely of Mayeenul Islam, is also mentioned as a comment
*/
/**
* CHANGE THE 'WPLANG' IN wp-config.php TO bn_BD ON THEME SWITCHING
* Thanks: toscho
* Source: http://wordpress.stackexchange.com/a/121136/22728
*/
add_filter( 'locale', 'toscho_change_language' );
function toscho_change_language( $locale ) {
return 'bn_BD';
}
/**
* TRUNCK STRING TO SHORTEN THE STRING
*
* the code block was collected from Ms. Tahmina Aktar (PHP and WordPress developer)
* The function shorten any string limiting with the parameter passed
* through the funciton
*
* Usage:
* <?php
* $string = "I'm Mayeenul Islam";
* $truncked_string = trunck_string( $string, 4, true ); ?>
*/
function trunck_string($str = "", $len = 150, $more = 'true') {
if ($str == "") return $str;
if (is_array($str)) return $str;
$str = strip_tags($str);
$str = trim($str);
// if it's les than the size given, then return it
if (strlen($str) <= $len) return $str;
// else get that size of text
$str = substr($str, 0, $len);
// backtrack to the end of a word
if ($str != "") {
// check to see if there are any spaces left
if (!substr_count($str , " ")) {
if ($more == 'true') $str .= "...";
return $str;
}
// backtrack
while(strlen($str) && ($str[strlen($str)-1] != " ")) {
$str = substr($str, 0, -1);
}
$str = substr($str, 0, -1);
if ($more == 'true') $str .= "...";
if ($more != 'true' and $more != 'false') $str .= $more;
}
return $str;
}
/**
* ENABLING ARROW ON PARENT MENU
*
* This code snippet adds an arrow to the parent menu item
* where there is/are sub menu[s]
*
* Source: http://stackoverflow.com/a/3594567/1743124
*
* Usage:
* With the general register_nav_menus() function this function needs to be in functions.php
* Then in the template's wp_nav_menu() function, add the 'walker' parameter
* <?php wp_nav_menu( array( 'walker' => new arrow_walker_nav_menu, 'theme_location' => 'menu_slug' ) ); ?>
*/
class arrow_walker_nav_menu extends walker_nav_menu {
function display_element($element, &$children_elements, $max_depth, $depth=0, $args, &$output) {
$id_field = $this->db_fields['id'];
if (!empty($children_elements[$element->$id_field])) {
$element->classes[] = 'arrow'; //CSS classname here
$element->title .= '<span class="caret right-caret">»</span>'; //append html here
}
walker_nav_menu::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
}
}
/**
* GRAB THE FIRST IMAGE FROM THE POST CONTENT
*
* This function will grab the first image from the post
* searching the <img> tags inside the post content
*
* Source: WordPress.StackExchange.com - Forgotten
*/
function grab_first_image() {
global $post;
$first_image = '';
ob_start();
ob_end_clean();
if( $the_output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches) )
$first_image = $matches[1][0];
return $first_image;
}
/**
* REMOVE UNNECESSARY HEADER INFO
*
* This function will remove unnecessary information from the <head> tag
* to make the site's head more clean and make the site more speedy
*
* Source: WordPress.StackExchange.com - Forgotten
*/
function remove_header_info() {
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link'); // for WordPress < 3.0
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); // for WordPress >= 3.0
}
add_action('init', 'remove_header_info');
/**
* THE EXCERPT FILTERS
*
* This group of functions will make a new controllable excerpt
* called nano_excerpt()
*
* Source: Codex - http://codex.wordpress.org/Function_Reference/the_excerpt
*/
function nano_excerpt( $limit = 75 ) {
$limited_excerpts = wp_trim_words( get_the_excerpt(), $limit, new_excerpt_more() );
echo $limited_excerpts;
return $limited_excerpts;
}
function new_excerpt_more() {
return '... <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">'. __( '»', 'your-theme' ) .'</a>';
}
function custom_excerpt_length( $length ) {
return 200;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/**
* Alternative to get_current_screen()
* @link http://wordpress.stackexchange.com/a/174596/22728
*
* @param string $base
* @param string $post_type
* @return bool
*/
function theme_is_current_screen( $base = null, $post_type = null ) {
if ( ! $base && ! $post_type ) {
return false;
}
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen ) {
// Fake it.
$screen = new StdClass;
$screen->post_type = $screen->base = '';
global $pagenow;
if ( $pagenow == 'admin-ajax.php' ) {
if ( isset( $_REQUEST['action'] ) ) {
$screen->base = $_REQUEST['action'];
}
} else {
$screen->post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : '';
if ( $pagenow == 'post.php' || $pagenow == 'post-new.php' || $pagenow == 'edit.php' ) {
$screen->base = preg_replace( '/[^a-z].+$/', '', $pagenow );
if ( ! $screen->post_type ) {
$screen->post_type = get_post_type( theme_get_post_id() );
}
} else {
$page_hook = '';
global $plugin_page;
if ( ! empty( $plugin_page ) ) {
if ( $screen->post_type ) {
$the_parent = $pagenow . '?post_type=' . $screen->post_type;
} else {
$the_parent = $pagenow;
}
if ( ! ( $page_hook = get_plugin_page_hook( $plugin_page, $the_parent ) ) ) {
$page_hook = get_plugin_page_hook( $plugin_page, $plugin_page );
}
}
$screen->base = $page_hook ? $page_hook : pathinfo( $pagenow, PATHINFO_FILENAME );
}
}
}
// The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped.
if ( $base ) {
if ( ! is_array( $base ) ) $base = array( $base );
if ( ! in_array( $screen->base, $base ) ) {
return false;
}
}
if ( $post_type ) {
if ( ! is_array( $post_type ) ) $post_type = array( $post_type );
if ( ! in_array( $screen->post_type, $post_type ) ) {
return false;
}
}
return true;
}
/*
* Attempt to determine post id in uncertain (admin) situations.
* Based on WPAlchemy_MetaBox::_get_post_id().
*/
function theme_get_post_id() {
global $post;
$ret = 0;
if ( ! empty( $post->ID ) ) {
$ret = $post->ID;
} elseif ( ! empty( $_GET['post'] ) && ctype_digit( $_GET['post'] ) ) {
$ret = $_GET['post'];
} elseif ( ! empty( $_POST['post_ID'] ) && ctype_digit( $_POST['post_ID'] ) ) {
$ret = $_POST['post_ID'];
}
return $ret;
}