"page"). $slug = (string) preg_replace( '/\.(html?|php|aspx?|jsp)$/i', '', $slug ); // Turn a slug like "my-missing-page" into the phrase "my missing page". $terms = trim( str_replace( array( '-', '_', '+', '.' ), ' ', urldecode( $slug ) ) ); /** * Filters the search terms derived from the missing URL. * * Return an empty string to skip the redirect for this request. * * @since 1.2 * * @param string $terms The terms extracted from the requested URL. * @param string $path The requested URL path. */ $terms = apply_filters( 'p4zero4_search_terms', $terms, $path ); if ( ! is_string( $terms ) || '' === trim( $terms ) ) { return; } $redirect_url = get_search_link( $terms ); /** * Filters the destination URL the visitor is redirected to. * * Return an empty value to cancel the redirect for this request. * * @since 1.2 * * @param string $redirect_url The search results URL. * @param string $terms The search terms. * @param string $path The requested URL path. */ $redirect_url = apply_filters( 'p4zero4_redirect_url', $redirect_url, $terms, $path ); if ( empty( $redirect_url ) ) { return; } /** * Filters the HTTP status code used for the redirect. * * Defaults to 302 (temporary). Use 301 only if you are sure the mapping is * permanent, as browsers cache 301 responses aggressively. * * @since 1.2 * * @param int $status The HTTP status code. */ $status = (int) apply_filters( 'p4zero4_redirect_status', 302 ); wp_safe_redirect( $redirect_url, $status ); exit; } add_action( 'template_redirect', 'p4zero4_redirect' );