Case-insensitive tab completion in the Mac OS X Terminal

If you are like me, you use the Terminal a lot. By default the terminal has case-sensitive tab completion, meaning that if you type ‘s’ and press tab it would not suggest a file/folder called ‘S’.

Luckily, this is quite an easy fix.

Create, or if you already have it; edit, a file called .inputrc in your ‘Home’ folder.

In it write:

set completion-ignore-case On

Restart Terminal and you are done!

Tagged with: ,
Posted in Beginner

New plugin – Quick Find!

This plugin adds a hierarchical navigation to your ‘Pages’ menu. This enables you to quickly find the page you want to edit.

Simply activate it to get it working!

Download now

Screenshot:

Tagged with: ,
Posted in Beginner

Post Formats

Turn your WordPress blog into a Tumblresque blog with Post Formats.

Post Formats, introduced with Version 3.1, are meta information that can be used by themes to customize presentation of a post. The basic idea is to provide a specific method for specifying the display “format” of a post. This replaces the need to use categories to accomplish the same thing, and even more importantly, is portable between themes that support those formats!

http://codex.wordpress.org/Post_Formats

Tagged with: ,
Posted in Intermediate

Which variables are available in your template files?

When you are coding a theme you usually edit standard files such as index.php, single.php, page.php and so on.

In these files you might have noticed that some variables are available even though you have not declared them. Such a variable would be $post for example.

But which other variables are available?

To find out, take a peek in the function load_template in wp-includes/theme.php. In this function alot of different global variables are declared. Normally, theme files are loaded through template-loader.php and to find out exactly which variables are available in that variable scope might be tricky, but the global variables in this function gives us an idea.

  • $posts
  • $post
  • $wp_did_header
  • $wp_did_template_redirect
  • $wp_query
  • $wp_rewrite
  • $wpdb
  • $wp_version
  • $wp
  • $id
  • $comment
  • $user_ID

Also, all the variables in the query_vars array are extracted and made available.

A way to make a custom variable available in your files would be to simply declare it in your theme’s functions.php file.

Tagged with: , , ,
Posted in Advanced

Query another blog on your site

I wrote some simple code so that you could query posts from another blog on your site by using the argument blogid in your query

function blogid_query_set_blog_id( $query ) {
 global $wpdb;

 if ( isset($query->query_vars['blogid']) && $query->query_vars['blogid'] != $wpdb->blogid ){
 $query->original_blog_id = $wpdb->blogid;
 $wpdb->set_blog_id( $query->query_vars['blogid'] );
 }
}
add_action( 'pre_get_posts', 'blogid_query_set_blog_id' );
add_action( 'loop_start', 'blogid_query_set_blog_id' );

function blogid_query_restore_blog_id( $query ) {
 global $wpdb;

 if ( isset($query->query_vars['blogid']) && isset($query->original_blog_id) )
 $wpdb->set_blog_id( $query->original_blog_id );
}
add_action( 'loop_end', 'blogid_query_restore_blog_id' );

function blogid_query_posts_results( $posts, $query ) {
 blogid_query_restore_blog_id( $query );
 return $posts;
}
add_filter( 'posts_results', 'blogid_query_posts_results', 10, 2 );

So how does it work?

query_posts( 'blogid=3' );

It work pretty well with little, or no testing so far. Please write in the comments if you find any problems with it!

Tagged with: , , ,
Posted in Intermediate

Add the browser name and version to the body class

As suggested by Phil Irish, the best way to make browser specific CSS is with a class on the body element rather than separate CSS files or complex hacks.

In his example he used conditional HTML to create multiple body-tags, but with this simple function you can just add the browser name and version to the classes printed by WordPress’ body_class function.

function mytheme_body_class( $class ) {
 $arr = array(
 'msie',
 'firefox',
 'webkit',
 'opera'
 );
 $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );

 foreach( $arr as $name ) {
 if( strpos( $agent, $name ) > -1 ) {
 $class[] = $name;

 preg_match( '/' . $name . '[\/|\s](\d)/i', $agent, $matches );
 if ( $matches[1] )
 $class[] = $name . '-' . $matches[1];

 return $class;
 }
 }

 return $class;
}
add_filter( 'body_class', 'mytheme_body_class' );

