Alan Edwardes

Pretty Search In WordPress

Old Content Warning

This post is very old (it was published 5 years ago), so I can't vouch for its content or accuracy. It may be here for posterity. Please take its content with a pinch of salt.

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

5 years, 3 months ago

I was 16 years old when I wrote this

216 words

rand: Cuil - The Good Looking ...

next: Random QR Code

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

share:FacebookTwitterRedditdiggStumbleUpondeliciousHacker NewsLinkedIn

Add a Comment

© 2006 – 2013 Alan Edwardes / Source on GitHub
Top