Recommended wp_title Filter in WordPress

Ziad Rahhal on

Who doesn’t want a meaningful title for his/her Web site? Those of you who are aware of the importance of the <title> tag will take a good care of their code to properly render content inside this element.

emotional support cat paper writer online casino top s movers com

When it comes to WordPress, working inside <title> is a bit of art. It is highly dynamic, customized and there are different ways of writing it. The WordPress team has recently made a recommendation with regards to properly using the wp_title() function. I quote the codex:

The wp_title() function should not be used by a theme in conjunction with other strings or functions (like concocting with bloginfo(‘name’) ) to set the title because it will render plugins unable to rewrite page titles correctly. The best practice is to use the wp_title filter with a callback function. This method is now a requirement for themes.

Something like <title><?php bloginfo(‘name’); ?> | <?php wp_title(); ?></title> inside header.php is a bad practice, because it prevents a plugin to alter the whole title. So we must use the wp_filter with a custom callback function.

Wiki Duh has written a how to, which I enhanced in order to properly deal with a situation where the user (theme developer) decides to position the separator to the right of the title. In my child theme’s functions.php file, I added the following code:

/* a custom callback function that displays a meaningful title
 * depending on the page being rendered
 */
function elevenchild_filter_wp_title($title, $sep, $sep_location) {
  // add white space around $sep
  $sep = ' ' . $sep . ' ';
  $site_description = get_bloginfo('description');
  if ($site_description && (is_home() || is_front_page()))
      $custom = $sep . $site_description;
  elseif(is_category())
      $custom = $sep . __('Category', 'twentyeleven');
  elseif(is_tag())
      $custom = $sep . __('Tag', 'twentyeleven');
  elseif(is_author())
      $custom = $sep . __('Author', 'twentyeleven');
  elseif(is_year() || is_month() || is_day())
      $custom = $sep . __('Archives', 'twentyeleven');
  else
      $custom = '';
  // get the page number (main page or an archive)
  if(get_query_var('paged'))
    $page_number = $sep . __('Page ', 'twentyeleven') . get_query_var('paged');
  // get the page number (post with multipages)
  elseif(get_query_var('page'))
    $page_number = $sep . __('Page ', 'twentyeleven') . get_query_var('page');
  else
    $page_number = '';
  // Comment the 4 lines of code below and see how odd the title format becomes
  if($sep_location == 'right' && !(is_home() || is_front_page())) {
      $custom = $custom . $sep;
      $title = substr($title, 0, -2);
  }
  // return full title
  return get_bloginfo('name') . $custom . $title . $page_number;
} // end of function elevenchild_filter_wp_title
/* add function 'elevenchild_filter_wp_title()' to the
 * wp_title filter, with priority 10 and 3 args
 */
add_filter('wp_title', 'elevenchild_filter_wp_title', 10, 3);

 
Now in your theme or in your child theme’s header.php file, you can call wp_title() like this:

<title>
  <?php wp_title('|', true, 'left'); ?>
  // OR <?php wp_title('|', true, 'right'); ?>
</title>

 
Don’t hesitate to use the above code as is without any contribution to me. You are welcome to report bugs, modify the code and make corrections.

CSS adjacent selector bug in IE7

Ziad Rahhal on

I was going through some of my old notes, and found one about a bug in IE7 that is related to the adjacent sibling selector (+)  in CSS. If you want to use the adjacent sibling selector in IE7, you cannot insert an HTML comment between the elements you are targeting; the comment will cause IE7 to ignore the CSS rule. let’s look at a simple example:

h3 + p {
    color: red;
}

Read more

How to solve: IDLE can’t import Tkinter. Your Python may not be configured for Tk.

Ziad Rahhal on

It has been twice that I forget Tkinter when I install Python-3.2 as an alternative to the default installed Python-2.6.5 on Ubuntu-10.04.  On those two occasions, I discovered that Tkinter was missing when I tried to launch IDLE. It will not start without Tkinter and it will report the following error line:

* IDLE can’t import Tkinter.  Your Python may not be configured for Tk. **

To fix it, simply install python3-tk (for Python 3) and tk-dev before building Python:

sudo aptitude install python3-tk
sudo aptitude install tk-dev

 
Now build and install Python3.2:

./configure
make
sudo make altinstall    # for multiple versions of Python to co-exist

You can always look at the README file for more sophisticated options with regards to installing Python.

Now you must be able to successfully start idle to write and test Python code.

P.S.1. In case you have installed Python3.2 before installing Tkinter, simply install python3-tk and tk-dev and rebuild Python3.2.

P.S.2. You don’t need to explicitly install idle with Python 3, because it is included by default. This is not the case with Python 2.6.5 for example.

How to Install Sun Java SE Development Kit - JDK on Ubuntu 10.04

Ziad Rahhal on

If you want to write and run Java programs, you need the Java SE Development Kit (JDK) which includes the necessary tools to compile and run Java programs.

In this post, I’ll show you how I got my Java environment set up on a Ubuntu-10.04 LTS (32 bit) box.

First Step

Go to the download page and download the Linux x86 compressed binary. It is jdk-7-linux-i586.tar.gz at the time of this writing. Unless you have changed the default settings, firefox will save it into the Downloads folder.

Second Step

Extract jdk-7-linux-i586.tar.gz into your preferable location:

I decided to install JDK in /opt/usr/local, so that is where I will extract:

sudo cp /home/yourHome/Downloads/jdk-7-linux-i586.tar.gz /opt/usr/local/
cd /opt/usr/local/
sudo tar -zxvf jdk-7-linux-i586.tar.gz

this will create a folder called jdk1.7.0
Read more