10 Useful Custom Functions For WordPress Themes


One of a many critical and essential record in WordPress Theme is a record name called functions.php. This record is useful for adding tradition functions and filter to overwrite a WordPress core functions or adding a new functions that wasn’t existed in core function.

In this article, i will share some useful tradition functions we can supplement into your WordPress thesis functions.php file.

1. Add Browser Detection in physique class

Here’s dash php formula we can supplement into your functions.php to supplement a browser physique difficulty in your WordPress Theme.

if( !function_exists( 'get_browser_body_class' )):
// Browser Detect
function get_browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';
if($is_iphone) $classes[] = 'iphone';
return $classes;

Your WordPress Theme contingency have next formula in physique for this to work.

body ?php body_class(); ?

End Results
Now your WordPress Theme will detect that browser your caller regulating so if your visitors are regulating chrome. it will relate physique difficulty like this

body class="chrome"

What a functions can do?
You can supplement a primary css difficulty for browser condition like if visitors are in internet path-finder or mozilla firefox, we can supplement this to your stylesheet (style.css)

body.ie div.my-container { width: auto; height:auto; }
body.gecko div.my-container { width: auto; height:auto; }

for me, i used a body.safari to change a nauseous helvetica fonts display in safari browser.

/* default - physique { font-family: helvetica, arial; } //lets use arial for safari browser */
body.safari { font-family: arial, sans-serif; }
2. Disable Adminbar for Normal Users

Sometime when we are regulating a membership site, we cite not to uncover a adminbar for logged-in user though in a other palm we wish a admins and judge to be means to use a adminbar, here’s a dash php formula we can supplement into your wordpress thesis functions.php.

// uncover admin bar usually for admins
if (!current_user_can('manage_options')) {
	add_filter('show_admin_bar', '__return_false');
// uncover admin bar usually for admins and editors
if (!current_user_can('edit_posts')) {
	add_filter('show_admin_bar', '__return_false');

End Results
Now users that had a ability to ‘manage_options’ and ‘edit_posts’ (mostly admin and editor will have this priviliage) will had a adminbar when they logged in.

3. Post Thumbnail Check for Posts

Using or saving featured post thumbnail to any posts had been a regularality for blog writers though in some cases we wish to request a css difficulty to a post that do not have post thumbnail trustworthy into posts. Here’s a php dash we can use.

if( !function_exists( 'get_has_thumb_class' )):
// Check if post has thumbnail attached
function get_has_thumb_class($classes) {
global $post;

if(  has_post_thumbnail($post-ID) ) {
$classes[] = 'has_thumb';
} else {
$classes[] = 'has_no_thumb';

return $classes;
add_filter('post_class', 'get_has_thumb_class');

Your WordPress Theme contingency have a next formula for this to work.

?php post_class(); ?

End Results
Now your WordPress Theme will detect if your posts have a thumbnail trustworthy or not.

/* if have thumbnail - essay ?php post_class(); ?*/
article class="has_thumb post post-99"
/* if do not have thumbnail */
article class="has_no_thumb post post-99"
4. Add Custom Field to Profile

Need to supplement some-more options for a user profiles, try adding this to functions.php

if( !function_exists( 'wp_add_new_profile_meta' )):
// Add new form meta
function wp_add_new_profile_meta( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
//add Goolge+ Profile link
$contactmethods['google_plus'] = 'Google Plus';

return $contactmethods;

End Results
Now if we navigate to wp-admin/profile.php, we will see an additional boxes to enter your Twitter, Facebook and Google Plus form links.


How to use a code?
You can elementary supplement this formula inside author.php

?php if(isset($_GET['author_name'])) : 
$curauth = get_userdatabylogin($author_name); 
else : 
$curauth = get_userdata(intval($author)); 
endif; ?
?php relate $curauth-twitter; //twitter, facebook or google_plus ?
5. Add Post View Counter

If we wish to build adult renouned posts, we can supplement this formula to your functions.php. This formula will collect how many times a posts had been noticed and save it on any post post view meta pivotal and meta value.

if( !function_exists('get_wp_post_view') ) :
// get post perspective count
function get_wp_post_view($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        lapse "0";
    lapse $count;
function set_wp_post_view($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        update_post_meta($postID, $count_key, $count);

After adding a above formula to functions.php, we need to open single.php or single-TAXONOMY.php and supplement this formula to before finish of ?php endwhile; ? like this

?php set_wp_post_view( get_the_ID() ); ?
?php endwhile; ?

How To Get The Most Viewed Posts?
If we need to get a many noticed posts, usually supplement this to anywhere in your themes.

$my_query = new WP_Query( array(  'post_type'= 'post', 'meta_key' = 'post_views_count', 'orderby' = 'meta_value_num', 'order' = 'DESC','posts_per_page' = 10 ) );
ul class="most-viewed"
?php while ($my_query-have_posts()) : $my_query-the_post(); ?
a title="?php relate the_title(); ?" href="?php relate get_permalink( get_the_ID() ); ?"?php relate the_title(); ?/a - small
?php relate get_post_meta( get_the_ID() , 'post_views_count', true);; ? Viewed/small
?php endwhile; wp_reset_query(); ?
6. Add difficulty to initial post in a loop

If we wanted to supplement importance to a initial post in your blog adding this dash to a functions.php of your wordpress thesis will supplement a difficulty of “first” to your initial post.

add_filter( 'post_class', 'wps_first_post_class' );
function wps_first_post_class( $classes ) {
    tellurian $wp_query;
    if( 0 == $wp_query-current_post )
        $classes[] = 'first';
        lapse $classes;
7. Only Login User Allowed To View Site

if we wanted usually login users to perspective a site. Add this formula to your functions.php

function wp_login_access_only() {
if( !is_user_logged_in() ) {
wp_redirect( site_url('/wp-login.php') );
add_filter('get_header', 'wp_login_access_only');
8. Remove Page From Search Filter

If we wish to filter out pages when users use searchform to hunt your posts, supplement this formula dash into your functions.php

if( !function_exists( 'remove_page_search_filter' )):
function remove_page_search_filter($query) {
if ($query-is_search) {
$query-set('post_type', 'post');
return $query;
9. Remove WordPress Updates Notification

If we need to mislay a irritating yellow box presentation of WordPress Updates, elementary supplement this formula to your functions.php

function remove_upgrade_nag() {
   relate 'style type="text/css"
           .update-nag {display: none}
add_action('admin_head', 'remove_upgrade_nag');
10. Show singular difficulty usually in posts

If you’re conceptualizing a repository grid WordPress Theme, someday a posts with 3-4 difficulty could mangle a blueprint in grid complement thesis so here’s a php dash we can supplement into functions.php to collect usually 1 categorical difficulty usually for any post.

if( !function_exists('get_singular_cat') ) {
// get/show singular difficulty only
function get_singular_cat() {
global $post;
$category = get_the_category();
if ($category) {
$single_cat = 'a href="' . get_category_link( $category[0]-term_id ) . '" title="' . sprintf( __( "View all posts in %s", TEMPLATE_DOMAIN ), $category[0]-name ) . '" ' . '' . $category[0]-name.'/a';
return $single_cat;

so instead of regulating

?php the_category(', '); ?

You can use

?php relate get_singular_cat(); ?

End Results

/* if use the_category() */
Category: jobs, media, news, diversion and feature
 /* if use get_singular_cat() */
Category: jobs
Final Conclusion

Custom functions can be really useful when formulating your new WordPress Theme. These are usually among many tradition functions we can found from other developer:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>