In wordpress theming is very important. Developers know the importance of functions.php file. Here we given most used wordpress functions in theme which will be useful for wordpress developer. I always written some very nice code snippets in functions.php file.
most used wordpress functions in theme
I found very useful codes which is very helpful for very wordpress designer and developers.
Here is very useful code snippets.
Enable Hidden Admin Feature displaying ALL Site Settings
2
function
all_settings_link() {
3
add_options_page(__(
'All Settings'
), __(
'All Settings'
),
'administrator'
,
'options.php'
);
5
add_action(
'admin_menu'
,
'all_settings_link'
);
Remove Update Notification for all users except ADMIN User
4
if
(!current_user_can(
'update_plugins'
)) {
5
add_action(
'init'
, create_function(
'$a'
,
"remove_action( 'init', 'wp_version_check' );"
), 2 );
6
add_filter(
'pre_option_update_core'
, create_function(
'$a'
,
"return null;"
) );
Include custom post types in the search results.
2
function
searchAll(
$query
) {
3
if
(
$query
->is_search ) {
$query
->set(
'post_type'
,
array
(
'site'
,
'plugin'
,
'theme'
,
'person'
)); }
6
add_filter(
'the_search_query'
,
'searchAll'
);
Add your custom post types to your sites main RSS feed by default.
2
function
custom_feed_request(
$vars
) {
3
if
(isset(
$vars
[
'feed'
]) && !isset(
$vars
[
'post_type'
]))
4
$vars
[
'post_type'
] =
array
(
'post'
,
'site'
,
'plugin'
,
'theme'
,
'person'
);
7
add_filter(
'request'
,
'custom_feed_request'
);
Modify the Login Logo & Image URL Link
01
add_filter(
'login_headerurl'
,
'namespace_login_headerurl'
);
07
function
namespace_login_headerurl(
$url
) {
08
$url
= home_url(
'/'
);
12
add_filter(
'login_headertitle'
,
'namespace_login_headertitle'
);
18
function
namespace_login_headertitle(
$title
) {
19
$title
= get_bloginfo(
'name'
);
23
add_action(
'login_head'
,
'namespace_login_style'
);
27
function
namespace_login_style() {
28
echo
'<style>.login h1 a { background-image: url( '
. get_template_directory_uri() .
'/images/logo.png ) !important; }</style>'
;
Loading jQuery from the Google CDN
02
add_action(
'init'
,
'jquery_register'
);
05
function
jquery_register() {
09
wp_deregister_script(
'jquery'
);
10
wp_register_script(
'jquery'
, (
'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
), false, null, true );
11
wp_enqueue_script(
'jquery'
);
Remove the WordPress Version Info for Security
2
function
complete_version_removal() {
5
add_filter(
'the_generator'
,
'complete_version_removal'
);
Add Spam & Delete Links to Comments on Front End
2
function
delete_comment_link(
$id
) {
3
if
(current_user_can(
'edit_post'
)) {
4
echo
'| <a href="'
.get_bloginfo(
'wpurl'
).
'/wp-admin/comment.php?action=cdc&c='
.
$id
.
'">del</a> '
;
5
echo
'| <a href="'
.get_bloginfo(
'wpurl'
).
'/wp-admin/comment.php?action=cdc&dt=spam&c='
.
$id
.
'">spam</a>'
;
Remove Default WordPress Meta Boxes
02
function
remove_default_post_screen_metaboxes() {
03
remove_meta_box(
'postcustom'
,
'post'
,
'normal'
);
04
remove_meta_box(
'postexcerpt'
,
'post'
,
'normal'
);
05
remove_meta_box(
'commentstatusdiv'
,
'post'
,
'normal'
);
06
remove_meta_box(
'trackbacksdiv'
,
'post'
,
'normal'
);
07
remove_meta_box(
'slugdiv'
,
'post'
,
'normal'
);
08
remove_meta_box(
'authordiv'
,
'post'
,
'normal'
);
10
add_action(
'admin_menu'
,
'remove_default_post_screen_metaboxes'
);
13
function
remove_default_page_screen_metaboxes() {
14
remove_meta_box(
'postcustom'
,
'page'
,
'normal'
);
15
remove_meta_box(
'postexcerpt'
,
'page'
,
'normal'
);
16
remove_meta_box(
'commentstatusdiv'
,
'page'
,
'normal'
);
17
remove_meta_box(
'trackbacksdiv'
,
'page'
,
'normal'
);
18
remove_meta_box(
'slugdiv'
,
'page'
,
'normal'
);
19
remove_meta_box(
'authordiv'
,
'page'
,
'normal'
);
21
add_action(
'admin_menu'
,
'remove_default_page_screen_metaboxes'
);
Add Custom User Profile Fields
02
function
my_custom_userfields(
$contactmethods
) {
05
$contactmethods
[
'contact_phone_office'
] =
'Office Phone'
;
06
$contactmethods
[
'contact_phone_mobile'
] =
'Mobile Phone'
;
07
$contactmethods
[
'contact_office_fax'
] =
'Office Fax'
;
10
$contactmethods
[
'address_line_1'
] =
'Address Line 1'
;
11
$contactmethods
[
'address_line_2'
] =
'Address Line 2 (optional)'
;
12
$contactmethods
[
'address_city'
] =
'City'
;
13
$contactmethods
[
'address_state'
] =
'State'
;
14
$contactmethods
[
'address_zipcode'
] =
'Zipcode'
;
15
return
$contactmethods
;
17
add_filter(
'user_contactmethods'
,
'my_custom_userfields'
,10,1);
Add an excerpt box for pages
1
if
( function_exists(
'add_post_type_support'
) )
3
add_action(
'init'
,
'add_page_excerpts'
);
4
function
add_page_excerpts()
6
add_post_type_support(
'page'
,
'excerpt'
);
Function to change the length of Exerpt
1
function
new_excerpt_length(
$length
) {
5
add_filter(
'excerpt_length'
,
'new_excerpt_length'
);
Auto Extract the First Image from the Post Content
01
/ AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
02
function
getImage(
$num
) {
05
$link
= get_permalink();
06
$content
= get_the_content();
07
$count
= substr_count(
$content
,
'<img'
);
09
for
(
$i
=1;
$i
<=
$count
;
$i
++) {
10
$imgBeg
=
strpos
(
$content
,
'<img'
,
$start
);
11
$post
=
substr
(
$content
,
$imgBeg
);
12
$imgEnd
=
strpos
(
$post
,
'>'
);
13
$postOutput
=
substr
(
$post
, 0,
$imgEnd
+1);
14
$postOutput
= preg_replace(
'/width="([0-9]*)" height="([0-9]*)"/'
,
''
,
$postOutput
);;
15
$image
[
$i
] =
$postOutput
;
18
if
(
stristr
(
$image
[
$num
],
'<img'
)) {
echo
'<a href="'
.
$link
.
'">'
.
$image
[
$num
].
"</a>"
; }
Unregister WP Default Widgets
02
function
unregister_default_wp_widgets() {
03
unregister_widget(
'WP_Widget_Pages'
);
04
unregister_widget(
'WP_Widget_Calendar'
);
05
unregister_widget(
'WP_Widget_Archives'
);
06
unregister_widget(
'WP_Widget_Links'
);
07
unregister_widget(
'WP_Widget_Meta'
);
08
unregister_widget(
'WP_Widget_Search'
);
09
unregister_widget(
'WP_Widget_Text'
);
10
unregister_widget(
'WP_Widget_Categories'
);
11
unregister_widget(
'WP_Widget_Recent_Posts'
);
12
unregister_widget(
'WP_Widget_Recent_Comments'
);
13
unregister_widget(
'WP_Widget_RSS'
);
14
unregister_widget(
'WP_Widget_Tag_Cloud'
);
16
add_action(
'widgets_init'
,
'unregister_default_wp_widgets'
, 1);
Enable GZIP output compression
1
if
(
extension_loaded
(
"zlib"
) && (
ini_get
(
"output_handler"
) !=
"ob_gzhandler"
))
2
add_action(
'wp'
, create_function(
''
,
'@ob_end_clean();@ini_set("zlib.output_compression", 1);'
));
Enable shortcodes in widgets
3
add_filter(
'widget_text'
,
'do_shortcode'
, 11);
If you have any interesting code snippets then please suggest me.
most used wordpress functions in theme