Modify the array if you want to sniff out even more browsers. Simple enough I think!

Tagged with: , ,
Posted in Beginner

Keep a track of what happens in your header and footer

When developing a theme you should execute the wp_head and wp_footer functions in your theme’s header and footer. The functions are hooked with an action that enables other plug-ins to add necessary files and code. But it is always good to know what happens, and how to remove unwanted code.

Actions and filters are stored in a variable called $wp_filter, so I recommend that you take a peek in it. For example, to see which functions that gets executed in your header, simply look in $wp_filter[‘wp_head’].

Array
(
..
[10] => Array
(
[wp_generator] => Array
(
[function] => wp_generator
[accepted_args] => 1
)
..
)
)

Here you can easily see why you have all these meta-tags in your header. And if you for some reason don’t want some or any of these, use the function remove_action. Just make sure you remove the actions before the action is executed.

Example:

function remove_unwanted_actions(){
remove_action('wp_head', 'wp_generator');
}
add_action('wp', 'remove_unwanted_actions');

Now I wouldn’t recommend using this to remove default WordPress actions, but if a plug-in adds some unnecessary stylesheets this is a good way to remove them!

Tagged with: ,
Posted in Intermediate

Create a custom menu by creating your own Walker

Alright, so I usually use wp_list_pages to create my menus. This usually works pretty well, but every now and then a project comes along that requires more than the functionality provided by the wp_list_pages function.

So maybe you want a menu that should have a nested sub menu when viewing a page and/or child to one of the “main pages”.

This could be done by passing a custom Walker to the wp_list_pages function.

class Custom_Walker_Page extends Walker_Page {
	function start_el(&$output, $page, $depth, $args, $current_page) {

		extract($args, EXTR_SKIP);

		$css_class = array();
		$extra = '';

		if ( !empty($current_page) ) {
			$_current_page = get_page( $current_page );
			if ( ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) ) || ( $page->ID == $current_page ) || ( $_current_page && $page->ID == $_current_page->post_parent ) )
				$css_class[] = 'sel';

			if ( $page->ID == $current_page || in_array($page->ID, (array) $_current_page->ancestors) ) {
				$sub = $current_page;
				if( count($_current_page->ancestors) > 1 )
					$sub = $_current_page->ancestors[0];

				$extra = wp_list_pages('title_li=&echo=0&child_of=' . $sub );

				if ( $extra )
					$extra = '<ul class="sub">' . $extra . '</ul>';
			}
		}

		$css_class = implode(' ', apply_filters('page_css_class', $css_class, $page));

		$output .= '<li' (="" $css_class="" ?="" class="' . $css_class . '" :="" )="" .=""><a href="%27%20.%20get_page_link%28$page-%3EID%29%20.%20%27" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page-&gt;post_title ) ) ) . '"><span>' . apply_filters( 'the_title', $page-&gt;post_title ) . '</span></a>';
		$output .= $extra;

	}
}

And then pass it to the wp_list_pages function:

$walker = new Custom_Walker_Page();
wp_list_pages( array( 'child_of' =&gt; $parent_id, 'depth' =&gt; 1, 'walker' =&gt; $walker) );

I guess this is pretty advanced stuff, so I have created some new categories. This post falls into the “Advanced” category.

Tagged with: , ,
Posted in Advanced

Add the excerpt meta box to ‘Edit page’

I do not know why the excerpt meta box is not shown by default when editing pages. I often list pages when using WordPress as a CMS, and I very often use excerpt when doing lists.

So how do you get the excerpt box like posts without using a plugin?

Simply add this code to your functions.php

function mytheme_addbox() {
add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
}
add_action( 'admin_menu', 'mytheme_addbox' );

Simple enough? Good!

Tagged with: ,
Posted in Intermediate
About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 13 other subscribers
Design a site like this with WordPress.com
Get started