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');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.