• This function will perform get search term from url if 404 not found and redirect the search term to https://localhost/wordpress/?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( ‘https://localhost/wordpress/?s=%5Bhow do I put my search term here?] ‘, 301 );
    }
    }
    add_action(‘template_redirect’, ‘simple404_redirect’);

  • The topic ‘Help me with this wordpress php function code’ is closed to new replies.