Wordpress Tips and Tricks: How to Custumize Your Theme

by admin admin Date: 10-07-2013 wordpress theme tips tricks code cms


When you're in the process of writing a WordPress theme, there are a few behaviors you can change. Some of them are pretty basic, but they can really enhance the theme my making it go that extra mile.

How to Customize The Login Page

It’s quiet easy to customize the existing Login Page. If you want to change only the logo or want to code a completely different layout, you have to start with the: login_enqueue_scripts action. Here you can decide to output a simple piece of CSS or a link to a full CSS file. You can also queue up any JavaScript you might want to use.

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' );  

Be careful with the selectors! The basic class selectors you can use are:

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 {}  

If you want to go more in depth, you can visit The WordPress codex page.

IMPORTANT: the !important 'hack' is required for the ‘p#nav a’ and ‘p#backtoblog a’ selectors.

How to Add The Post Title To The Blog Title

There are some SEO plug-ins that do this but today we suggest a method for overriding the ‘wp_title’ default behavior:

Imagine that we want this only for the actual post. We simply have to grab the current post title and in this case prep-end it to the blog 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);  

How to Make Sure Rewrite Rules Are Up-To-Date

If the rewrite rules aren’t up-to-date when you update your theme, it’s possible for them to not render. There isn’t anything that inform that this is the problem, and the manual suggests you to go into Admin > Permalinks and clicking ‘Save Changes’.

We can automate this using the after_switch_theme action and then calling the flush_rewrite_rules:

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

How to Redirect A Search When There Is Only One Result

We can use the template_redirect action by confirming that this is a search and that the global query has only one result.

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');    

How to Customize Search To Only Check Post Titles

By overriding the posts_search filter you can provide an alternate SQL query for the search.

This isn’t actually documented, so it is something you may have to spend time maintaining if WordPress makes a change in a future release.

So, we have to grab the part of the get_posts method in query.php.

Then, we use it to overwrite the default search SQL and ignore the post content.

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 );  

You could of course customize this further and make it an option to include/exclude post content.

 
by admin admin Date: 10-07-2013 wordpress theme tips tricks code cms hits : 3775  
 
 
 
 

Related Posts