Alan Edwardes

Pretty Search In WordPress

Sure, you can enable the fancy blog post url's with the .htaccess file, but when someone uses the search box WordPress still sends the user to http://www.blogname.com/?s=Foobar, whatever you have your permalink structure set as. To get around this, i've used my own PHP file to redirect the user's search to http://www.blogname.com/search/Foobar/.

I created a file that I just dropped into the root of my blog directory called "pretty-search.php". This is the code it uses:

<?php $term = $_GET['s']; header("Location: http://www.blogname.com/search/$term/"); ?>

This simple bit of code changes the user's search request that would originally be http://www.blogname.com/?s=Foobar to http://www.blogname.com/search/Foobar/, to make the URL look better.

To make the script actually work though, I had to also point my searchform in the sidebar to the redirecting file. So, this can't be done if the search is a widget, as it is difficult to access the code.

<form method="get" id="searchform" action="/pretty-search.php">
<input type="text" value="Search <?php bloginfo('name'); ?>" name="s" id="s" />
</form>

When you've sorted that bit out, you're done. If you want to see a demo of the script in action, just type something in the search form at the top of the sidebar and hit enter. Remember, change the URL in pretty-search.php to your own blog url, currently it's set to "http://www.blogname.com/". ;)

pretty-search.php - 87b

24th of February 2008 at 9:35 AM

4 years, 2 months ago

written by Alan Edwardes.

216 words

rand: Using PHP isset To Detect ...

next: Random QR Code

prev: Play.com launch DRM-free MP3 downloads: ...

share:FacebookTwitterRedditdiggStumbleUpondeliciousHacker NewsLinkedIn

Add a Comment

© 2006 – 2012 Alan Edwardes / code on github
Top