bAsic WordPress tricks

Here are some basic tricks to use for any WordPress site.

Javascript optimization:
If possible, put your all JS sources in footer.php file rather than in header.php file. This will reduce page loading time and optimize your page as well as good practice for SEO purpose.

To disable default <p> tags in WP
When we validate web page against W3C validator, it will generally show extra unwanted <p> tags. To remove them from the page, paste below code in your page.php template above the header call.

<?php remove_filter ('the_content', 'wpautop');
//disable wptexturize
remove_filter('the_content', 'wptexturize'); ?>


Copyright date on footer
Use below code in your footer to show current year copyrights.

<?=date('Y')?> or
<?php echo date('Y');?>
if above shortcode is not working.

You can also customize as below to show it like for ex: 2010-2011 copyrights….
<?php echo date('Y');?> - <?php echo date('Y')+1;?>

This will display current year and upcoming year.

Limiting excerpt on post/page
Sometimes we may need to use excerpt in our customized form. Here is the code you need to paste in function.php file of your theme. If you don’t have such file in your folder then create one.

Paste below code in functions.php:

function string_limit_words($string, $word_limit)
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
return implode(' ', $words);

Add this chunk in your template file, change the number 20 to the number you want to limit your excerpt for:
<?php $excerpt = get_the_excerpt();
echo string_limit_words($excerpt,20); ?>


Show last modified date on blog:
Paste below code in your footer.php file.
<?php echo "Last modified: " . $d; ?>
<?php $d = the_modified_date(); ?>
<?php echo "Last modified: " . date("d F, Y", getlastmod()); ?>


Include any file in WP site:
Below is the syntax to include file in your template. Write your file name in place of sidebar2.php in below syntax.
<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>


Custom permalinks:
Change option in your permalink setting in WP admin to any other option than that of default one. It will automatically generate URL as “www.sitename/about” or you can use custom permalink as %postname% for the same.

Customize Blog/site title:
Customize your site title in header.php file from that of default. Below is one of the option which will look
for ex: Site-name»About
<title><?php bloginfo('name'); ?> <?php wp_title(); ?> </title> - default separator &raquo ;

<title><?php bloginfo('name'); ?> <?php wp_title('&raquo;', true, 'left'); ?> </title> – you can modify separator by replacing &raquo; in above syntax.

<title><?php wp_title('&laquo;', true, 'right'); ?></title> – displays title as
for ex: About | Sitename

Show sub pages of a parent page on sidebar:
To display all the child pages of a parent post/page, write below code in your sidebar.php file. Change post id and page/post name for to the id and name of the page for which you want to display sub pages.

<?php  if(is_page('page_name') || $post->post_parent == post_id) : ?>
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo $children; ?>
<?php } ?>


Highlight active page item in navigation:
To highlight the menu item of an active page, use below code in your template file (usually header.php) where you have your navigation.

For ex: About page,
<a href="#" <?php if (is_page('about')){ echo "class=\"activetab\"";}?> >About</a>

Change the class name to CSS class you created for active menu item and page name to the page name  for which active tab needs to be highlighted.

Call for single post in sidebar:
To display “Quote of the day” for example in sidebar, first create a post named as “Quote of the day” You can add/edit message in this post later. Call for this single post in your sidebar.php using below code.

<?php query_posts('post_name'); if(have_posts()) : the_post();  ?>
<?php the_content(); ?> // displays content of the post that is "message"
<?php endif; ?>

Change the post_name to the name of the post which is “Quote of the day” post, for your sidebar.

Excluding specific category from home page only when home page is a post.

Paste below code in your template file(index.php) to exclude the posts from specific category on home page.
if (is_home()) {
query_posts("cat=-1, -4");

This code will remove posts from 1,4 categories and only from home page. Change the category ID accordingly.

Display tags on posts:

Paste below code in your template file (loop.php) where you want to display tags.
<?php the_tags('<strong>Tags:</strong> ', ', '); ?>

Find more on codex : http://codex.wordpress.org/Function_Reference/the_tags

Show comment counts on posts:

Write below code in your template file where you want to display comment counts.
<?php comments_number('No Comment', '1 Comment', '% Comments'); ?>

with hyperlink on that comment count,
<?php comments_popup_link ('No Comment','One Comment','% Comments','','none'); ?>


Display category title

Just paste below code in your template file where you want to display category title.

<?php $cat = get_category('cat- id');   echo $cat->cat_name; ?> change cat id with accordingly.

Hide admin bar (WP 3 version) from front end display:

Generally, I don’t like that default admin bar displaying over the header on my site. If you too, just add below code to your functions.php file template at beginning. Problem solved!

add_filter( 'show_admin_bar', '__return_false' );


Remove date from your search results:

If you want to remove default date only from your search results page and keeping it as it is on other posts on site, replace below code <?php twentyten_posted_on(); ?> in functions.php file with this code:
if ( is_search() ) :
else :
endif; ?>


Display recent posts in sidebar (or anywhere):

Write below code in template file where you want to display recent posts. This will show 4 recent posts.

<?php $myposts = get_posts('numberposts=4');
foreach($myposts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endforeach; ?>


WP default login/register features:

If you want to use default WP functionality for login/ register features on your WP site, you can simply provide hyper links like below:

<a href="<?php echo get_option('home'); ?>/wp-login.php">Login</a>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=register">Sign up</a>


Search input box on-blur feature:

When we are using WP search module for the site, text box value of the input doesn’t change on focus. We need to customize the code a little bit so when user clicks in textbox the default value disappear and user can type search key words. This can be done by replacing a code in your site’s WP-Includes>general-template.php file,

Find below code (line no – 162) in to general-template.php file
<input type="text" value="' . get_search_query() . '" name="s" id="s" />

and replace it with below code :

<input type="text" value="' . 'Search' . '" name="s" id="s" onfocus=" if(this.value == \'Search\') this.value=\'\' " onblur="if(this.value==\'\') this.value=\'Search\'" />

Hope it helps! Enjoy coding.


Happy to Hear...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s