0

I would like to block certain keywords (multiple keywords, banned keywords like fuk, sit etc.) from searching on Wordpress blog and show/redirect visitor/member (searcher) to different page.

Used this code but it returned 500 internal server error:

    add_action('wp', 'check_search');
function check_search() {

    global $wp_query;

    if (!$s = get_search_query())
        return false;

    if preg_match('/^(word1|word2|word3)$/', $s)) {
        $wp_query->set_404();
        status_header(404);
        get_template_part(404);
        exit();
    }

}

How to fix and How to do the redirection?

  • It works for me if I fix the syntax error: if ( preg_match. Always turn on debugging during developing. – Max Yudin Dec 21 '18 at 11:57

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.