Jump to content

Help Me With This Wordpress Php Function Code?


cfs5403

Recommended Posts

This function will perform get search term from url if 404 not found and redirect the search term to http://www.website.com/?s=(my search term here), can help me edit the code abit?

function simple404_redirect() {
if(is_404()) {

 $get_params = "";
 if ( preg_match("@/?(\?.*)@", $_SERVER["REQUEST_URI"], $matches) ) {
  $get_params = $matches[1];
 }

 // Extract search term from URL
 $patterns_array = array();
 if ( ( $patterns = trim( get_option('ignored_patterns' ) ) ) ) {
  $patterns_array = explode( '\n', $patterns );
 }

 $patterns_array[] = "/(trackback|feed|(comment-)?page-?[0-9]*)/?$";
 $patterns_array[] = "\.(html|php)$";
 $patterns_array[] = "/?\?.*";
 $patterns_array = array_map(create_function('$a', '$sep = (strpos($a, "@") === false ? "@" : "%"); return $sep.trim($a).$sep."i";'), $patterns_array);

 $search = preg_replace( $patterns_array, "", urldecode( $_SERVER["REQUEST_URI"] ) );
 $search = basename(trim($search));
 $search = str_replace("_", "-", $search);
 $search = trim(preg_replace( $patterns_array, "", $search));
 $search_words = trim(preg_replace( "@[_-]@", " ", $search));  
 wp_redirect( 'http://www.website.com/?s=[how to put the $search here]', 301 );
}
}
add_action('template_redirect', 'simple404_redirect');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.