This is a modification of the 404 function found at NSLog. This is tweaked such that if a page is not found on your WordPress blog, the part of the URI beyond the domain name is stripped off and fed into the WordPress search function. If only one post if found, that post is directly loaded. Otherwise the whole list of posts is loaded and displayed.
Learn more about it here:
http://weblogtoolscollection.com/archives/2004/08/05/404-search-function-for-wordpress/
davidprince
Member
Posted 4 years ago #
That is really cool. I am adding it now.
davidprince
Member
Posted 4 years ago #
Here is the answer if your blog is not in the root directory.
<?
$URI_prefix = "/blog/";
$search_preterm = str_replace($URI_prefix,"",$_SERVER['REQUEST_URI']);
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = stripslashes($search_preterm);
$search_term = urldecode(stripslashes($search_term));
$search_url = 'http://davidblog.ws/blog/index.php?s=';
$full_search_url = $search_url . $search_term;
$full_search_url = preg_replace('/ /', '%20', $full_search_url);
$full_page = implode("", file($full_search_url));
print_r($full_page); die();
?>
Change /blog/ to whatever subdirectory your blog is in.
Also.. You may want your .htacess in your blog dir to call a slightly different 404.php file (I call 404blog.php) that way the search text is formatted correctly regardless of whether the original 404 occurs in your root or blog directory.
Carl..
Your problem is because the code on original page is styled (in some way) on the authors page. Therefore you need to strip the styling or just copy what is below.
<?
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$search_url = 'http://davidblog.ws/blog/index.php?s=';
$full_search_url = $search_url . $search_term;
$full_search_url = preg_replace('/ /', '%20', $full_search_url);
$full_page = implode("", file($full_search_url));
print_r($full_page); die();
?>
CarLBanks
Member
Posted 4 years ago #
Thanks David! This is an awesome hack.
davidprince
Member
Posted 4 years ago #
Damn.. Neither peice of code is stripping tailing slashes..
davidprince
Member
Posted 4 years ago #
Hmm.. maybe not stripping a trailing slash is a good thing.
CarLBanks
Member
Posted 4 years ago #
http://carlbanks.triplehelix.info/firefox
My posts have firefox in them, but it's not returning anything.
davidprince
Member
Posted 4 years ago #
That is odd.
It seems to be showing your 404.php file in the address.
See this and compare http://davidblog.ws/blog/firefox
404.php does not show up in the address so something is messed somewhere.
Is the code you added to .htacess exactly like the example. I don't think a full path to 404.php worked for me.
davidprince
Member
Posted 4 years ago #
Check out my above link to see how I am calling the search to a newly created search.php. :) I think it gives the lost user an understanding of what is wrong.
CarLBanks
Member
Posted 4 years ago #
That's a cool addition, David!
davidprince
Member
Posted 4 years ago #
I made the 404.php call search.php, which is almost identical to index.php except I added the red text at the top of the page..
Here is the call to that page in 404.php:
$search_url = 'http://davidblog.ws/blog/search.php?s=';
That is the only line that is changed. The red text is added to a #topmain division where my breadcrumb is called.
davidprince
Member
Posted 4 years ago #
Sorry.. How exactly did you do that?
I am slow, so please speak slowy. :)
davidprince
Member
Posted 4 years ago #
Ah.. Okay I understand. Pretty cool I will whip something up.
So I don't use the WP search function (I use free Atomz search), but since I just migrated by whole blog over from MT and changed the URLs I thought this would be pretty handy. I changed this so that it just spits out a searchable term that I automatically put into the search box on my 404 page.
It goes like this:
<?
$search_term = substr($_SERVER['REQUEST_URI'],1);
$find = array ("'.html'", "'[-/_]'") ;
$replace = " " ;
$new_search = preg_replace ( $find , $replace , $search_term );
echo $new_search ;
?>
(I should probably figure out a way for it to remove the directory in the URL it's getting, but I'm lazy and this works OK. I tried a lot of variations on "'/.+/'" but I got nothing. Any suggestions? )
You can see it in action with this old URL: http://orangepolitics.org/about/one_community_many_voices.html
You could also check out Binary Bonzai's Kubrick web kit...it has a great 404 and search page.
Anonymous
Unregistered
Posted 4 years ago #
i m getting this error:
Parse error: parse error, unexpected ';' in /home/httpd/vhosts/deydas.com/httpdocs/404.php on line 131
-abhi.
davidprince
Member
Posted 4 years ago #
Did you take the code directly from the authors site? If so it has been styled and you need to use unstyled text. Ie the curly quotes are not acceptable. Try copying the code directly from this post.
<?
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$search_url = 'http://davidblog.ws/blog/index.php?s=';
$full_search_url = $search_url . $search_term;
$full_search_url = preg_replace('/ /', '%20', $full_search_url);
$full_page = implode("", file($full_search_url));
print_r($full_page); die();
?>
Anonymous
Unregistered
Posted 4 years ago #
yes david, i've unstyled it but still nothing...
-abhi.
FYI, in case anyone cares, I fixed my directory problem and made one more improvement. Now it only makes the new search term if it's a 404 error (this page handles all errors). So in my conditional cases that evaluate getenv("REDIRECT_STATUS") I added this code to part for 404's:
$search_term = substr($_SERVER['REQUEST_URI'],1);
$find = array ("'.html'", "'.+/'", "'[-/_]'") ;
$replace = " " ;
$new_search = preg_replace ( $find , $replace , $search_term );
and then in the search field I just put <?php echo ($new_search); ?> which only has a value if it was a 404 error.
Im loving the hack, but i've noticed a lot of searches for "/archives/2004/12/27/post-name/", which when searched for doesn't bring up any posts, how would i only have it search for "/post-name/"?