Home > Enterprise >  Return A 404 Error For Wordpress Search Page, Unless Someone Has Visited The Website First
Return A 404 Error For Wordpress Search Page, Unless Someone Has Visited The Website First

Time:01-23

Bit of a strange question here people.

Would like to be able to put together some code, so that if someone directly types in our website url, with the default Wordpress search query at the end www.ourdomain.com/?s=your search words, that this would return a 404 error, as we no that anyone typing in our website url, with a dodgy search url attached is a spam bot.

However, if someone were to land on our website first, and then type in a search query, the 404 page wouldn't be returned, as we know that they're likely to be a genuine customer and not a bot.

Trying to stop a large search spam attack, that we're facing.

Have tried various plugins etc, with no luck so far, and suddenly thought of this as a possible answer?

Code to start with:

add_action( 'template_redirect', function () {
    
  if (is_search() ) {

Tried a few Wordpress plugins, but no luck with them at all.

CodePudding user response:

add_action( 'template_redirect', 'my_search_404' );
function my_search_404() {
    if ( is_search() && !isset($_SERVER['HTTP_REFERER'])) {
        wp_die( '404: Page not found', '404: Page not found', array( 'response' => 404 ) );
    }
}

But you need to consider a good security plugin , this is not enough for a large spam attack .

  • Related