WordPress tips and tricks: how to custumize your theme

WordPress tips and tricks: how to custumize your theme
by Janeth Kent 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 Janeth Kent Date: 10-07-2013 wordpress theme tips tricks code cms hits : 5830  
 
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.

 
 
 

Related Posts

Android Hidden Codes: unveiling custom dialer codes and their functionality

In the world of Android smartphones, there exist numerous hidden codes that can unlock a treasure trove of functionalities and features. These codes, known as custom dialer codes, provide access…

Secret iPhone codes to unlock hidden features

We love that our devices have hidden features. It's fun to learn something new about the technology we use every day, to discover those little features that aren't advertised by the…

Interesting and Helpful Google Search Features You’ll Want to Start Using

Google – THE search engine for many internet users. It has been with us since its launch back in 1998 and thanks to its simplicity of use and genius algorithms,…

How to use your browser as a file browser, in Chrome or Microsoft Edge

We're going to explain how to use the Chrome browser as a file browser, both on Android and on your computer. This is a hidden feature of Chromium that will…

How to make the website's dark mode persistent with Local Storage, CSS and JS

Recently we wrote about how to do a switchable alternative color mode or theme, a very useful and popular feature to websites. Today’s article is going to be about how…

Dark Mode on website using CSS and JavaScript

In today’s article we are going to learn how to build pretty much standard these days on the web pages and that is the alternative color mode and switching between…

A Java Approach: Selection Structures - Use Cases

Hello everyone and welcome back! Up to now we have been concerned to make as complete an overview as possible of the fundamental concepts we need to approach the use…

JavaScript: Spread and Rest operators

In today’s article we are going to talk about one of the features of the ES6 version(ECMAScript 2015) of JavaScript which is Spread operator as well as Rest operator. These features…

How to watch deleted or private Youtube videos

Today we are going to talk about the technique which you permit to be able to recover videos from Youtube that was deleted, made private or simply blocked by Youtube…

Hashmap: Overflow Lists

In this short series of articles we will go to see how it is possible to create the Hashmap data structure in C. In the implementation we're going to use the…

Data structures in Java - Linked Lists

With 2020 we are going to look at a new aspect of programming: data structures. It is often the case that everyone uses structures provided by the various programming languages.…

Introduction to REGEX - Regular Expression

Today we are going to write about Regular Expressions known as regex or shortened  regexp, a very useful concept of using search patterns. Surely you were in a situation when you…