5 WordPress Tricks to Improve Theme Performance

0
5694

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.
[sourcecode language=”php”]
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 );
[/sourcecode]

 

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
[sourcecode language=”php”]
function theme_flush_rewrite() {
flush_rewrite_rules();
}
add_action( ‘after_switch_theme’, ‘theme_flush_rewrite’ );
[/sourcecode]

 

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.
[sourcecode language=”php”]
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’ );
[/sourcecode]

But, you must aware of basic class selectors to override the correct selectors, here is the complete list of basic class selectors –
[sourcecode language=”css”]
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 {}
[/sourcecode]

 

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.
[sourcecode language=”php”]
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’);
[/sourcecode]

 

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.
[sourcecode language=”php”]
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);
[/sourcecode]