5 WordPress Tricks to Improve Theme Performance

Posted by Joe Burrill | On | July 3, 2013 | Filed under Development,Wordpress

WordPress tricks are quite popular among web developers, as these are used to reduce stress from developer’s mind. WordPress tricks are some kind of hacks done onto PHP code of the WordPress installation. We all are aware with WordPress theme feature which lets you change your website design without redesigning it from scratch. Just choose a WordPress theme out of millions available on internet and install it on your WordPress website.

 

WordPress theme performance depends upon its features and design strategies used by the designers. WordPress tricks can improve WordPress website by playing with theme function codes of WordPress theme.
 

5 WordPress Tricks to Improve Theme Performance

 
Today, we’re going to feature 5 WordPress tricks to improve your WordPress theme performance. We hope you will enjoy this article and these WordPress tricks will help you to get rid of flaws of your WordPress theme performance.

 


 

1. Restrict Search Results to Article Titles Only

The most annoying moment for a visitor is not getting desired topic on a website while searching, instead getting full of SH*T off the topic results. By default WordPress make search through whole contents including tags, categories, article contents etc. But visitor always wants accurate results mostly depends upon article titles only. By getting ‘get_posts‘ method and customizing default SQL search behavior to the article title only.

function theme_title_only_search( $search, &$wp_query ) {  
    global $wpdb;
    
    if ( empty($search) )  
        return $search; // skip processing - no search term in query  
    
    $search = '';
    
    $q = $wp_query->query_vars;
    
    // Fill again in case pre_get_posts unset some vars.
    $q = $wp_query->fill_query_vars($q);
    
    if ( isset($q) && !empty($q['s']) ) {
        // added slashes screw with quote grouping when done early, so done later
        $q['s'] = stripslashes($q['s']);
        if ( empty( $_GET['s'] ) && $wp_query->is_main_query() )
            $q['s'] = urldecode($q['s']);
        if ( !empty($q['sentence']) ) {
            $q['search_terms'] = array($q['s']);
        } else {
            preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
            $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
        }
        $n = !empty($q['exact']) ? '' : '%';
        $searchand = '';
        foreach( (array) $q['search_terms'] as $term ) {
            $term = esc_sql( like_escape( $term ) );
            $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
            $searchand = ' AND ';
        }

        if ( !empty($search) ) {
            $search = " AND ({$search}) ";
            if ( !is_user_logged_in() )
                $search .= " AND ($wpdb->posts.post_password = '') ";
        }
    }
    return $search;
}  
add_filter( 'posts_search', 'custom_search', 10, 2 );

 

2. Up-to-Date Rewrite Rules Automatically

This is the most important WordPress code, your theme’s rewrite rule must be up-to-date to render customized functions perfectly. This is important because, you won’t get notify about this if error occurs. Manually, you can do it by going to the Admin panel then updating Permalink page. But using the following code you can do it automatically by calling ‘flush_rewrite_rules’ function-

function theme_flush_rewrite() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'theme_flush_rewrite' );

 

3. Customize The Login Page

Login page of your Wodpress site in very important, if you have very popular website then you have to improve it for better visitor experience. Well, WordPress won’t let you add additional login page to your site, but you can use following WordPress Trick to customize it for better. You can brand your site’s login page by placing a logo onto it. You need to edit login_enqueue_scripts action.

function theme_customize_login() { 
?>
    <link rel="stylesheet" href="<?php echo get_bloginfo( 'stylesheet_directory' ) . '/login.css'; ?>" type="text/css" media="all" />
<?php 
}
add_action( 'login_enqueue_scripts', 'theme_customize_login' );

But, you must aware of basic class selectors to override the correct selectors, here is the complete list of basic class selectors –

body.login {}
body.login div#login {}
body.login div#login h1 {}
body.login div#login h1 a {}
body.login div#login form#loginform {}
body.login div#login form#loginform p {}
body.login div#login form#loginform p label {}
body.login div#login form#loginform input {}
body.login div#login form#loginform input#user_login {}
body.login div#login form#loginform input#user_pass {}
body.login div#login form#loginform p.forgetmenot {}
body.login div#login form#loginform p.forgetmenot input#rememberme {}
body.login div#login form#loginform p.submit {}
body.login div#login form#loginform p.submit input#wp-submit {}
body.login div#login p#nav {}
body.login div#login p#nav a {}
body.login div#login p#backtoblog {}
body.login div#login p#backtoblog a {}

 

4. Redirecting to the Content if There is only One Result in Search

This is somehow like Google’s ‘I’m Feeling Lucky’ functionality, You will be served to the content directly if there is only one result in your search query. This will save your time to reach to the content and your visitors will love this functionality for sure. You have to customize ‘template_redirect‘ action to achieve this functionality.

function theme_show_on_match() {  
    if (is_search()) {  
        global $wp_query;  
        if ($wp_query->post_count == 1) {  
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );  
        }  
    }  
}
add_action('template_redirect', 'theme_show_on_match');  

 

5. Suffix Blog Title With Post Title

This WordPress hack is for SEO perpose, many SEO WordPress plugins offer this feature of adding your website title just after post title. This is SEO tactics to follow, but this can be done without using any plugin. All you need to override the behavior of ‘wp_title’. By using following WordPress code hack you can achieve the above desired action in the browser window title.

function theme_add_post_title_to_blog_title($title, $sep) {
    if( is_single() ) {
        global $post;
        $title = $post->post_title . ' | ' . get_bloginfo( 'name' );
    }
    return $title;
}

add_filter('wp_title', 'theme_add_post_title_to_blog_title', 10, 2);

♂ Author Info ♀

Written by Joe Burrill

Joe Burrill has written 639 posts on Blogger's Path

Spread the ❤

Related Posts☟

Comments

  1. By Saiful - July 3, 2013

    I like the last one there, as suffixing the blog title with the post title will help to improve the SEO things.

  2. By Rajkumar Jonnala - July 4, 2013

    Hey Amit,
    Very nice tutorial you have shared.
    But are you sure, that after updating and adding the codes, the security over the blog would remain same?
    As we update the parsing info from login page, might be they create some vulnerability so that hackers can put themselves easily.
    Just a question though.

  3. By Raad - July 4, 2013

    i agree with Saiful’s point.

  4. By Amit - July 5, 2013

    Security would be intact as the original WP theme offers, either these tips to improve performance, security measure should be taken at user’s end.

  5. By Salon - July 5, 2013

    Thanks for authors, your blog is very attractive and useful for me.you shared good knowledge of the site.

Leave a comment