Skip to Content
Menu

is_bot()

Detect bot/crawler traffic.

PHP March 16, 2017

Usage

PHP
nebula()->is_bot()

Parameters

This function does not accept any parameters. Is this incorrect?

Additional Notes

This function requires the library Device Detection for advanced detection, but works with very basic functionality without it.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Utilities/Device.php on line 188.

    2 Hooks

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_is_bot"
    "nebula_bot_regex"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    Note: This function contains 1 to-do comment.

    PHP
            public function is_bot(){
                $override = apply_filters('pre_nebula_is_bot', null);
                if ( isset($override) ){return $override;}
    
                if ( $this->is_googlebot() ){
                    return true;
                }
    
                if ( !empty($this->super->server['HTTP_USER_AGENT']) ){
                    $bot_regex = array('bot', 'crawl', 'spider', 'feed', 'slurp', 'tracker', 'http', 'favicon', 'curl', 'coda', 'netcraft'); //Consider 'cloudflare-' as long as it is not ever used in actual users' user agent
                    $all_bot_regex = apply_filters('nebula_bot_regex', $bot_regex);
                    foreach( $all_bot_regex as $bot_regex ){
                        if ( strpos(strtolower($this->super->server['HTTP_USER_AGENT']), $bot_regex) !== false ){ //@todo "Nebula" 0: Update strpos() to str_contains() in PHP8
                            return true;
                        }
                    }
                }
    
                if ( $this->is_slackbot() ){ //The regex above should already capture this
                    return true;
                }
    
                return false;
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_is_bot', 'my_custom_is_bot'); 
    function my_custom_is_bot(){ 
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_is_bot', '__return_false');